HBase----从创建到简单使用的代码案例

本文档详细介绍了HBase从创建到简单使用的全过程,包括关键步骤和代码示例,帮助初学者快速掌握HBase的基础操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

package com.learn.hbase.demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

/**
 * 通过ZK连接HBase,创建HBaseClient
 */
public class HBaseClient {

//    在HBase用户中,常见的使用Connection的错误方法有:
//   (1)自己实现一个Connection对象的资源池,每次使用都从资源池中取出一个Connection对象;
//   (2)每个线程一个Connection对象。
//   (3)每次访问HBase的时候临时创建一个Connection对象,使用完之后调用close关闭连接。
//    在HBase中Connection类已经实现了对连接的管理功能,所以我们不需要自己在Connection之上再做额外的管理。
//    另外,Connection是线程安全的,而Table和Admin则不是线程安全的,
//    因此正确的做法是一个进程共用一个Connection对象,而在不同的线程中使用单独的Table和Admin对象
    private Connection conn;

    public HBaseClient(String zkQuorum) {
        Configuration defaultConf = HBaseConfiguration.create();
        defaultConf.set("hbase.zookeeper.quorum", zkQuorum);

        try {
            conn = ConnectionFactory.createConnection(defaultConf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 负责DDL
     */
    public Admin getAdmin() {
        try {
            return conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 负责DML
     */
    public Table getTable(TableName tableName) {
        try {
            return conn.getTable(tableName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

2.

package com.learn.hbase.demo;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * 创建HBaseClient(admin/table)
 */
public abstract class BaseDemo {

    protected static HBaseClient hBaseClient = new HBaseClient("localhost:2383");

    protected static final String NS = "learn";

    //id,name,sex,age,enter_date,status
    protected static final String TBL = "students";

    protected static final TableName TABLE_NAME = TableName.valueOf(NS, TBL);

    protected static final byte[] CF = "info".getBytes();

    protected static final byte[] COL_NAME = "name".getBytes();
    protected static final byte[] COL_GENDER = "gender".getBytes();
    protected static final byte[] COL_AGE = "age".getBytes();
    protected static final byte[] COL_ENTER_DATE = "enter_date".getBytes();
    protected static final byte[] COL_STATUS = "status".getBytes();

    protected static void showStudentInfo(Result result) {

        System.out.print("\nid:" + Bytes.toString(result.getRow()));
        System.out.print(",name:" + Bytes.toString(result.getValue(CF, COL_NAME)));
        System.out.print(",gender:" + Bytes.toString(result.getValue(CF, COL_GENDER)));
        System.out.print(",age:" + Bytes.toString(result.getValue(CF, COL_AGE)));
        System.out.print(",enter_date:" + Bytes.toString(result.getValue(CF, COL_ENTER_DATE)));
        System.out.print(",status:" + Bytes.toString(result.getValue(CF, COL_STATUS))+"\n");

    }
}

3.

package com.learn.hbase.demo;


import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * DDL操作
 */
public class AdminDemo extends BaseDemo {

    public static void main(String[] args) throws IOException {

        Admin admin = hBaseClient.getAdmin();
        initNamespace(admin);
//        flushAndCompact(admin);
        admin.close();

    }

    private static void flushAndCompact(Admin admin) throws IOException {
        admin.flush(TABLE_NAME);
        admin.majorCompact(TABLE_NAME);
    }

    private static void initNamespace(Admin admin) throws IOException {
        //hbase api没有类似existNamespace的方法,故以下变通的方法来判断某个namespace是否存在
        Set<String> existsNS = new HashSet<>();
        for (NamespaceDescriptor nd : admin.listNamespaceDescriptors()) {
            existsNS.add(nd.getName());
        }

        //删除已经存在的NS
        if (existsNS.contains(NS)) {
            System.out.println(NS + " exists,delete it first ...");

            //列出hbase中所有的表,逐个删除
            for (TableName tn : admin.listTableNamesByNamespace(NS)) {

                //下线表
                System.out.println("disable table:" + tn.getNameWithNamespaceInclAsString());
                admin.disableTable(tn);

                //删除表
                System.out.println("delete table:" + tn.getNameWithNamespaceInclAsString());
                admin.deleteTable(tn);
            }

            //删除namespace
            admin.deleteNamespace(NS);

            System.out.println("delete ns:" + NS);
        }

        //创建ns
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(NS).build();
        admin.createNamespace(namespaceDescriptor);
        System.out.println("create ns:" + NS);


        //创建table
        HTableDescriptor descriptor = new HTableDescriptor(TABLE_NAME);

        //列族
        HColumnDescriptor cf = new HColumnDescriptor("info");
        cf.setTimeToLive(HConstants.FOREVER);
        cf.setMinVersions(1);
        cf.setMaxVersions(3);
        descriptor.addFamily(cf);

        admin.createTable(descriptor);

        System.out.println("create table:" + TABLE_NAME);

        System.out.println("desc table:" + admin.getTableDescriptor(TABLE_NAME));
    }
}

4.

package com.learn.hbase.demo;


import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;

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

/**
 * insert/update
 */
public class PutDemo extends BaseDemo {

    public static void main(String[] args) throws IOException {
        Table table = hBaseClient.getTable(TABLE_NAME);
//        singlePut(client);
        batchPut(table);
        table.close();
    }


    /**
     * 单条插入
     */
    public static void singlePut(Table table) throws IOException {
        Put put = new Put("D00001".getBytes());
        put.addColumn(CF, COL_NAME, "Tom".getBytes());
        put.addColumn(CF, COL_AGE, "25".getBytes());
        put.addColumn(CF, COL_GENDER, "F".getBytes());
        put.addColumn(CF, COL_ENTER_DATE, "2018-10-01".getBytes());
        put.addColumn(CF, COL_STATUS, "learning".getBytes());
        table.put(put);
    }

    /**
     * 批量插入
     */
    public static void batchPut(Table table) throws IOException {
        //年龄未知,不填写
        Put put1 = new Put("D00002".getBytes()); // 1 - Long.MAX_VALUE,如设置为Long.MAX_VALUE,HBase会修改成系统当前时间,精确到毫秒
        put1.addColumn(CF, COL_NAME, "小明".getBytes());
        put1.addColumn(CF, COL_GENDER, "M".getBytes());
        put1.addColumn(CF, COL_ENTER_DATE, "2019-01-01".getBytes());
        put1.addColumn(CF, COL_STATUS, "learning".getBytes());


        Put put2 = new Put("D00003".getBytes());
        put2.addColumn(CF, COL_NAME, "大强".getBytes());
        put2.addColumn(CF, COL_AGE, "30".getBytes());
        put2.addColumn(CF, COL_GENDER, "M".getBytes());
        put2.addColumn(CF, COL_ENTER_DATE, "2018-01-01".getBytes());
        put2.addColumn(CF, COL_STATUS, "graduated".getBytes());

        List<Put> puts = new ArrayList();
        puts.add(put1);
        puts.add(put2);

        table.put(puts);
    }
}

5.

package com.learn.hbase.demo;


import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

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

/**
 * 根据RowKey查询
 */
public class GetDemo extends BaseDemo {

    public static void main(String[] args) throws IOException {
        Table client = hBaseClient.getTable(TABLE_NAME);

        singleGet(client);

        client.close();
    }

    /**
     * 单条获取
     */
    public static void singleGet(Table table) throws IOException {
        Get get = new Get("D00001".getBytes());
        Result result = table.get(get);
        showStudentInfo(result);
    }

    /**
     * 批量获取
     */
    public static void batchGet(Table table) throws IOException {
        Get get = new Get("D00002".getBytes());
        Get get2 = new Get("D00003".getBytes());

        List<Get> gets = new ArrayList();
        gets.add(get);
        gets.add(get2);

        Result[] results = table.get(gets);

        for (Result r : results) {
            showStudentInfo(r);
        }
    }
}

6.

package com.learn.hbase.demo;


import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.*;

import java.io.IOException;

/**
 * 范围扫描及过滤器使用
 */
public class ScanDemo extends BaseDemo {

    public static void main(String[] args) throws IOException {

        Table client = hBaseClient.getTable(TABLE_NAME);

        Scan scan = new Scan();

        //include
        scan.setStartRow("D00001".getBytes());

        //exclude
        scan.setStopRow("D00004".getBytes());

        ResultScanner rs = client.getScanner(scan);
        showResults(rs, "rs:D00001 - D00003");

        Filter f1 = new SingleColumnValueFilter(CF, COL_STATUS, CompareFilter.CompareOp.EQUAL, new BinaryComparator("learning".getBytes()));
        scan.setFilter(f1);

        ResultScanner rs2 = client.getScanner(scan);
        showResults(rs2, "rs2:状态为learning");


        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        Filter f2 = new SingleColumnValueFilter(CF, COL_GENDER, CompareFilter.CompareOp.EQUAL, new BinaryComparator("F".getBytes()));
        filterList.addFilter(f1);
        filterList.addFilter(f2);
        scan.setFilter(filterList);

        ResultScanner rs3 = client.getScanner(scan);
        showResults(rs3, "rs3:状态为learning且性别是F");

        client.close();
    }

    private static void showResults(ResultScanner rs, String tag) {
        System.out.println("\n---- " + tag + " ----------");
        for (Result r : rs) {
            showStudentInfo(r);
        }
    }
}

7.

package com.learn.hbase.demo;


import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;

/**
 * 删除记录
 */
public class DeleteDemo extends BaseDemo {

    public static void main(String[] args) throws IOException {

        Table client = hBaseClient.getTable(TABLE_NAME);

//        deleteCol(client);
//        deleteRow(client);
        putAfterDel(client);
        client.close();
    }

    /**
     * 删除整行
     */
    private static void deleteRow(Table table) throws IOException {
        Delete delete = new Delete("D00001".getBytes());
        table.delete(delete);
    }

    /**
     * 删除单列
     */
    private static void deleteCol(Table table) throws IOException {
        Delete delete = new Delete("D00001".getBytes());
        delete.addColumn(CF, COL_AGE);
        table.delete(delete);
    }

    /**
     * 删除之后,老version的数据无法插入,需要进行一次compact;新version的数据可以插入
     * <p>
     * flush 'gp:test'
     * major_compact 'gp:test'
     */
    private static void putAfterDel(Table table) throws IOException {
//        Put put = new Put("D00001".getBytes(), 1L);
        Put put = new Put("D00001".getBytes(),Long.MAX_VALUE);
        put.addColumn(CF, COL_NAME, "Tom".getBytes());
        table.put(put);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值