標籤:通過 driver static XML creat pack lzw 目錄 config
本文主要是利用IDEA 2017做一個小的Demo,能訪問CentOS的HBase。
一,hosts設定
1,Win10的C:\Windows\System32\drivers\etc目錄下,在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‘
通過Java操作HBase