HBase实战:Java API过滤器应用详解

数据库在查询数据的时候都免不了要用到过滤器,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"))
        
    
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空