Protobuf 实例(java)

int64 recipe_id = 1;

string recipe_code = 2;

string dimension = 3;

repeated TempRecord oven_temp_list = 4;

repeated TempRecord probe_temp_list = 5;

}

message TempRecord {

int32 temp = 1;

int64 timestamp = 2;

}

注:protobuf属性解释可以参照另一篇文章,传送门:Protobuf 属性解释

在Java中的写法,代码如下:

@Override

public Boolean insertRecord(String record) throws InvalidProtocolBufferException {

//ProtoBuf组装

DCookRecordsProto.ProgramRecordValue.Builder value1 = DCookRecordsProto.ProgramRecordValue.newBuilder();

value1.setContentCode(“xxx”)

.setContentVersion(1)

.setDimension(“xx#xx”);

for (int i = 0; i < 2; i++) {

DCookRecordsProto.TempRecord.Builder tempRecord = DCookRecordsProto.TempRecord.newBuilder();

tempRecord.setTemp(100 + i + 1).setTimestamp(System.currentTimeMillis());

value1.addOvenTempList(tempRecord);

value1.addProbeTempList(tempRecord);

}

DCookRecordsProto.DCookRecord.Builder cookRecord = DCookRecordsProto.DCookRecord.newBuilder();

cookRecord.setRecordType(1)

.setRecordProducer(“d”)

.setRecordStartTime(System.currentTimeMillis())

.setRecordEndTime(System.currentTimeMillis())

.setRecordValue(Any.pack(value1.build()));

DCookRecordsProto.DCookRecords.Builder cookRecords = DCookRecordsProto.DCookRecords.newBuilder();

cookRecords.addRecords(cookRecord);

byte[] bytes = cookRecords.build().toByteArray();

//Base64 Encoded

String encoded = Base64.getEncoder().encodeToString(bytes);

System.out.println(“---------encoded----------”);

System.out.println(encoded);

//ProtoBuf反解析

final String did = LoginUtils.getDid();

List recordPoList = Lists.newArrayList();

byte[] decoded = Base64.getDecoder().decode(record);

DCookRecordsProto.DCookRecords records = DCookRecordsProto.DCookRecords.parseFrom(decoded);

if (!(ObjectUtils.isEmpty(records) || CollectionUtils.isEmpty(records.getRecordsList()))) {

for (DCookRecordsProto.DCookRecord record1 : records.getRecordsList()) {

RmCookRecordPo recordPo = new RmCookRecordPo();

recordPo.setDid(did);

if (record1.getRecordStartTime() > 0) {

Instant instant = Instant.ofEpochMilli(record1.getRecordStartTime());

recordPo.setRecordStartTime(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));

}

if (record1.getRecordEndTime() > 0) {

Instant instant = Instant.ofEpochMilli(record1.getRecordEndTime());

recordPo.setRecordEndTime(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));

}

recordPo.setRecordProducer(record1.getRecordProducer());

recordPo.setRecordType(record1.getRecordType());

if (record1.getRecordType() == 1 || record1.getRecordType() == 2) {

DCookRecordsProto.ProgramRecordValue recordValue = record1.getRecordValue()

.unpack(DCookRecordsProto.ProgramRecordValue.class);

ProgramRecordValue programRecordValue = new ProgramRecordValue();

programRecordValue.setContentCode(recordValue.getContentCode());

programRecordValue.setContentVersion(recordValue.getContentVersion());

programRecordValue.setDimension(recordValue.getDimension());

if (!CollectionUtils.isEmpty(recordValue.getOvenTempListList())) {

List tempRecordList = Lists.newArrayList();

for (DCookRecordsProto.TempRecord ovenTemp : recordValue.getOvenTempListList()) {

TempRecord tempRecord = new TempRecord();

tempRecord.setTemp(ovenTemp.getTemp());

tempRecord.setTimestamp(ovenTemp.getTimestamp());

tempRecordList.add(tempRecord);

}

programRecordValue.setOvenTempListList(tempRecordList);

}

if (!CollectionUtils.isEmpty(recordValue.getProbeTempListList())) {

List tempRecordList = Lists.newArrayList();

for (DCookRecordsProto.TempRecord protoTemp : recordValue.getProbeTempListList()) {

TempRecord tempRecord = new TempRecord();

tempRecord.setTemp(protoTemp.getTemp());

tempRecord.setTimestamp(protoTemp.getTimestamp());

tempRecordList.add(tempRecord);

}

programRecordValue.setProbeTempListList(tempRecordList);

}

recordPo.setRecordValue(JSONObject.toJSONString(programRecordValue));

} else if (record1.getRecordType() == 2) {

DCookRecordsProto.RecipeRecordValue recordValue = record1.getRecordValue()

.unpack(DCookRecordsProto.RecipeRecordValue.class);

RecipeRecordValue recipeRecordValue = new RecipeRecordValue();

recipeRecordValue.setRecipeId(recordValue.getRecipeId());

recipeRecordValue.setRecipeCode(recordValue.getRecipeCode());

recipeRecordValue.setDimension(recordValue.getDimension());

if (!CollectionUtils.isEmpty(recordValue.getOvenTempListList())) {

List tempRecordList = Lists.newArrayList();

for (DCookRecordsProto.TempRecord ovenTemp : recordValue.getOvenTempListList()) {

TempRecord tempRecord = new TempRecord();

tempRecord.setTemp(ovenTemp.getTemp());

tempRecord.setTimestamp(ovenTemp.getTimestamp());

tempRecordList.add(tempRecord);

}

recipeRecordValue.setOvenTempListList(tempRecordList);

}

if (!CollectionUtils.isEmpty(recordValue.getProbeTempListList())) {

List tempRecordList = Lists.newArrayList();

for (DCookRecordsProto.TempRecord protoTemp : recordValue.getProbeTempListList()) {

TempRecord tempRecord = new TempRecord();

tempRecord.setTemp(protoTemp.getTemp());

tempRecord.setTimestamp(protoTemp.getTimestamp());

tempRecordList.add(tempRecord);

}

recipeRecordValue.setProbeTempListList(tempRecordList);

}

recordPo.setRecordValue(JSONObject.toJSONString(recipeRecordValue));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值