package cn.itcast_01_hbase;

import java.util.ArrayList;

import java.util.List;

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.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.MasterNotRunningException;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.apache.hadoop.hbase.client.Delete;

import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HBaseAdmin;

import org.apache.hadoop.hbase.client.HConnection;

import org.apache.hadoop.hbase.client.HConnectionManager;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.client.ResultScanner;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.client.Table;

import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;

import org.apache.hadoop.hbase.filter.CompareFilter;

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.After;

import org.junit.Before;

import org.junit.Test;

public class HbaseTest_2 {

    /**

     * 配置ss

     */

    static Configuration config = null;

    private Connection connection = null;

    private Table table = null;

    @Before

    public void init() throws Exception {

        config = HBaseConfiguration.create();// 配置

        //这里没有配置好像用的是path里面的hbase,这个程序是在linunx中

        //运行的,所以 没有问题,下面的配置是集群中的,但是由于集群没有搭建起来

        //用的是单机的

        //config.set("hbase.zookeeper.quorum", "master,work1,work2");// zookeeper地址

        //config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口

        /*connection = ConnectionFactory.createConnection(config);

        table = connection.getTable(TableName.valueOf("www.baidu.com20170630"));*/

        connection = ConnectionFactory.createConnection(config);

        table = connection.getTable(TableName.valueOf("t4"));

    }

    /**

     * 创建一个表

     * 

     * @throws Exception

     */

    @Test

    public void createTable() throws Exception { 

        //创建表管理

        HBaseAdmin admin = new HBaseAdmin(config);

        TableName tableName = TableName.valueOf("t4");

        HTableDescriptor desc = new HTableDescriptor(tableName);

        HColumnDescriptor family = new HColumnDescriptor("info");//

        desc.addFamily(family);

        HColumnDescriptor family2 = new HColumnDescriptor("ino2");

        desc.addFamily(family2);

        admin.createTable(desc);

    }

    @Test

    @SuppressWarnings("deprecation")

    public void deleteTable() throws MasterNotRunningException,

            ZooKeeperConnectionException, Exception {

        HBaseAdmin admin = new HBaseAdmin(config);

        admin.disableTable("t3");

        admin.deleteTable("t3");

        admin.close();

    }

    /**

     * 向hbase中增加数据

     * 

     * @throws Exception

     */

    @SuppressWarnings({ "deprecation", "resource" })

    @Test

    public void insertData() throws Exception { 

    List<Put> arraylist = new ArrayList<Put>(1);

    Put put =  new Put(Bytes.toBytes("1223434"));//rowkey

    put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("huangxiaoming"));

    put.add(Bytes.toBytes("info"),Bytes.toBytes("password"),Bytes.toBytes("anglebaby"));

    put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("25"));

    put.add(Bytes.toBytes("info"),Bytes.toBytes("gender"),Bytes.toBytes("1"));

    arraylist.add(put);

    //提交

    table.put(arraylist);

    //插入数据

    table.flushCommits();

    }

    /**

     * 修改数据,不存在时新增

     * 

     * @throws Exception

     */

    @Test

    public void uodateData() throws Exception {

        Put put = new Put(Bytes.toBytes("323422"));

        put.add(Bytes.toBytes("info"), Bytes.toBytes("name_B"), Bytes.toBytes("郭德纲"));

        put.add(Bytes.toBytes("info"), Bytes.toBytes("password_A"), Bytes.toBytes(1234));

        Put put1 = new Put(Bytes.toBytes("323432"));

        put1.add(Bytes.toBytes("info"), Bytes.toBytes("name_B"), Bytes.toBytes("沙河上"));

        put1.add(Bytes.toBytes("info"), Bytes.toBytes("password_B"), Bytes.toBytes(1234));

        Put put2 = new Put(Bytes.toBytes("323442"));

        put2.add(Bytes.toBytes("info"), Bytes.toBytes("name_B"), Bytes.toBytes("猪八戒"));

        put2.add(Bytes.toBytes("info"), Bytes.toBytes("password_B"), Bytes.toBytes(1234));

        //插入数据

        table.put(put);

        table.put(put1);

        table.put(put2);

        //提交

        table.flushCommits();

    }

    /**

     * 删除数据

     * 

     * @throws Exception

     */

    @Test

    public void deleteDate() throws Exception {

        Delete delete =  new Delete(Bytes.toBytes("1234"));

        table.delete(delete);

        table.flushCommits();

    }

    /**

     * 单条查询

     * 

     * @throws Exception

     */

    @Test

    public void queryData() throws Exception {

        Get get =new Get(Bytes.toBytes("1234"));

        //get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"));//根据列族查询列名字查询

        get.addFamily(Bytes.toBytes("info"));//根据列族查询

        Result result = table.get(get);

        long start = System.currentTimeMillis();

        System.out.println(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password"))));

        System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"))));

        long end =  System.currentTimeMillis();

        System.out.println(end-start);

    }

    /**

     * 全表扫描

     * 

     * @throws Exception

     */

    @Test

    public void scanData() throws Exception { 

        Scan scan = new Scan();

        scan.setStartRow(Bytes.toBytes("1"));

        scan.setStopRow(Bytes.toBytes("z"));//

        //scan.addFamily(Bytes.toBytes("info"));

        scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));

        ResultScanner scanner = table.getScanner(scan);

        for(Result result : scanner){

            System.out.print(Bytes.toString(result.getRow())+";");//rowkey

            System.out.print(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))+";");

            System.out.print(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password")))+";");

            System.out.println();

        }

    }

    private void printScanner(ResultScanner scanner ){

        for(Result result : scanner){

            System.out.print(Bytes.toString(result.getRow())+";");//rowkey

            System.out.print(Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")))+";");

            System.out.print(Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password")))+";");

            System.out.println();

        }        

    }

    /**

     * 全表扫描的过滤器

     * 列值过滤器

     * 

     * @throws Exception

     */

    @Test

    public void scanDataByFilter1() throws Exception { 

        Scan scan = new Scan();

    SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"), CompareOp.GREATER, Bytes.toBytes("lisi1234"));    

    scan.setFilter(filter);

    ResultScanner scanner = table.getScanner(scan);

    printScanner(scanner);

    }

    /**

     * rowkey过滤器

     * @throws Exception

     */

    @Test

    public void scanDataByFilter2() throws Exception {

    Scan scan = new Scan();

    RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^2"));

    scan.setFilter(rowFilter);

    ResultScanner scanner = table.getScanner(scan);

    printScanner(scanner);

    }

    /**

     * 匹配列名前缀

     * @throws Exception

     */

    @Test

    public void scanDataByFilter3() throws Exception { 

        Scan scan = new Scan();

        ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes("n"));

        //scan.addFamily(Bytes.toBytes("info"));

        scan.setFilter(columnPrefixFilter);

        ResultScanner scanner = table.getScanner(scan);

        printScanner(scanner);    

    }

    @Test

    public void scanDataByFilter44() throws Exception {

        //MultipleColumnPrefixFilter 和 ColumnPrefixFilter 行为差不多,但可以指定多个前缀

        Scan scan = new Scan();

        byte[][] prefixes = new byte[][] {Bytes.toBytes("na"),Bytes.toBytes("p")};

        //Filter f = new MultipleColumnPrefixFilter(prefixes);

        MultipleColumnPrefixFilter filter = new MultipleColumnPrefixFilter(prefixes);

        scan.setFilter(filter);

        ResultScanner scanner = table.getScanner(scan);

        printScanner(scanner);

    }

    /**

     * 过滤器集合

     * @throws Exception

     */

    @Test

    public void scanDataByFilter4() throws Exception {

        // 创建全表扫描的scan

        Scan scan = new Scan();

        FilterList filters = new FilterList(Operator.MUST_PASS_ALL);

        RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("^2"));

        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"), CompareOp.NOT_EQUAL, Bytes.toBytes("lisi"));

    filters.addFilter(rowFilter);

    filters.addFilter(singleColumnValueFilter);

    scan.setFilter(filters);

    ResultScanner scanner = table.getScanner(scan);

    printScanner(scanner);

    }

    @After

    public void close() throws Exception {

        table.close();

        connection.close();

    }

}

