数据库在查询数据的时候都免不了要用到过滤器,HBase作为一款开源分布式非关系型数据库也不例外,因为在大数据场景下不可能同时查询所有数据。使用过滤器可以让我们不用查询全部结果就可以快速找到我们想要的数据。本节就来介绍一下HBase中过滤器的Java API的使用。
注意:在Windows上使用Eclipse操作HBase时,HBase节点的主机名一定不能是localhost!!!
0.搭建本地开发环境
(0)将HBase的主机名加入本地Hosts
编辑C:\Windows\System32\drivers\etc\hosts文件,加入下面的内容:
192.168.126.110 bigdata
(1)下载HBase依赖的Jars
使用FTP工具如WinSCP将HBase安装目录下的lib目录下的所有Jar包下载至本地目录如E:/hbaselibs中。
(2)新建HBaseTest工程
打开Eclipse IDE,依次选择”File”->”New”->”Java Project”,工程名字填写HBaseTest,”Finsh”。
(3)给工程添加依赖包
在HBaseTest工程上右键单击,依次选择”New”->”Folder”,文件夹名字填写”hbaselibs”,”Finish”。将E:/hbaselibs目录中的所有Jar包复制、粘贴到工程下面的hbaselibs文件夹中。展开hbaselibs文件夹,选中所有Jar包,右键,依次选择”Build Path”->”Add to Build Path”即可
(4)创建Demo Package
在HBaseTest工程下面的src文件上右键,依次选择”New”->”Package”,Package名字填写”Demo”,”Finish”。我们下面的测试代码都在该Demo Package下编写。
(5)创建HBaseFilterTest.Java类
右键Demo,依次选择”New”->”Class”,类名填写”HBaseFilterTest”,”Finish”。
(6)导入以下案例用到的包
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
(7)执行下面的代码导入测试数据
public class DataInit {
@Test
public void CreateTable() throws Exception{
// 本地Hadoop环境,为了消除警告
System.setProperty("hadoop.home.dir", "E:\\hadoop-2.4.1\\hadoop-2.4.1");
// 配置信息
Configuration conf = HBaseConfiguration.create();
// Zookeeper的地址
conf.set("hbase.zookeeper.quorum", "192.168.126.110");
// 创建连接
Connection conn = ConnectionFactory.createConnection(conf);
// 创建客户端
Admin admin = conn.getAdmin();
if (admin.tableExists(TableName.valueOf("emp"))) {
System.out.println("table has exist!");
System.exit(0);
}
else {
// 指定表名
HTableDescriptor ht = new HTableDescriptor(TableName.valueOf("emp"));
// 指定列族名
HColumnDescriptor hc = new HColumnDescriptor("empinfo");
// 将列族加入到表中
ht.addFamily(hc);
// 创建表
admin.createTable(ht);
System.out.println("create table Success!");
}
// 关闭客户端
admin.close();
}
@Test
public void PutData() throws Exception{
//本地Hadoop环境,为了消除警告
System.setProperty("hadoop.home.dir", "E:\\hadoop-2.4.1\\hadoop-2.4.1");
// 配置信息
Configuration conf = HBaseConfiguration.create();
// Zookeeper的地址
conf.set("hbase.zookeeper.quorum", "192.168.126.110");
// 创建连接
Connection conn = ConnectionFactory.createConnection(conf);
// 创建客户端
Admin admin = conn.getAdmin();
if (!admin.tableExists(TableName.valueOf("emp"))) {
System.out.println("table does not exist!");
System.exit(0);
}
else
{
//客户端
Table table = conn.getTable(TableName.valueOf("emp"));
//第一条数据
Put put1 = new Put(Bytes.toBytes("7369"));
put1.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("ename"), Bytes.toBytes("SMITH"));
Put put2 = new Put(Bytes.toBytes("7369"));
put2.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("sal"), Bytes.toBytes("800"));
//第二条数据
Put put3 = new Put(Bytes.toBytes("7499"));
put3.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("ename"), Bytes.toBytes("ALLEN"));
Put put4 = new Put(Bytes.toBytes("7499"));
put4.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("sal"), Bytes.toBytes("1600"));
//第三条数据
Put put5 = new Put(Bytes.toBytes("7521"));
put5.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("ename"), Bytes.toBytes("WARD"));
Put put6 = new Put(Bytes.toBytes("7521"));
put6.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("sal"), Bytes.toBytes("1250"));
//第四条数据
Put put7 = new Put(Bytes.toBytes("7566"));
put7.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("ename"), Bytes.toBytes("JONES"));
Put put8 = new Put(Bytes.toBytes("7566"));
put8.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("sal"), Bytes.toBytes("2975"));
//第五条数据
Put put9 = new Put(Bytes.toBytes("7654"));
put9.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("ename"), Bytes.toBytes("MARTIN"));
Put put10 = new Put(Bytes.toBytes("7654"));
put10.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("sal"), Bytes.toBytes("1250"));
//第六条数据
Put put11 = new Put(Bytes.toBytes("7698"));
put11.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("ename"), Bytes.toBytes("BLAKE"));
Put put12 = new Put(Bytes.toBytes("7698"));
put12.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("sal"), Bytes.toBytes("2850"));
//第七条数据
Put put13 = new Put(Bytes.toBytes("7782"));
put13.addColumn(Bytes.toBytes("empinfo"), Bytes.toBytes("ename"), Bytes.toBytes("CLARK"));
Put put14 = new Put(Bytes.toBytes("7782"))