1、依赖(thrift,非zk)
参考:https://blog.csdn.net/chinabestchina/article/details/105720328
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.0-beta-1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>2.0.0-beta-1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-thrift</artifactId>
<version>1.0.0</version>
</dependency>
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<TResult> 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<TResult> 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<TResult> 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上只设置了一个列簇,一列,所以需要解析数据绑定字段
/**
* 分割hbase字符串
* @Param fieldsArray 字段数组
* @Param line 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;
}