通过Java操作HBase

  本文主要是利用IDEA 2017做一个小的Demo,能访问CentOS的HBase。

一,hosts设置

1,Win10的C:WindowsSystem32driversetc目录下,在hosts的最后加一行代码。(其中,LZW是CentOS的主机名)

192.168.30.128     LZW

2,通过如下命令编辑CentOS的hosts文件,同样是加上上面的IP

vi /etc/hosts

二,代码编写  

1,IDEA 2017新建Maven项目,填入信息,直到完成

  

  

2,pom.xml文件新增如下配置,并应用更改

  <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.6</version>
        </dependency>
  </dependencies>

3,新建com.demo.hbase包,并新增HBaseDemo代码文件

package com.demo.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBaseDemo {
    public static void main(String[] args) throws IOException {
        Configuration conf= HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","192.168.30.129");

        HTable table=new HTable(conf,"testtable");

        Put put=new Put(Bytes.toBytes("row1"));
        put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"),Bytes.toBytes("val1"));
        put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual2"),Bytes.toBytes("val2"));

        table.put(put);
    }
}

4,运行代码,然后在HBase shell查看结果。成功添加

scan 'testtable'

  

原文地址:https://www.cnblogs.com/nanchuan/p/7615486.html