hbase java Api练习

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
 1223434                                                     column=info:age, timestamp=1498891615751, value=25
 1223434                                                     column=info:gender, timestamp=1498891615751, value=1
 1223434                                                     column=info:name, timestamp=1498892027083, value=lisi1234
 1223434                                                     column=info:password, timestamp=1498892027083, value=\x00\x12\xD5.
 1234                                                        column=info:name, timestamp=1498892427671, value=lisi1234
 1234                                                        column=info:password, timestamp=1498892427671, value=\x00\x12\xD5.
 12341                                                       column=info:name, timestamp=1498894169373, value=sudan
 12341                                                       column=info:password, timestamp=1498894169373, value=\x00\x00\x00\x17
 12342                                                       column=info:name, timestamp=1498894341082, value=hanmei
 12342                                                       column=info:password, timestamp=1498894341082, value=\x00\x00\x04\xD2
 123422                                                      column=info:name, timestamp=1498894389540, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
 123422                                                      column=info:password, timestamp=1498894389540, value=\x00\x00\x04\xD2
 12343                                                       column=info:name, timestamp=1498894341091, value=sunwukong
 12343                                                       column=info:password, timestamp=1498894341091, value=\x00\x00\x04\xD2
 123432                                                      column=info:name, timestamp=1498894389549, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
 123432                                                      column=info:password, timestamp=1498894389549, value=\x00\x00\x04\xD2
 12344                                                       column=info:name, timestamp=1498894341093, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
 12344                                                       column=info:password, timestamp=1498894341093, value=\x00\x00\x04\xD2
 123442                                                      column=info:name, timestamp=1498894389551, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
 123442                                                      column=info:password, timestamp=1498894389551, value=\x00\x00\x04\xD2
 223422                                                      column=info:name, timestamp=1498894677209, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
 223422                                                      column=info:password, timestamp=1498894677209, value=\x00\x00\x04\xD2
 223432                                                      column=info:name, timestamp=1498894677222, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
 223432                                                      column=info:password, timestamp=1498894677222, value=\x00\x00\x04\xD2
 223442                                                      column=info:name, timestamp=1498894677225, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
 223442                                                      column=info:password, timestamp=1498894677225, value=\x00\x00\x04\xD2
 323422                                                      column=info:name, timestamp=1498894726797, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
 323422                                                      column=info:name_A, timestamp=1498896667538, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
 323422                                                      column=info:name_B, timestamp=1498896689859, value=\xE9\x83\xAD\xE5\xBE\xB7\xE7\xBA\xB2
 323422                                                      column=info:password, timestamp=1498894726797, value=\x00\x00\x04\xD2
 323422                                                      column=info:password_A, timestamp=1498896689859, value=\x00\x00\x04\xD2
 323432                                                      column=info:name, timestamp=1498894726809, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
 323432                                                      column=info:name_A, timestamp=1498896667547, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
 323432                                                      column=info:name_B, timestamp=1498896689870, value=\xE6\xB2\x99\xE6\xB2\xB3\xE4\xB8\x8A
 323432                                                      column=info:password, timestamp=1498894726809, value=\x00\x00\x04\xD2
 323432                                                      column=info:password_A, timestamp=1498896667547, value=\x00\x00\x04\xD2
 323432                                                      column=info:password_B, timestamp=1498896689870, value=\x00\x00\x04\xD2
 323442                                                      column=info:name, timestamp=1498894726811, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
 323442                                                      column=info:name_A, timestamp=1498896667549, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
 323442                                                      column=info:name_B, timestamp=1498896689872, value=\xE7\x8C\xAA\xE5\x85\xAB\xE6\x88\x92
 323442                                                      column=info:password, timestamp=1498894726811, value=\x00\x00\x04\xD2
 323442                                                      column=info:password_A, timestamp=1498896667549, value=\x00\x00\x04\xD2
 323442                                                      column=info:password_B, timestamp=1498896689872, value=\x00\x00\x04\xD2
15 row(s) in 0.0790 seconds

hbase(main):017:0> 
时间: 2024-10-06 14:47:24

hbase java Api练习的相关文章

Hbase java API 调用详解

Hbase java API 调用 一. hbase的安装 参考:http://blog.csdn.net/mapengbo521521/article/details/41777721 二.hbase访问方式 Native java api:最常规最高效的访问方式. Hbase shell:hbase的命令行工具,最简单的接口,适合管理员使用 Thrift gateway:利用thrift序列化结束支持各种语言,适合异构系统在线访问 Rest gateway:支持rest风格的http api

HBase Java API使用(一)

前言 1. 创建表:(由master完成) 首先需要获取master地址(master启动时会将地址告诉zookeeper)因而客户端首先会访问zookeeper获取master的地址 client和master通信,然后有master来创建表(包括表的列簇,是否cache,设置存储的最大版本数,是否压缩等). 2. 读写删除数据 client与regionserver通信,读写.删除数据 写入和删除数据时讲数据打上不同的标志append,真正的数据删除操作在compact时发生 3. 版本信息

HBase Java API使用

概括 1. 创建.删除及启用禁用表.添加列等都需用到HBaseAdmin,另外需要注意删除,添加列等操作都需要禁用表 2. 表中添加数据,查询等都是和HTable相关,如果是多线程的情况下注意用HTablePool 3.  插入数据使用Put,可以单行添加也可批量添加 4. 查询数据需使用Get,Result,Scan.ResultScanner等 一.HBaseConfiguration org.apache.hadoop.hbase.HBaseConfiguration 对HBase进行配置

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

验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(写缓存) 6.单条get 7.批量get 8.简单scan 具体请参考GitHub. https://github.com/quchunhui/hbase_sample pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave

Hbase java api

本文中使用的是最原始的java api, 没有使用spring-data-hbase,只是使用spring管理Hbase的配置 查询操作分为如下几个步骤: 1. 获取Hbase配置,这里的配置主要指hbase的地址.如果是Zookeeper管理的,可以使用Zookeeper的地址和端口 2. 根据配置获取Hbase连接: connection = ConnectionFactory.createConnection(this.hbaseConfig.gethBaseConfiguration()

【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-hadoop2 建立一个java工程 导入hadoop 相关jar导入hbase相关jar 使用客户端(java API)操作hbase 示例一 package hbase; import java.text.SimpleDateFormat; import java.util.ArrayList;

Hbase java API test

依赖: <dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.6</version> </dependency> <dependency> <groupId>org.apache.hbase</g

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

package com.zy; import java.io.IOException; import org.apache.commons.lang.time.StopWatch; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.cli

HBase学习(十一)hbase Java API 介绍及使用示例

几个相关类与HBase数据模型之间的对应关系 java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) HTableDescriptor 列族(Column Family) Put 列修饰符(Column Qualifier) Get Scanner 一.HBaseConfiguration 关系:org.apache.hadoop.hbase.HBaseConfiguration 作用:对HBase进

HBase Java API类介绍

几个相关类与HBase数据模型之间的对应关系 java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) HTableDescriptor 列族(Column Family) Put 列修饰符(Column Qualifier) Get Scanner 一.HBaseConfiguration 关系:org.apache.hadoop.hbase.HBaseConfiguration 作用:对HBase进