大数据全系列 教程
1869个小节阅读:468k
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
maven项目中使用
修改pom.xml,添加依赖
xxxxxxxxxx
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.19.1</version>
</dependency>
将com拷贝到src目录下。
拷贝PhoneLogDemo的insert()方法为insertProtocBuf()
xxxxxxxxxx
/**生成10个用户的在某一年内的通话记录,每个用户产生1000条通话记录
* dnum:对方手机号码 type:呼叫类型 0主叫 1表示被叫 length:通话时长 date:时间
*/
public void insertProtocBuf() throws Exception {
//1.定义一个List<Put>
List<Put> putList = new ArrayList<Put>();
//2.循环10次,模拟10个用户
for(int i = 0;i<10;i++){
//3.清空putList,防止上次操作的影响
putList.clear();
//4.生成当前用户的手机号码
String phoneNumber = getPhoneNumber("158");
System.out.println(phoneNumber);
//5.模拟每个用户的1000条数据
for(int j=0;j<1000;j++){
//cf:length=,cf:dnum=,cf:date=,cf:type= 0表示主叫 1表示被叫
//6生成每一行通话记录的数据
String dnum = getPhoneNumber("199");
int length = random.nextInt(200)+1;
int type = random.nextInt(2);
String date = getDate(2050);
//7.rowkey设计
String rowkey = phoneNumber+"_"
+(Long.MAX_VALUE-simpleDateFormat.parse(date).getTime())+i+j;
//8.创建put对象
Put put = new Put(Bytes.toBytes(rowkey));
MyPhone.Phone.Builder builder = MyPhone.Phone.newBuilder();
builder.setDnum(dnum);
builder.setLength(length);
builder.setType(type);
builder.setDate(date);
MyPhone.Phone phone = builder.build();
//由原来的四个字段存储通话记录,改为1个字段存储
put.addColumn(Bytes.toBytes("basic"),Bytes.toBytes("info"),phone.toByteArray());
//9将put对象添加putList 勿忘我
putList.add(put);
}
//10.将当前用户的1000条通话记录提交
table.put(putList);
}
}
执行insertProtocBuf()方法
查看文件大小,对比网页查看
生成的内容查看,格式的变化
xxxxxxxxxx
[root@node1 ~]# hbase hfile -p -f /hbase_ha/data/itbaizhan/phone_log/dd9e5d2a67a2c91345847b16ae9cae2b/basic/7103dda2958644bb9977e695724e95e5
K:15891940106_92233695121822728073731/basic:info/1639050680296/Put/vlen=38/seqid=7 V: \x08j\x12\x0B19982728623\x18\x01"\x132050-01-02 01:55:03
K:15891940106_92233695121822728073751/basic:info/1639050680296/Put/vlen=39/seqid=7 V: \x08\xB3\x01\x12\x0B19937463281\x18\x01"\x132050-01-02 01:55:03
Scanned kv count -> 10000
拷贝scan()方法为scanProtocBuf()方法
xxxxxxxxxx
//查询某用户3月份的通话记录
public void scanProtocBuf() throws Exception {
//1.定义某用户的手机号码
String phoneNumber = "15888551642";
//2.定义startRow 包含
String startRow = phoneNumber+"_"+(Long.MAX_VALUE-
simpleDateFormat.parse("2050-04-01 00:00:00").getTime());
//3.定义stopRow 包含
String stopRow = phoneNumber+"_"+(Long.MAX_VALUE-
simpleDateFormat.parse("2050-03-01 00:00:00").getTime());
//4.创建Scan对象
Scan scan = new Scan();
//5.设置起始和结束行
scan.withStartRow(Bytes.toBytes(startRow));
scan.withStopRow(Bytes.toBytes(stopRow),true);
//6.执行查询
ResultScanner resultScanner = table.getScanner(scan);
//7.解析resultScanner
for(Result result:resultScanner){
//8.解析result
Cell[] cells = result.rawCells();
//String rowInfo = "rowkey:"+Bytes.toString(CellUtil.cloneRow(cells[0]));
//获取值
byte[] phoneInfoBytes = CellUtil.cloneValue(cells[0]);
//将字节数据中的数据反序列化为MyPhone.Phone对象
MyPhone.Phone phone = MyPhone.Phone.parseFrom(phoneInfoBytes);
System.out.print(phone.getDnum()+"--");
System.out.print(phone.getType()+"--");
System.out.print(phone.getLength()+"--");
System.out.println(phone.getDate()+"--");
}
}