数据库表

 scan 't4'
ROW COLUMN+CELL
column=info:age, timestamp=, value=
column=info:gender, timestamp=, value=
column=info:name, timestamp=, value=lisi1234
column=info:password, timestamp=, value=\x00\x12\xD5.
column=info:name, timestamp=, value=lisi1234
column=info:password, timestamp=, value=\x00\x12\xD5.
column=info:name, timestamp=, value=sudan
column=info:password, timestamp=, value=\x00\x00\x00\x17
column=info:name, timestamp=, value=hanmei
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=sunwukong
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
column=info:name_A, timestamp=, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
column=info:name_B, timestamp=, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:password_A, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
column=info:name_A, timestamp=, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
column=info:name_B, timestamp=, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:password_A, timestamp=, value=\x00\x00\x04\xD2
column=info:password_B, timestamp=, value=\x00\x00\x04\xD2
column=info:name, timestamp=, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
column=info:name_A, timestamp=, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
column=info:name_B, timestamp=, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
column=info:password, timestamp=, value=\x00\x00\x04\xD2
column=info:password_A, timestamp=, value=\x00\x00\x04\xD2
column=info:password_B, timestamp=, value=\x00\x00\x04\xD2
row(s) in 0.0790 seconds hbase(main)::>

最新文章

  1. 表单验证插件之jquery.validate.js
  2. ubuntu14.04 server 安装vmware worktation 12
  3. 如何让你的scrapy爬虫不再被ban
  4. Linux内核基础--事件通知链(notifier chain)
  5. Strom Topology执行分析:worker数,Bolt实例数,executor数,task数
  6. hdu 4417 划分树
  7. Java 内部类种类及使用解析
  8. Flex移动应用程序开发的技巧和窍门(五)
  9. HDU1114Piggy-Bank(完全背包)
  10. Cocos2D iOS之旅:如何写一个敲地鼠游戏(七):弹出地鼠
  11. urllib库详解 --Python3
  12. Hotspot Java虚拟机的类加载器
  13. bs4抓取糗事百科
  14. 潭州课堂25班:Ph201805201 django 项目 第四十一课 后台 轮播图管理功能讲解,文档管理功能 实现 (课堂笔记)
  15. 【亲测】在网页上查找接口,Network--XHR,出来的每个Name都是后台的一个接口
  16. C++ 日志生成 DLL
  17. 像素鸟 游戏开发教程 FlappyBird Game development tutorial
  18. Java中的Calendar日历用法详解
  19. Codeforces Round #359 (Div. 2) B. Little Robber Girl&#39;s Zoo 水题
  20. CSS Sprites 原理技术介绍及其优化方法

热门文章

  1. jstl中取map,其中map的key是一个对象,value是一个list
  2. [Python爬虫] 之三十:Selenium +phantomjs 利用 pyquery抓取栏目
  3. gitlab多人协同工作 ——转
  4. PageRank学习
  5. Angular 监听路由变化事件
  6. 小程序 的 textarea 组件 层级问题如何解决
  7. ant-design-pro Login 组件 实现 rules 验证
  8. linux下的QQ执行玩法:pidgin-lwqq
  9. 08-hibernate注解-多对多单向外键关联
  10. android studio教学视频资源(点开即看)