文章目录
HBase的Java代码开发
配置Maven工程pom.xml文件的依赖
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-mr1-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
<!--将我们其他用到的一些jar包全部都打包进来 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>false</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
(一)创建表
创建myuser表,两个列族,分别为f1,f2。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
//创建myuser表,两个列族,分别为f1,f2。
public class CrreateHBaseTable {
public static void main(String[] args) throws IOException {
Configuration configuration = HBaseConfiguration.create();
//连接hbase集群不需要指定hbase主节点的ip和端口
//只需指定各节点zooleeper的ip和端口,只指定一台zookeeper也可以
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
//创建连接池对象
Connection connection = ConnectionFactory.createConnection(configuration);
//对数据库进行DDL操作时,要获取管理员对象
Admin admin = connection.getAdmin();
//指定我们创建的表的名字: myuser
TableName myuser = TableName.valueOf("myuser");
HTableDescriptor hTableDescriptor = new HTableDescriptor(myuser);
//指定myuser表的列族: f1、f2
HColumnDescriptor f1 = new HColumnDescriptor("f1");
HColumnDescriptor f2 = new HColumnDescriptor("f2");
hTableDescriptor.addFamily(f1);
hTableDescriptor.addFamily(f2);
//创建表
admin.createTable(hTableDescriptor);
//释放资源
admin.close();
connection.close();
}
}
(二)删除表
删除myuser表。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
//删除myuser表
public class DeleteHBaseTable {
public static void main(String[] args) throws IOException {
Configuration configuration = HBaseConfiguration.create();
//连接hbase集群不需要指定hbase主节点的ip和端口
//只需指定各节点zooleeper的ip和端口,只指定一台zookeeper也可以
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
//创建连接池对象
Connection connection = ConnectionFactory.createConnection(configuration);
//对数据库进行DDL操作时,要获取管理员对象
Admin admin = connection.getAdmin();
TableName myuser = TableName.valueOf("myuser");
admin.disableTable(myuser);
admin.deleteTable(myuser);
//释放资源
admin.close();
connection.close();
}
}
(三)向表中添加数据
向myuser表中添加数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
//向myuser表中添加数据, rowkey="0001" f1:name = "zhangsan" f1:name = 18 f1:id = 25 f2:address = "地球人"
public class AddData {
public static void main(String[] args) throws IOException {
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("myuser"));
//创建put对象,并指定rowkey值
Put put = new Put("0001".getBytes());
put.addColumn("f1".getBytes(),"name".getBytes(),"zhangsan".getBytes());
put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes(18));
put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(25));
put.addColumn("f1".getBytes(),"address".getBytes(),Bytes.toBytes("地球人"));
//执行添加操作
table.put(put);
//释放资源
table.close();
connection.close();
}
}
(四)删除数据
删除数据rowkey为0001的数据记录
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
//删除数据rowkey为0001的数据记录
public class DeleteData {
public static void main(String[] args) throws IOException {
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("myuser"));
//删除数据
Delete delete = new Delete("0001".getBytes());
table.delete(delete);
table.close();
connection.close();
}
}
(五)查询数据
向myuser表中插入一批数据用作查询操作
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class InsertBatchData {
public static void main(String[] args) throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181");
Connection connection = ConnectionFactory.createConnection(configuration);
//获取表
Table myuser = connection.getTable(TableName.valueOf("myuser"));
//创建put对象,并指定rowkey
Put put = new Put("0002".getBytes());
put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(1));
put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));
put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));
Put put2 = new Put("0003".getBytes());
put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));
put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));
put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));
Put put3 = new Put("0004".getBytes());
put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));
put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));
Put put4 = new Put("0005".getBytes());
put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));
put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));
Put put5 = new Put("0006".getBytes());
put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));
put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));
Put put6 = new Put("0007".getBytes());
put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));
put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));
put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));
List<Put> listPut = new ArrayList<Put>();
listPut.add(put);
listPut.add(put2);
listPut.add(put3);
listPut.add(put4);
listPut.add(put5);
listPut.add(put6);
myuser.put(listPut);
myuser.close();
connection.close();
}
}
1.Get查询
查询rowkey为0003的f1列族所有数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//查询rowkey为0003的f1列族所有数据
public class GetByRowKey {
public static void main(String[] args) throws IOException {
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("myuser"));
//通过Get对象,指定rowkey,默认查询这个rowkey的所有数据
Get get = new Get(Bytes.toBytes("0003"));
//限制只查询f1列族下面所有列的值
get.addFamily("f1".getBytes());
//执行get查询,返回一个result对象,所有rowkey为0003的单元格的所有信息都会都封装在这个对象中
Result result = table.get(get);
//将每个单元格的所有信息封装在cell对象内,再放入集合中
List<Cell> cells = result.listCells();
//遍历cells, 看cell中封装了哪些信息
for (Cell cell : cells) {
byte[] value = CellUtil.cloneValue(cell);//获取单元格的值
byte[] rowkey = CellUtil.cloneRow(cell);//获取单元格对应的rowkey
byte[] columnName = CellUtil.cloneQualifier(cell);//获取单元格所属的列名
byte[] familyName = CellUtil.cloneFamily(cell);//获取单元格所属的列族名
System.out.println(Bytes.toString(familyName));
System.out.println(Bytes.toString(columnName));
System.out.println(Bytes.toString(rowkey));
//列名是Id和列名是age的列对应的value整数int类型,要判断一下转成int类型
if("id".equals(Bytes.toString(columnName)) || "age".equals(Bytes.toString(columnName))){
System.out.println(Bytes.toInt(value));
}else {
System.out.println(Bytes.toString(value));
}
}
table.close();
connection.close();
}
}
2.Scan查询
查询rowkey为[0003,0007)之间的数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//查询rowkey为[0003,0007)之间的数据
public class ScanRangeData {
public static void main(String[] args) throws IOException {
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("myuser"));
Scan scan = new Scan();//默认会全表扫描,所有下面要添加约束
//扫描f1列族
scan.addFamily("f1".getBytes());
//扫描f2列族的phone字段
scan.addColumn("f2".getBytes(),"phine".getBytes());
//设定扫描的起始行
scan.setStartRow("0003".getBytes());
scan.setStopRow("0007".getBytes());
//通过getScan查询获得表中符合要求的数据, 返回的是多条数据
ResultScanner scanner = table.getScanner(scan);
//遍历ResultScanner 得到每一条数据,每一条数据都是封装在result对象里面了
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] familyName = CellUtil.cloneFamily(cell);
byte[] qualifierName = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] valueArr = CellUtil.cloneValue(cell);
if("id".equals(Bytes.toString(qualifierName)) || "age".equals(Bytes.toString(qualifierName))){
int value = Bytes.toInt(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}else {
String value = Bytes.toString(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}
}
}
table.close();
connection.close();
}
}
3.RowFilter行键过滤器
查询所有的rowkey比0003小的所有的数据
package cn.com.vivo.day01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//查询所有的rowkey比0003小的所有的数据
public class RowFilterQuery {
public static void main(String[] args) throws IOException {
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("myuser"));
Scan scan = new Scan();
/* RowFilter需要加上两个参数
第一个参数就是我们的比较规则
第二个参数就是我们的比较对象 */
BinaryComparator binaryComparator = new BinaryComparator("0003".getBytes());//获取比较对象
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, binaryComparator);
//为scan对象设置过滤器
scan.setFilter(rowFilter);
//执行查询
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] familyName = CellUtil.cloneFamily(cell);
byte[] qualifierName = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] valueArr = CellUtil.cloneValue(cell);
if("id".equals(Bytes.toString(qualifierName)) || "age".equals(Bytes.toString(qualifierName))){
int value = Bytes.toInt(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}else {
String value = Bytes.toString(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}
}
}
table.close();
connection.close();
}
}
4.FamilyFilter列族过滤器
查询列族名包含2的所有数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//查询列族名包含2的所有数据
public class FamilyFilterQuery {
public static void main(String[] args) throws IOException {
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("myuser"));
Scan scan = new Scan();
// familyFilter来实现列族的过滤
SubstringComparator substringComparator = new SubstringComparator("2");
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, substringComparator);
scan.setFilter(familyFilter);
//执行查询
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] familyName = CellUtil.cloneFamily(cell);
byte[] qualifierName = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] value = CellUtil.cloneValue(cell);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+Bytes.toString(value));
}
}
table.close();
connection.close();
}
}
5.QualifierFilter列过滤器
查询列名包含name的所有数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//查询列名包含name的所有数据
public class QualifierFilterQuery {
public static void main(String[] args) throws IOException {
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("myuser"));
Scan scan = new Scan();
//QualifierFilter来实现列值过滤
SubstringComparator substringComparator = new SubstringComparator("name");
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, substringComparator);
scan.setFilter(qualifierFilter);
//执行查询
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] familyName = CellUtil.cloneFamily(cell);
byte[] qualifierName = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] value = CellUtil.cloneValue(cell);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+Bytes.toString(value));
}
}
table.close();
connection.close();
}
}
6.ValueFilter值过滤器
查询所有数据中包含字符串"8"的数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//查询所有数据中包含8的数据
public class ValueFilterQuery {
public static void main(String[] args) throws IOException {
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("myuser"));
Scan scan = new Scan();
//ValueFilter来实现列值过滤
SubstringComparator substringComparator = new SubstringComparator("8");
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, substringComparator);
scan.setFilter(valueFilter);
//执行查询
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] familyName = CellUtil.cloneFamily(cell);
byte[] qualifierName = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] value = CellUtil.cloneValue(cell);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+Bytes.toString(value));
}
}
table.close();
connection.close();
}
}
7.SingleColumnValueFilter单列值过滤器
查询f1 列族、name列、值为刘备的数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//查询f1 列族、name列、值为刘备的数据
//类似与 select * from myuser where name = '刘备'
public class SingleColumnValueFilterQuery {
public static void main(String[] args) throws IOException {
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("myuser"));
Scan scan = new Scan();
//SingleColumnValueFilter来实现单列值过滤
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
"f1".getBytes(),
"name".getBytes(),
CompareFilter.CompareOp.EQUAL,
"刘备".getBytes()
);
scan.setFilter(singleColumnValueFilter);
//执行查询
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] familyName = CellUtil.cloneFamily(cell);
byte[] qualifierName = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] valueArr = CellUtil.cloneValue(cell);
if("id".equals(Bytes.toString(qualifierName)) || "age".equals(Bytes.toString(qualifierName))){
int value = Bytes.toInt(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}else {
String value = Bytes.toString(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}
}
}
table.close();
connection.close();
}
}
扩展: SingleColumnValueExcludeFilter 列值排除过滤器,可实现与列值过滤器相反的功能。类似于下面SQL:
select * from myuser where name != ‘刘备’
8.PrefixFilter前缀过滤器
查询rowkey前缀以 00开头的所有的数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//查询rowkey前缀以 00开头的所有的数据
public class PrefixFilterQuery {
public static void main(String[] args) throws IOException {
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("myuser"));
Scan scan = new Scan();
//PrefixFilter来实现前缀过滤
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
scan.setFilter(prefixFilter);
//执行查询
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] familyName = CellUtil.cloneFamily(cell);
byte[] qualifierName = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] valueArr = CellUtil.cloneValue(cell);
if("id".equals(Bytes.toString(qualifierName)) || "age".equals(Bytes.toString(qualifierName))){
int value = Bytes.toInt(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}else {
String value = Bytes.toString(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}
}
}
table.close();
connection.close();
}
}
9.PageFilter分页过滤器
每页显示2个rowkey的数据,查询第3页的数据。
package cn.com.vivo.day01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.List;
//每页显示2个rowkey的数据,查询第3页的数据。
public class PageFilterQuery {
public static void main(String[] args) throws IOException {
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("myuser"));
Scan scan = new Scan();
int pageNum = 3;//从第三页开始查询
int pageSize = 2;//每页显示2条rowkey的所有数据
//分页过滤器需要我们提供从页面起始的rowkey, 我们要根据查询的页码来寻找起始的rowkey
if(pageNum == 1){//如果查询的是第一页,可以使用""来表示第一个rowkey
//设置一次向后扫描多少条数据,即设置pageSize
scan.setMaxResultSize(pageSize);
scan.setStartRow("".getBytes());//表示从第一个rowkey开始查询
}else {
//我们要想办法获取第三页的起始rowkey是什么
String startRow = "";
int tempPageSize = (pageNum - 1) * pageSize +1;//注意加1后的到的才是第3页的起始rowkey
scan.setMaxResultSize(tempPageSize);
PageFilter pageFilter = new PageFilter(tempPageSize);
scan.setFilter(pageFilter);
ResultScanner scanner = table.getScanner(scan);
//遍历的最后一条数据中有我们要找的第三页的startRow
for (Result result : scanner) {
List<Cell> cells = result.listCells();
int startRowIndex = cells.size() - 1;
byte[] startRowArr = CellUtil.cloneRow(cells.get(startRowIndex));
startRow = new String(startRowArr);
}
scan.setMaxResultSize(pageSize);
scan.setStartRow(startRow.getBytes());
}
PageFilter filter = new PageFilter(pageSize);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] familyName = CellUtil.cloneFamily(cell);
byte[] qualifierName = CellUtil.cloneQualifier(cell);
byte[] rowkey = CellUtil.cloneRow(cell);
byte[] valueArr = CellUtil.cloneValue(cell);
if("id".equals(Bytes.toString(qualifierName)) || "age".equals(Bytes.toString(qualifierName))){
int value = Bytes.toInt(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}else {
String value = Bytes.toString(valueArr);
System.out.println(Bytes.toString(familyName)+"\t"
+Bytes.toString(qualifierName)+"\t"
+Bytes.toString(rowkey)+"\t"
+value);
}
}
}
}
}