hbase - [04] java訪問hbase

HOUHUILIN發表於2024-03-28

需要匯入jar包

  • $HBASE_HOME/lib下的所有jar包
  • $HADOOP_HOME/share/hadoop/common的所有jar包

package com.harley.hbase.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class HbaseTest{

    protected Connection connection = null;
    protected Admin admin = null;

    /**
     * 建立表所需引數
     */
    protected String tableName = "mydata:psn2";
    protected String cf = "cf";

    @Before
    public void init()
    {
        System.out.println("====init....====");
        System.setProperty("hadoop.home.dir","D:/Java/bigdata/bigdata/hadoop-3.2.1");
        try {
            Configuration conf = new Configuration();
            conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
            connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * get
     */
    @Test
    public void get()
    {
        Table table = null;

        try {
            table = this.connection.getTable(TableName.valueOf(this.tableName));
            /**
             * 引數為 rowkey
             */
            Get get = new Get("student".getBytes("UTF-8"));
            Result result = table.get(get);

            String rowkey = new String(result.getRow(),"UTF-8");
            String name = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"name".getBytes("UTF")).getValueArray());
            String age = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"age".getBytes("UTF")).getValueArray());
            String gender = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"gender".getBytes("UTF")).getValueArray());
            System.out.println("---rowkey--->"+rowkey+"----name--->"+name+"---age---->"+age+"----gender--->"+gender);


        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if(table!=null){
                    table.close();
                    table=null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }

    /**
     * scan
     */
    @Test
    public void scan()
    {
        Table table = null;
        try {
            table = this.connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            ResultScanner scanner = table.getScanner(scan);

            int count = 1;

            for (Result result : scanner) {
                String rowkey = new String(result.getRow(), "UTF-8");
                String name = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"name".getBytes("UTF-8")).getValueArray());
                String age = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"age".getBytes("UTF-8")).toString());
                String gender = new String(result.getColumnLatestCell(this.cf.getBytes("UTF-8"),"gender".getBytes("UTF-8")).toString());

                System.out.println("--條數:"+count+"---rowkey=>"+rowkey+"----name=>"+name+"----age=>"+age+"----gender=>"+gender);
                count++;
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(table!=null){
                    table.close();
                    table=null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * put
     */
    @Test
    public void put()
    {
        Table table = null;
        try {
            table = this.connection.getTable(TableName.valueOf(tableName));
            /**
             * rowkey
             */
            Put put = new Put("student01".getBytes("UTF-8"));

            /**
             * 新增鍵和值
             */
//            put.addColumn(this.cf.getBytes("UTF-8"),"name".getBytes("UTF-8"),"老白".getBytes("UTF-8"));
//            put.addColumn(this.cf.getBytes("UTF-8"),"age".getBytes("UTF-8"),"32".getBytes("UTF-8"));
//            put.addColumn(this.cf.getBytes("UTF-8"),"gender".getBytes("UTF-8"),"男".getBytes("UTF-8"));

            put.addColumn(this.cf.getBytes("UTF-8"),"name".getBytes("UTF-8"),"Tom".getBytes("UTF-8"));
            put.addColumn(this.cf.getBytes("UTF-8"),"age".getBytes("UTF-8"),"20".getBytes("UTF-8"));
            put.addColumn(this.cf.getBytes("UTF-8"),"gender".getBytes("UTF-8"),"man".getBytes("UTF-8"));


            /**
             * 相當於事務中的commit
             */
            table.put(put);
            System.out.println("————————put success——————");



        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if(table!=null)
                table.close();
                table=null;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


    /**
     * create
     */
    @Test
    public void create()
    {
        System.out.println("====create....====");
        try {
            TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
            ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(this.cf.getBytes("UTF-8")).build();
            TableDescriptor tableDescriptor = tdb.setColumnFamily(columnFamilyDescriptor).build();
            this.admin.createTable(tableDescriptor);
            System.out.println("-------create success-------");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * list_namespace
     */
    @Test
    public void list_namespace()
    {

        try {
            NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();

            int count = 1;

            for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {

                System.out.println(count+"----------"+namespaceDescriptor.getName());
                count++;
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    /**
     * list
     */
    @Test
    public void list()
    {
        System.out.println("——————————list....———————————");

        try {

            List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();
            int count=1;
            for (TableDescriptor tableDescriptor : tableDescriptors) {

                System.out.println(count+"====="+tableDescriptor.getTableName());
                count++;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @After
    public void after()
    {
        System.out.println("====destroy....====");
        try {
            if(admin!=null){
                admin.close();
                admin=null;
            }
            if(connection!=null)
            {
                connection.close();
                connection=null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

— 要養成終生學習的習慣 —

相關文章