1、行过滤,打印显示行号小于等于row010的所有行数据
public void scanData1() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化行过滤器,使用二进制比较器,小于等于row10
RowFilter rf = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row010")));
scan.setFilter(rf);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |
2、获得行id中含有9的所有行,子串比较
public void scanData2() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化行过滤器,使用字串比较,行id中含有9的所有行
RowFilter rf = new RowFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("9"));
scan.setFilter(rf);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |
3、获得行id中含有9的所有行,正则比较
public void scanData3() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化行过滤器,使用正则比较,行id中含有9的所有行
RowFilter rf = new RowFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator(".+9"));
scan.setFilter(rf);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |
4、获得f1列族数据,列族过滤,正则比较
public void scanData4() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化列族过滤器,获得f1列族数据,列族过滤,正则比较
FamilyFilter ff = new FamilyFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("f1"));
scan.setFilter(ff);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |
5、获得所有name数据,列限定符过滤,正则比较
public void scanData5() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化列限定符过滤器,获得所有name数据,列限定符过滤,正则比较
QualifierFilter qf = new QualifierFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("name"));
scan.setFilter(qf);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |
6、行过滤,打印显示行号小于等于row010且大于等于row005的所有行数据
public void scanData6() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化行过滤器,使用二进制比较器,小于等于row010
RowFilter rf1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row010")));
//初始化行过滤器,使用二进制比较器,大于等于row005
RowFilter rf2 = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row005")));
//新建过滤器集合
FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL, rf1, rf2);
scan.setFilter(fl);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |
7、行过滤,打印显示行号小于等于row005或大于等于row095的所有行数据
public void scanData7() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化行过滤器,使用二进制比较器,小于等于row005
RowFilter rf1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row005")));
//初始化行过滤器,使用二进制比较器,大于等于row095
RowFilter rf2 = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row095")));
//新建过滤器集合
FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ONE, rf1, rf2);
scan.setFilter(fl);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |
8、值过滤,获取name小于ma010的该行的name字段的信息
public void scanData8() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化值过滤器,获取name小于ma010的所有信息
ValueFilter vf = new ValueFilter(CompareFilter.CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("ma010")));
//列限定符过滤器
QualifierFilter qf = new QualifierFilter(CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("name")));
FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL, vf, qf);
scan.setFilter(fl);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |
9、单列值过滤,获取name小于ma010的该行的所有信息
public void scanData9() throws Exception{
//初始化HBase的conf
Configuration conf = HBaseConfiguration.create();
//通过连接工厂创建连接
Connection conn = ConnectionFactory.createConnection(conf);
//通过连接获得表对象
Table table = conn.getTable(TableName.valueOf("test:t1"));
//使用scan方法从表中得到数据
Scan scan = new Scan();
//初始化单列值过滤器,获取name小于ma010的所有信息
SingleColumnValueFilter scvf = new SingleColumnValueFilter(Bytes.toBytes("f1"),
Bytes.toBytes("name"),
CompareFilter.CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("ma010")));
//初始化列族过滤器
FamilyFilter ff = new FamilyFilter(CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("f1")));
FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL, scvf, ff);
scan.setFilter(fl);
ResultScanner rs = table.getScanner(scan);
//打印显示结果行的所有细胞
for (Result r : rs) {
List cells = r.listCells();
for (Cell cell : cells) {
String row = Bytes.toString(CellUtil.cloneRow(cell));
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String cq = Bytes.toString(CellUtil.cloneQualifier(cell));
String val = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(row + "/" + cf + "/" + cq + "/" + val);
}
}
//关闭资源
table.close();
conn.close();
} |