通過Java操作HBase

來源:互聯網
上載者:User

標籤:通過   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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.