码迷,mamicode.com
首页 > 其他好文 > 详细

grpc 创建者模式的使用

时间:2017-05-19 20:22:25      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:tin   protoc   list   double   UI   response   message   etc   except   

  复杂对象创建模式样例:

  

RequestInstance2.RequestInstance.Builder builder = RequestInstance2.RequestInstance.newBuilder();
for(int i=0;i<features.size();i++){
String feature = features.get(i);
String[] arr = feature.split(",");
if(arr==null)
continue;

RequestInstance2.Feature.Builder featuresBuilder = RequestInstance2.RequestInstance.newBuilder()
.getCommonFeature().toBuilder().addFeaturesBuilder(0);
for (int j = 0; j < arr.length; j++) {

featuresBuilder.setName(String.valueOf(j + 1)).setSValue(arr[j]);
}
builder.addInstanceFeature(i, RequestInstance2.RequestInstance.newBuilder()
.getCommonFeature().toBuilder().addFeatures(featuresBuilder.build()));
}

RequestInstance2.RequestInstance instance = builder.build();
byte[] bytes = instance.toByteArray();
ByteString bs = ByteString.copyFrom(bytes);

InputHead inputHead = InputHead.getDefaultInstance().toBuilder().setVersion(20).setBodySize(bs.size()).addAppKeys(0).setAppKeys(0, 85001).build();
Request request = Request.getDefaultInstance().toBuilder().setHead(inputHead).setBody(bs).build();

Response response = null;
try {
response = blockingStub.getPredictorValue(request);
} catch (StatusRuntimeException e) {
logger.error("RPC failed: "+ e.getStatus());
return null;
}
double value = 0d;
List<Double> doubles = new ArrayList<>();
if(response!=null){

ByteString tempBytes = response.getBody();
try {
pino_resp_pb.Resp resp = pino_resp_pb.Resp.parseFrom(tempBytes);

if(resp.getOutputCount()>0){
pino_resp_pb.Output output = resp.getOutput(0);

pino_resp_pb.Matrix matrix = output.getMatrix();
if(matrix!=null&&matrix.getDoubleValCount()>0){
for(int i=0;i<matrix.getDoubleValCount();i++){
value = matrix.getDoubleVal(i);
doubles.add(value);
}
}
}
} catch (InvalidProtocolBufferException e) {
logger.error(e.getMessage(),e);
}
}

  

grpc 创建者模式的使用

标签:tin   protoc   list   double   UI   response   message   etc   except   

原文地址:http://www.cnblogs.com/freedommovie/p/6880028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!