- 1、依赖(thrift,非zk)
- 2、连接
- 3、查询
- 4、数据筛选
- 4.1 根据时间戳筛选
- 4.2 根据行键筛选
- 5、字段绑定
参考:https://blog.csdn.net/chinabestchina/article/details/105720328
org.apache.hbase hbase-client 2.0.0-beta-1 org.apache.hbase hbase-common 2.0.0-beta-1 org.apache.hbase hbase-thrift 1.0.0
PS. 测试了hbase-thrift 2.0.0版本,会报错,所以暂时先用着1.0.0的
2、连接 private static void connect() throws TTransportException {
// 创建连接 需要的参数 ip,端口,超时时间(可选)
transport = new TSocket(host, port, timeout);
TProtocol protocol = new TBinaryProtocol(transport);
client = new THBaseService.Client(protocol);
logger.info(host + port +timeout);
transport.open();
}
3、查询
public static void scan(THBaseService.Iface client,String table) {
try {
//批量查询
TScan scan = new TScan();
List resultList = client.getScannerResults(
ByteBuffer.wrap(table.getBytes(StandardCharsets.UTF_8)), scan, 100000
);
resultList.forEach(r -> {
r.getColumnValues().forEach(c -> {
// getRow 行键(r)
// getFamily 列簇(c)
// getQualifier 列名(c)
// getValue 值(c)
});
});
} catch (Exception e) {
e.printStackTrace();
}
}
4、数据筛选
4.1 根据时间戳筛选
public static void scan(THBaseService.Iface client,String table) {
try {
long gap = 24 * 60 * 60 * 1000;
long now = new Date().getTime();
//批量查询
TScan scan = new TScan();
// 设置时间戳 一天间隔
long startTime = now - gap;
long endTime = now;
scan.setTimeRange(new TTimeRange(startTime, endTime));
List resultList = client.getScannerResults(
ByteBuffer.wrap(table.getBytes(StandardCharsets.UTF_8)), scan, 100000
);
// resultList.forEach(...);
} catch (Exception e) {
e.printStackTrace();
}
}
4.2 根据行键筛选
行键规则:字段phone + 下划线 + new Date().getTime() (反转) + 下划线 + 随机字符
public static void scan(THBaseService.Iface client,String table) {
try {
String phone = "123456";
long gap = 24 * 60 * 60 * 1000;
long now = new Date().getTime();
//批量查询
TScan scan = new TScan();
// 一天间隔
String startTime = phone + "_" + String.valueOf(now - gap);
String endTime = phone + "_" + String.valueOf(now);
// 设置行键
scan.setStartRow(Bytes.toBytes(reverse(startTime));
scan.setStopRow(Bytes.toBytes(reverse(endTime)));
List resultList = client.getScannerResults(
ByteBuffer.wrap(table.getBytes(StandardCharsets.UTF_8)), scan, 100000
);
// resultList.forEach(...);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String reverse(String str) {
return new StringBuffer(str).reverse().toString();
}
5、字段绑定
由于当前hbase上只设置了一个列簇,一列,所以需要解析数据绑定字段
public static Map setFields(String[] fieldsArray, String line) {
Map map = new HashMap<>();
try {
// StringUtils.split 分割会不完全
String[] l = line.split("\|");
for (int i = 0; i < l.length; i++) {
// 转码(避免中文乱码)
byte[] bytes = l[i].getBytes(StandardCharsets.UTF_8);
map.put(fieldsArray[i], new String(bytes));
}
} catch (Exception e) {
System.out.println("setFields error" + e.message);
e.printStackTrace();
}
return map;
}



