本篇博客,小菌为大家带来关于使用HBase的JavaAPI的一些常用练习。
使用hbaseapi 对hbase_rate表,按照startRowKey=1和endRowKey=100进行扫描查询结果。
代码语言:javascript复制@Test
public void scanData() throws IOException {
// 获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表
Table rate_table = connection.getTable(TableName.valueOf("hbase_rate"));
// 创建扫描对象
Scan scan = new Scan();
// 设置startkey 和 endkey 属性
scan.setStartRow("1".getBytes());
scan.setStopRow("100".getBytes());
ResultScanner scanner = rate_table.getScanner(scan);
for (Result result : scanner) {
// 获取到rowkey
System.out.println(Bytes.toString(result.getRow()));
// 遍历获取得到的所有列族以及所有的列名称
KeyValue[] raw = result.raw();
String column_family = "";
String column_name = "";
StringBuffer stringBuffer = new StringBuffer();
for (KeyValue keyValue : raw) {
// 获取到列族
column_family = Bytes.toString(keyValue.getFamilyArray(),keyValue.getFamilyOffset(),keyValue.getFamilyLength());
// 获取到列名
column_name = Bytes.toString(keyValue.getQualifierArray(),keyValue.getQualifierOffset(),keyValue.getQualifierLength());
stringBuffer.append(Bytes.toString(result.getValue(column_family.getBytes(), column_name.getBytes()))).append("t");
}
System.out.println(stringBuffer.toString());
System.out.println("-------------------------------------------");
}
rate_table.close();
}
使用hbaseapi对hbase_comments表,只查询comments列的值。
代码语言:javascript复制@Test
public void findColumn() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
// 获取到表
Table comments_table = connection.getTable(TableName.valueOf("hbase_comments"));
// 全表扫描
Scan scan = new Scan();
ResultScanner resultScanner = comments_table.getScanner(scan);
for (Result result : resultScanner) {
//获取rowkey
// System.out.println(Bytes.toString(result.getRow()));
String column_family = "";
String column_name = "";
KeyValue[] raw = result.raw();
Cell[] cells = result.rawCells();
for (KeyValue keyValue : raw) {
// 获取到列族
column_family = Bytes.toString(keyValue.getFamilyArray(),keyValue.getFamilyOffset(),keyValue.getFamilyLength());
// 获取到列名
column_name = Bytes.toString(keyValue.getQualifierArray(),keyValue.getQualifierOffset(),keyValue.getQualifierLength());
// System.out.println("列名:" column_name);
if (" comments".equals(column_name)){
// 如果列名等于 comments,那么就把数据打印出来
System.out.println("comments_value:" Bytes.toString(result.getValue(column_family.getBytes(), column_name.getBytes())));
}
}
}
}
使用 hbaseapi 对hbase_user表,查询包含5的列值
代码语言:javascript复制@Test
public void test01() throws Exception {
// 获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表
Table table = connection.getTable(TableName.valueOf("hbase_user"));
//全表扫描
Scan scan = new Scan();
//创建一个过滤器,并设置给scan
//SubstringComparator 是包含,BinaryComparator 是完全匹配
ValueFilter valueFilter = new ValueFilter(EQUAL, new SubstringComparator("5"));
scan.setFilter(valueFilter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
// 遍历每一行的cell
for (Cell cell : cells) {
// 先获取到列名
String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
// 获取到值
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("列名:" qualifier "...值:" value);
}
}
}
使用hbaseapi对hbase_video表,只查询age列,并且大于700的值(使用列过滤器和列值过滤器)
代码语言:javascript复制@Test
public void test02() throws Exception{
// 获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表
Table table = connection.getTable(TableName.valueOf("hbase_video"));
//全表扫描
Scan scan = new Scan();
// 当添加多个过滤器的时候,就需要创建一个集合
FilterList filterList = new FilterList();
//设置列过滤器和列值过滤器
QualifierFilter qualifierFilter = new QualifierFilter(EQUAL, new SubstringComparator("age"));
ValueFilter valueFilter = new ValueFilter(GREATER, new BinaryComparator("700".getBytes()));
filterList.addFilter(qualifierFilter);
filterList.addFilter(valueFilter);
//把过滤器集合设置给scan
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
// 获取到 rowkey
//System.out.println(Bytes.toString(result.getRow()));
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
// 获取到列
String qualifier = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
// 获取到值
String value = Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength());
// String string = Bytes.toString(CellUtil.cloneQualifier(cell));
System.out.println(qualifier "--" value "--");
}
}
}
本次的分享就到这里,关于HBase过滤器更多的使用可以参考这篇博客哟《HBase的JavaAPI使用–进阶篇–过滤器的使用》。受益的小伙伴们记得关注小菌哟~点赞加关注,小白不迷路ヾ(=・ω・=)o