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</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.6</version>
        </dependency>
    </dependencies>

API test 代码:

package com.sea.hbase;

import java.io.IOException;
import java.util.ArrayList;

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.Admin;
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.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.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
 * *************************************************************************
 * <PRE>
 *  @ClassName:    : HbseApiDemo
 *
 *  @Description:    :
 *
 *  @Creation Date   : 2019年9月16日 下午1:58:04
 *
 *  @Author          :  Sea
 *
 *
 * </PRE>
 **************************************************************************
 */
public class HbseApiDemo11 {

    public static Configuration conf;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.18.199:2181,192.168.18.203:2181,192.168.18.204:2181");
    }

    /**
     * 判断是表是否存在
     *
     * @param tableName
     * @return
     * @throws MasterNotRunningException
     * @throws ZooKeeperConnectionException
     * @throws IOException
     */
    public static boolean isTableExist(String tableName)
            throws MasterNotRunningException, ZooKeeperConnectionException, IOException {

        // 在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        return admin.tableExists(TableName.valueOf(tableName));
    }

    /**
     * create table
     *
     * @param tableName
     * @param columnFamily
     * @throws MasterNotRunningException
     * @throws ZooKeeperConnectionException
     * @throws IOException
     */
    public static void createTable(String tableName, String... columnFamily) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        // 判断表是否存在
        if (isTableExist(tableName)) {
            System.out.println("表" + tableName + "已存在");
            // System.exit(0);
        } else {
            // 创建表属性对象,表名需要转字节
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            // 创建多个列族
            for (String cf : columnFamily) {
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
                hColumnDescriptor.setMinVersions(1);
                hColumnDescriptor.setMaxVersions(1);
                descriptor.addFamily(hColumnDescriptor);
            }
            // 根据对表的配置,创建表
            admin.createTable(descriptor);
            System.out.println("表" + tableName + "创建成功!");

        }
    }

    /**
     * 删除表
     * @param tableName
     * @param columnFamily
     * @throws IOException
     */
    public static void dropTable(String tableName) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        // 判断表是否存在
        if (isTableExist(tableName)) {
            // delete this table
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("表" + tableName + "删除成功!");
        } else {
            System.out.println("表" + tableName + "不存在!");

        }
    }

    /**
     * 删除多行数据
     * @param tableName
     * @param rows
     * @throws IOException
     */
    public static void deleteMutilRows(String tableName ,String... rowkeys )throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));
        ArrayList<Delete> deletes = new ArrayList<Delete>();
        for (String rowkey : rowkeys)
        {
             deletes.add(new Delete(Bytes.toBytes(rowkey)));
        }
        table.delete(deletes);
    }

    /**
     * 删除指定列族的数据
     * @param tableName
     * @param rows
     * @throws IOException
     */
    public static void deleteColumnFamily(String tableName ,String rowkey ,String columnFamilys,String column)throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));
            Delete delete = new Delete(Bytes.toBytes(rowkey));
            delete.addColumn(Bytes.toBytes(columnFamilys),Bytes.toBytes(column));
        table.delete(delete);
    }

    /**
     * 向表中插入数据
     * @param tableName
     * @param rowKey
     * @param columnFamily
     * @param column
     * @param value
     * @throws IOException
     */
    public static  void  addRowData(String tableName,String rowkey,String columnFamily ,String colName,String value) throws IOException {

        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put=new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(colName), Bytes.toBytes(value));
        table.put(put);

    }

    /**
     * 得到所有的数据
     * @param tableName
     * @throws IOException
     */
    public static void getAllRows(String tableName) throws IOException {

        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan=new Scan();
        ResultScanner scanResult = table.getScanner(scan);
        for (Result result : scanResult) {
//            byte[] row = result.getRow();
            Cell[] rawCell = result.rawCells();
            for (Cell cell : rawCell)
            {
                System.err.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
                System.err.println(Bytes.toString(CellUtil.cloneRow(cell)));
                System.err.println(Bytes.toString(CellUtil.cloneFamily(cell)));
                System.err.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.err.println(Bytes.toString( CellUtil.cloneValue(cell)));
             }
        }
    }
    /**
     * 得到所有的数据
     * @param tableName
     * @throws IOException
     */
    public static void getByRowkeys(String tableName,String rowkey) throws IOException {

        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf(tableName));

        Get get=new Get(Bytes.toBytes(rowkey));
        Result result = table.get(get);
            Cell[] rawCell = result.rawCells();
            for (Cell cell : rawCell)
            {
                System.err.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
                System.err.println(Bytes.toString(CellUtil.cloneRow(cell)));
                System.err.println(Bytes.toString(CellUtil.cloneFamily(cell)));
                System.err.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.err.println(Bytes.toString( CellUtil.cloneValue(cell)));
            }
    }

    /**
     * test filter
     * @param args
     * @throws IOException
     *      比较器  ByteArrayComparable
        通过比较器可以实现多样化目标匹配效果,比较器有以下子类可以使用:
        BinaryComparator               匹配完整字节数组
        BinaryPrefixComparator     匹配字节数组前缀
        BitComparator
        NullComparator
        RegexStringComparator    正则表达式匹配
        SubstringComparator        子串匹配
     */
      public static ResultScanner getDataFamilyFilter(String tableName,String family) throws IOException{
            Connection connection = ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL ,
                    new BinaryComparator(Bytes.toBytes(family)));   //表中不存在account列族,过滤结果为空
//             new BinaryPrefixComparator(value) //匹配字节数组前缀
//             new RegexStringComparator(expr) // 正则表达式匹配
//             new SubstringComparator(substr)// 子字符串匹配
            Scan scan = new Scan();
            // 通过scan.addFamily(family)  也可以实现此操作
            scan.setFilter(ff);
            ResultScanner resultScanner = table.getScanner(scan);
            return resultScanner;
        }

    public static void main(String[] args) throws IOException {

        boolean tableExist = isTableExist("student");
        System.err.println(tableExist);
        System.err.println("############################");

        System.err.println("#########create staff1 ###################");

        createTable("staff1", "info1","info2");
        addRowData("staff1","0101","info1","sea","test");
        addRowData("staff1","0102","info2","sea1","test1");
        addRowData("staff1","0102","info2","name","test1");
        addRowData("staff1","0102","info2","addr","test1");
        boolean isExitStaff = isTableExist("staff1");
        System.err.println("exit table isExitStaff"+isExitStaff);
        System.err.println(isExitStaff);
//        dropTable("staff1");
        System.err.println("############################");

//        deleteMutilRows("student","1002","10023");
//        deleteColumnFamily("student","10025","info","sex");
//
//        addRowData("student","0101","info","sea","test");
        getAllRows("staff1");
    }

}

原文地址:https://www.cnblogs.com/lshan/p/12072036.html

时间: 2024-08-28 18:47:14

Hbase java API test的相关文章

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包括协处理器统计行数

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进