HBase的JavaAPI

HBase通过JavaAPI和HIVE集成

1、Maven导入依赖包: hbase-clinet 版本与server上的hbase相同

<dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>1.3.5</version>
</dependency>
<dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-hbase-handler</artifactId>
      <version>1.0.0</version>
</dependency>
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
</dependency>

 

2、编写demo

//Configuration同Hadoop
//Connection打开hbase链接
//Admin类管理命名空间和表等元数据
//connection管理数据
//H【Table】|【Column】Descriptor table描述类/列族描述类/列描述类/
public class HbaseJavaApi {
    static Connection connect;
    public static void main(String args[]) {
        try {
            createTable();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            listTable();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void createTable() throws IOException {
        //账户类
        Admin admin=getAdmin();
        //表描述类
        HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf("demo6"));
        //列描述类
        HColumnDescriptor columnDescriptor=new HColumnDescriptor("info1");
        //挂载列到表上
        tableDescriptor.addFamily(columnDescriptor);
        //客户端执行添加表
        admin.createTable(tableDescriptor);
    }
    public static void listTable() throws IOException {
        Admin admin=getAdmin();
        //客户端执行list,获取表名数组
        TableName[] tableNames = admin.listTableNames();
        for (TableName name : tableNames) {
            System.out.println(name);
        }
        connect.close();
    }
    public static void insertColumn() throws IOException {
        //获取表
        Table table = connect.getTable(TableName.valueOf("zookeeper"));
        //构建PUT,属性为行键
        Put put=new Put("xixi".getBytes());
        //添加列值信息,列族,列键,cell值
        put.addColumn("cf01".getBytes(),"name".getBytes(),"jason".getBytes());
        //客户端执行put
        table.put(put);

        //构建get,属性为行键
        Get get=new Get("rk01".getBytes());
        //添加要查询的,列族,列键
        get.addColumn("cf01".getBytes(),"name".getBytes());
        //获取get结果
        Result result = table.get(get);
        //从结果中取得特定信息,指定列族列键的cell
        result.getValue("cf01".getBytes(),"name".getBytes());

        //构建scan
        Scan scan=new Scan();
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result1 : resultScanner) {
            //从结果中取得特定信息,指定列族列键的cell
            result1.getValue("cf01".getBytes(),"name".getBytes());
        }
    }
    public static Admin getAdmin() throws IOException {
        if (connect==null) {
            //开启链接,指定zookeeper链接信息
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum.", "localhost");
            conf.set("hbase.zookeeper.property.clientPort", "2181");
            connect = ConnectionFactory.createConnection(conf);
        }
        //打开客户端
        Admin admin=connect.getAdmin();
        return admin;
    }
}

 

3、发布工程,包括依赖包

543982433811aaf7edf275531db0bb6

bb638559fe42c55d9d40904b489fc88

911ae7ca865752beabd0344c9a705ce

12b4adf9b8ef8662681c665e505701b

 

4、shell 运行jar

上传jar包到服务器,命令执行

java -cp HBase-JAVAAPI.jar com.kgc.study.HBaseJavaApi

 

HBase by Phoenix

安装phoenix

1563850390390

1563850285180

每个regionserver上都安装上phoenix

双引号大小写,单引号

HBase by Hive

建表语句

CREATE external TABLE tbl_name_hive_new(
rowkey string,
val_cf1_col1 string,
val_cf1_col2 string,
val_cf2_col1 string,
val_cf2_col2 string
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serderproperties("hbase.columns.mappring"=":key,cf1:col1,cf1:col2,cf2:col1,cf2:col2")
tblproperties("hbase.table.name"="tbl_name_hbase_exists");

--hbase.columns.mapping列键的顺序与表中字段顺序一致
--无论外部内部表,数据都存在hbase维护的库中
--外部表建表要求hbase表已存在
--内部表建表要求hbase表不存在(此时内部表也是误删安全的)
--数据会在双方实时映射(因为操作的都是hbase上的同一个文件)

注:hbase插入的数据会先在内存缓冲区,因此可能会出现hbase更新数据后未实时更新到hive。如果发生,在hbase端使用compact tbl_name命令

  • hive读取的是hbase文件
  • hive导入已有的hbase可选用外部表,也可选用内部表
  • hive创建新的hbase表用内部表
  • hive写入可以实时映射到hbase
  • hbase可以写入实时映射到hive
posted @ 2019-08-28 15:43  WhoYoung  阅读(1207)  评论(0编辑  收藏  举报