Protocol Buffer:
是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化
Protocol Buffer安装:
yum grouplist 检查yum 组--安装包在yum里是分组的
yum groupinfo Development tools 查看Development tools组信息
yum groupinstall Development tools 成组安装安装包(也就是安装
Protocol Buffer的前置包)
编译安装
上传
Protocol Buffer包到服务器
1、解压 tar -zxvf protobuf-2.5.0.tar.gz
2、./configure --prefix=/opt/soft/protobuf
3、编译 &&安装 make && make install
使用:
Protocol Buffer的代码都以.proto结尾
phone.proto:
package com.sxt.hbase; //包要和自己代码保持一致
message phone
{
required string phoneNum = 1;
required string time = 2;
required string dpNum = 3;
required int32 type = 4;
}
message datePhoneList {
repeated phone datePhone = 1;
}
编译成Java代码:./代表生成到根目录下
/opt/soft/protobuf/bin/protoc --java_out=./ daymsg.proto
然后把生成的daymsg.java复制到Java中就可以当工具类用了
/**
* 十个手机号码 每个用户每天共 产生100条
*
* @throws Exception
* @throws
*/
@Test
public void insertDBs2() throws Exception {
List<Put> puts = new ArrayList<Put>();
for (int i = 0; i < 10; i++) {
// 自己手机号码
String pNum = getPhone("186");
Long dataLong = sdf.parse("20170323000000").getTime();
// 一天的所有通话记录
Daymsg.daymsg.Builder dayMsg = Daymsg.daymsg.newBuilder();
// rowkey设计:大数-当前时间戳
String rowkey = pNum + "_" + (Long.MAX_VALUE-dataLong);
for (int j = 0; j < 100; j++) {
String dateStr = getDate2("20170323");
// 对方手机号码
String pNum2 = getPhone("170");
// 一条通话记录
Daymsg.phonemsg.Builder phoneMsg = Daymsg.phonemsg.newBuilder();
phoneMsg.setDate(dateStr);
phoneMsg.setPhonenum(pNum2);
phoneMsg.setType((r.nextInt(2)+""));
dayMsg.addMsgs(phoneMsg);
}
Put put = new Put(rowkey.getBytes());
put.add("cf".getBytes(), "daymsg".getBytes(), dayMsg.build().toByteArray());
puts.add(put);
}
htable.put(puts);
}
/**
* get方式 读取一个cell cf:daymsg
* 186982144790_9223370546656375807
*/
@Test
public void getDB2() throws Exception {
// 参数:rowkey
Get get = new Get("186982144790_9223370546656375807".getBytes());
get.addColumn("cf".getBytes(), "daymsg".getBytes());
Result rs = htable.get(get);
Cell cell = rs.getColumnLatestCell("cf".getBytes(), "daymsg".getBytes());
Daymsg.daymsg dayMsg = Daymsg.daymsg.parseFrom(CellUtil.cloneValue(cell));
for(Daymsg.phonemsg pMsg : dayMsg.getMsgsList()) {
System.out.println(pMsg.getDate() + " - " + pMsg.getPhonenum() + " - " +
pMsg.getType());
}
}
|