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

时间:2017-11-15 10:01:37

标签: java r windows tcp-ip rserve

  • 我正在使用cmd从服务器计算机运行RServe

    Rserve.exe --RS-conf Rserv.conf --RS-port 12306
    
  • Rserv.conf文件包含以下内容:

    pwdfile RserveAuth.txt
    需要认证的 远程启用
    明文禁用

  • RserveAuth.txt包含以下内容:

    管理员123456

  • 我从JAVA

    连接到R服务器
    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();            
    } 
     }
    }
    
  • 所有没有用户名的人都可以与Rserve建立连接。密码。如何添加安全性并仅允许使用有效凭据连接以访问Rserve

1 个答案:

答案 0 :(得分: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());

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

相关问题