HBase——使用Java API操作HBase(6)

1、新建Java項目

2、將HBase下的jar包添加到Java項目中去。

3、啓動hadoop、hbase

 

4、使用Java代碼操作Hbase

創建表:

注意需要:hamcrest-core-1.3.jar包 ,

否則會如下錯誤信息:

TestHBase.java

package com.xbmu.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.junit.Test;

public class TestHBase {
    @Test
    public void testCreateTable() throws Exception{
        // 創建表
        // 配置zookeeper地址
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","192.168.159.111");

        // 得到一個HBase的客戶端
        HBaseAdmin client = new HBaseAdmin(conf);
        // 採用:面向對象的思想來建表
        // 1.指定表的描述符
        HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("mytable"));
        // 2.指定列族
        htd.addFamily(new HColumnDescriptor("info"));
        htd.addFamily(new HColumnDescriptor("grade"));

        // 創建表
        client.createTable(htd);

        // 關閉客戶端
        client.close();
    }
}

報錯:org.apache.hadoop.hbase.MasterNotRunningException

解決方法:https://blog.csdn.net/chen798213337/article/details/52097764

如果還解決不了:就修改一下本地電腦的hosts文件

C:\Windows\System32\drivers\etc\hosts

插入數據:

    @Test
    public void testPutData() throws Exception{
        // 插入數據
        // 配置zookeeper地址
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","192.168.159.111");

        // 得到一個客戶端
        HTable client = new HTable(conf,"mytable");

        // 構造一個Put對象:一條數據
        Put put = new Put(Bytes.toBytes("id001"));

        // 指定列的值
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("Tom"));

        // 一次插入多條記錄 client.put(List);
        client.put(put);

        client.close();
    }

獲取數據

@Test
    public void testGetData() throws Exception{
        //查詢數據:指定Rowkey
        //配置ZooKeeper地址
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.159.111");

        //得到一個客戶端
        HTable client = new HTable(conf, "mytable");

        // 構造一個Get對象
        Get get = new Get(Bytes.toBytes("id001"));

        //執行查詢: 相當於  select * from mytable where rowkey=???
        Result r = client.get(get);

        //輸出:注意:HBase中,沒有數據的類型,所有類型都是二進制
        String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
        System.out.println("名字是:" + name);

        client.close();

    }

獲取所有數據

@Test
    public void testScanData() throws Exception{
        //查詢數據
        //配置ZooKeeper地址
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "192.168.159.111");

        //得到一個客戶端
        HTable client = new HTable(conf, "mytable");

        //定義一個掃描器
        Scan scan = new Scan();
        //過濾器:scan.setFilter(filter)

        //通過掃描器查詢所有的數據
        //相當於:JDBC:ResultSet
        ResultScanner rs = client.getScanner(scan);

        //輸出
        for(Result r:rs){
            String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
            System.out.println("名字是:" + name);
        }

        client.close();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章