How to resolve exception "eval failed, request status: error code: 127" in R and Java?

StackOverflow https://stackoverflow.com/questions/21907545

  •  14-10-2022
  •  | 
  •  

문제

I am using R and Java for displaying prediction.

I have data of 5 hours. I want to predict 5th-hour data from four hours' data (memory with respect to date). By using 4 hours' data I am creating new collection and inserting the 5th hour's predicted data in a new collection. But I am getting the following error:

The Exception is eval failed, request status: error code: 127
org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:233)
at scheduler.scheduler.predictions.getPredictionsofData(predictions.java:45)
at pack.GetCollectionMultithreaded.getPredictionAndInsert(GetCollectionMultithreaded.java:386)
at pack.GetCollectionMultithreaded.runCustomerListAndPredictionEvery5Min(GetCollectionMultithreaded.java:155)
at pack.GetCollectionMultithreaded.main(GetCollectionMultithreaded.java:103)

Here is code:

public class predictions {

public void getPredictionsofData(DB dbObj){
 FileInputStream fis = null;

 DBCollection network_device_realtime = dbObj.getCollection("mycollectionname");
DBObject return_dobject = null;

// For Network device1 realtime
try{
 List<String> listOfIps = network_device_realtime.distinct("hostId");
 RConnection c = new RConnection(Rhost,Rport);
 c.eval("library(RMongo)");
 c.eval("library(plyr)");
 c.eval("library(randomForest)");
 c.eval(" db <- mongoDbConnect('demo','localhost',27017)");
 for( int i= 0 ;i<listOfIps.size(); i++){
     float my_predicted_date = 0 ;
             BasicDBObject criteria = new BasicDBObject();
     BasicDBObject projections = new BasicDBObject();
     criteria.put("hostId",listOfIps.get(i));
    projections.put("runtimeMillis", 1);
    DBCursor cursor = network_device_realtime.find(criteria,projections).sort(new BasicDBObject("runtimeMillis",-1)).limit(1);
    while(cursor.hasNext()) {   
        BasicDBObject obj = (BasicDBObject) cursor.next();
        my_predicted_date = (float) obj.getDouble("runtimeMillis");
    }
    // Set predict date for testing purpose 
     my_predicted_date = my_predicted_date-(4*60*60*1000);

    // for calculating predictions next 24 hrs
    for(int j = 1; j <= 12 ;j++){
            my_predicted_date = my_predicted_date+(300*1000);//j*60*60*1000calculating next hrs data
        System.out.println("Date Gen in network: " +my_predicted_date);

         c.eval("query <- dbGetQuery(db,'"+network_device_realtime+"','{\"hostId\":\""+listOfIps.get(i)+"\",\"cpuUtilization\":{\"$ne\":\"null\"},\"memoryUtilization\":{\"$ne\":\"NaN\"},\"runtimeMillis\":{\"$ne\":\"null\"}}')");

         c.eval("date <- query$runtimeMillis");
			 c.eval("host_id <- query$hostId");
         c.eval("cpu <-  query$cpuUtilization ");
			 c.eval("memory <- query$memoryutil");
         c.eval("all_data<-data.frame(cpu,date)");
         c.eval("training<- all_data");

         c.eval("rf_fit<-randomForest(memory~date,data=training)");
         c.eval("df <- data.frame(date="+my_predicted_date+ ")");
         c.eval("predictions<-predict(rf_fit,newdata=new)");
         REXP memory_predictions= c.eval("predictions");
         c.eval("rf_fit<-randomForest(cpu~date,data=training)");
         c.eval("df <- data.frame(date="+my_predicted_date+ ")");
         c.eval("predictions<-predict(rf_fit,newdata=new)");
         REXP cpu_predictions= c.eval("predictions");
         String json = ""; 
         json ="{\"memoryUtilization\":"+ memory_predictions + ",\"cpuUtilization\" : "+ cpu_predictions + ",\"hostId\" : \""+ listOfIps.get(i) + "\",\"runtimeMillis\":"+my_predicted_date+",\"deviceType\":\"snmp\"}";
         return_dobject=(DBObject) JSON.parse(json);
         dbObj.getCollection("prediction").insert(return_dobject);
        }
     }
         c.close();
    }
catch(Exception e){
     System.out.println("ERROR: In Connection to R ");
     System.out.println("The Exception is "+ e.getMessage());
     e.printStackTrace();
 }
}
}//class

In this code I am getting error on this line:

 c.eval("rf_fit<-randomForest(memory~date,data=training)");

How do I resolve this error?

도움이 되었습니까?

해결책 2

This exception mainly occurs due to data in statement

c.eval("rf_fit<-randomForest(memory~date,data=training)");

contains null.

This is might be due to bug in your data framing. Please check it once.

다른 팁

To get the proper error message, use this instead of simple eval

REXP rResponseObject = rServeConnection.parseAndEval(
    "try(eval("+R_COMMAND_OR_SOURCE_FILE_PATH+"),silent=TRUE)");
if (rResponseObject.inherits("try-error")) {
  LOGGER.error("R Serve Eval Exception : "+rResponseObject.asString());
}

This logger prints exact error thrown from R.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top