使用身份验证从JAVA连接到RServe

Akk*_*kki 13 java windows r tcp-ip rserve

  • 我使用cmd从Server机器运行RServe

    Rserve.exe --RS-conf Rserv.conf --RS-port 12306
    
    Run Code Online (Sandbox Code Playgroud)
  • Rserv.conf文件包含以下内容:

    pwdfile RserveAuth.txt
    auth required
    remote enable
    plaintext disable

  • RserveAuth.txt具有以下内容:

    管理员123456

  • 我从JAVA连接到R Server

    import org.rosuda.REngine.REXPMismatchException;
    import org.rosuda.REngine.REngineException;
    import org.rosuda.REngine.Rserve.RConnection;
    import org.rosuda.REngine.Rserve.RserveException;
    import org.rosuda.REngine.REXP;
    import org.rosuda.REngine.*;
    public class ConnecttoR
    {
      ...
      ...
     public void connectR()
     {
         try 
        { 
            RConnection connection = new RConnection("172.16.33.242",12306); // Works if authentication is not required in Rserv.conf 
        }
        catch (RserveException e) {
        e.printStackTrace();
        } 
        catch(REXPMismatchException e){
        e.printStackTrace();
        }
        catch(REngineException e){
        e.printStackTrace();            
    } 
     }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 无需用户名和密码即可连接所有人.如何添加安全性并仅允许使用有效凭据连接以访问Rserve

Bab*_*abl 4

由于您在创建连接后启用了身份验证作为第一个命令,因此您需要执行该login命令。Java 库有一个特殊的包装器。

有关示例用例,请参阅下面的代码。

RConnection connection = new RConnection("127.0.0.1",12306);
connection.login("Admin", "123456");
REXP x = connection.eval("R.version.string");
System.out.println(x.asString());
Run Code Online (Sandbox Code Playgroud)

另外,我建议使用完整路径作为pwdfile值。