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)::>

hbase java Api练习的更多相关文章

  1. 【Hbase学习之三】Hbase Java API

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...

  2. hbase java api样例(版本1.3.1,新API)

    hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...

  3. hbase java API跟新数据,创建表

    package hbaseCURD; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import o ...

  4. HBase 学习之路(六)——HBase Java API 的基本使用

    一.简述 截至到目前(2019.04),HBase 有两个主要的版本,分别是1.x 和 2.x ,两个版本的Java API有所不同,1.x 中某些方法在2.x中被标识为@deprecated过时.所 ...

  5. HBase 系列(六)——HBase Java API 的基本使用

    一.简述 截至到目前 (2019.04),HBase 有两个主要的版本,分别是 1.x 和 2.x ,两个版本的 Java API 有所不同,1.x 中某些方法在 2.x 中被标识为 @depreca ...

  6. Hbase Java API详解

    HBase是Hadoop的数据库,能够对大数据提供随机.实时读写访问.他是开源的,分布式的,多版本的,面向列的,存储模型. 在讲解的时候我首先给大家讲解一下HBase的整体结构,如下图: HBase ...

  7. Hbase Java API程序设计步骤

    http://www.it165.net/admin/html/201407/3390.html 步骤1:创建一个Configuration对象 包含了客户端链接Hbase服务所需的全部信息: zoo ...

  8. HBase Java API使用(一)

    前言 1. 创建表:(由master完成) 首先需要获取master地址(master启动时会将地址告诉zookeeper)因而客户端首先会访问zookeeper获取master的地址 client和 ...

  9. Hbase(六) hbase Java API

    一. 几个主要 Hbase API 类和数据模型之间的对应关系: 1. HBaseAdmin关系: org.apache.hadoop.hbase.client.HBaseAdmin作用:提供了一个接 ...

  10. Hbase Java API包括协处理器统计行数

    package com.zy; import java.io.IOException; import org.apache.commons.lang.time.StopWatch; import or ...

随机推荐

  1. Axure 简单原型设计

    简介 Axure RP是一个专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写. Axure RP是美国 ...

  2. Python Matplotlib绘制气温图表

    代码中数据从 www.wunderground.com/history/ 下载 #coding=utf-8 import csv from datetime import datetime from ...

  3. transport.js报hasOwnProperty对象不支持此属性

    ECShop transport.js错误 这次出现的问题是transport.js在IE下提示对象不支持该用法.出现错误位置为608行的下面的代码: if(this.hasOwnProperty(k ...

  4. dubbo forbid 注意的几种方式

    1.检查所调用的项目模块是否起来了 2.如果起来后,检查该模块配置是否正确 3.服务端起来后与管理端的项目内容不一致(比如服务端增加了东西,管理端没有更新)

  5. 在页面左右一个悬浮div兼容IE6 IE7 8 9 Firefox chrome

    在页面左右一个悬浮div兼容IE6 IE7 8 9 Firefox chrome #identifier-pannel { bottom: 345px; margin-left: 512px; pos ...

  6. RTSP - RTP over TCP

    RTP over RTSP(TCP)(一)   RTP over RTSP包混合发送的解决办法   RTSP - RTP over TCP     To use TCP communication, ...

  7. java设计模式之模板方法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744002 今天你还是像往常一样来上班,一如既往地开始了你的编程工作. 项目经理告 ...

  8. react-redux 中 connect 的常用写法

    1.方式一 export default connect(({ monitorRedux })=>{ return { data: monitorRedux.data } })(Monitor) ...

  9. VUE购物车示例

    代码下载地址:https://github.com/MengFangui/VueShoppingCart 1.index.html <!DOCTYPE html> <html lan ...

  10. 【BIEE】MDS-01377:无法从使用 JNDI 名称 “jdbc/mds/owsm” 配置的数据源获取数据库连接

    这是一次意外关机情况,BIEE环境是window server2008,但是一次意外情况,BIEE自动关机了,造成开启成功新启动BIEE产生如下错误: 错误产生后,倒腾了半天,并没发现什么问题,只是通 ...