代码示例

有很多的代码是重复的,只是为形成记忆,见谅,
另外需要将 hbase-site.xml,hdfs-site.xml,core-site.xml三个文件放到Resources上目录中

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.Batch;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;
import java.util.List;

public class HbaseApi_test_1 {
    private static Connection hbaseConn;
    private static Configuration hbaseConf;
    //HBaseAdmin 提供了一个接口来管理 HBase 数据库的表信息
    private static Admin hbaseAdmin;

    /*** 静态构造,在调用静态方法前运行,  初始化连接对象  * */
    static {
        hbaseConf = HBaseConfiguration.create();
        try {
            hbaseConn = ConnectionFactory.createConnection(hbaseConf);
            System.out.println("连接上了?" + !hbaseConn.isClosed());
            hbaseAdmin = hbaseConn.getAdmin();
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }

//****************************put记录 - 添加一行****************************
    public static void put_oneRow() throws java.lang.Exception {
        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);
        //设置rowkey
        Put zhangshan = new Put(Bytes.toBytes("zhangshan"));
        //为列赋值,要为字符型,否则读出来会乱码
        //参数1:列族 ,参数2:列,参数3:值,(这里的时间戳会自动生成)
        zhangshan.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes("30"));
        //参数1:列族 ,参数2:列,参数3:时间戳,参数4:值
        //zhangshan.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"), 1, Bytes.toBytes("31"));
        zhangshan.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("three"));

        //设置过期时间(毫秒),一旦达到到期时间,HBase 将自动删除行
        //zhangshan.setTTL(5000);

/**
 * 对于相对不太重要的数据,可以在Put/Delete操作时,通过调用Put.setWriteToWAL(false)或Delete.setWriteToWAL(false)函数,放弃写WAL日志,从而提高数据写入的性能。
 * 值得注意的是:谨慎选择关闭WAL日志,因为这样的话,一旦RegionServer宕机,Put/Delete的数据将会无法根据WAL日志进行恢复。
 */
        zhangshan.setWriteToWAL(false);

        table.put(zhangshan);

        //关闭资源
        table.close();
        //hbaseConn.close();
    }


    //****************************put记录 - 添加多行****************************
    public static void put_MultiRow() throws java.lang.Exception {
        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);
        //put的集合
        ArrayList<Put> puts = new ArrayList<>();
        //设置rowkey
        Put lishi = new Put(Bytes.toBytes("lishi"));
        //为列赋值,要为字符型,否则读出来会乱码
        lishi.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes("100"));
        lishi.addColumn(Bytes.toBytes("course"), Bytes.toBytes("shuxue"), Bytes.toBytes("99"));
        lishi.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"), Bytes.toBytes("98"));
        lishi.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("very good"));

        Put wangwu = new Put(Bytes.toBytes("wangwu"));
        wangwu.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes("500"));
        wangwu.addColumn(Bytes.toBytes("course"), Bytes.toBytes("shuxue"), Bytes.toBytes("499"));
        wangwu.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"), Bytes.toBytes("498"));
        wangwu.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("very very good"));

        puts.add(lishi);
        puts.add(wangwu);
        //一次添加多行
        table.put(puts);

        //关闭资源
        table.close();
        //hbaseConn.close();
    }


    //****************************批量put记录****************************
    public static void put_MultiRow_batch() throws java.lang.Exception {
        long begin = System.currentTimeMillis();

        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);
        //put的集合
        ArrayList<Put> puts = new ArrayList<>();

        for (int i = 1; i < 100000; i++) {
            Put put = new Put(Bytes.toBytes("person-" + i));
            put.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("good"));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes(i + ""));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("shuxue"), Bytes.toBytes(i + 1 + ""));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"), Bytes.toBytes(i + 2 + ""));
            puts.add(put);
        }
        table.put(puts);
        //写入hdfs中,不写也可以,hbase会自动处理的
        hbaseAdmin.flush(tableName);

        long end = System.currentTimeMillis();
        System.out.println("运行时间:" + (end - begin));
        //关闭资源
        table.close();
        //hbaseConn.close();
    }


    //****************************BufferedMutator主要用来异步批量的将数据写入一个hbase表,通过Connection获取一个实例。****************************
    public static void put_MultiRow_batch_bufferedMutator() throws java.lang.Exception
    {
        long begin = System.currentTimeMillis();

        TableName tableName = TableName.valueOf("ns1:mytest_4");
        BufferedMutator bufferedMutator = hbaseConn.getBufferedMutator(tableName);
        List<Put> puts = new ArrayList<>();
        for (int i = 1; i < 100000; i++) {
            Put put = new Put(Bytes.toBytes("person-" + i));
            put.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("good"));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes(i + ""));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("shuxue"), Bytes.toBytes(i + 1 + ""));
            put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"), Bytes.toBytes(i + 2 + ""));
            puts.add(put);
        }
        //异步写入
        bufferedMutator.mutate(puts);

        bufferedMutator.flush();

        long end = System.currentTimeMillis();
        System.out.println("运行时间:" + (end - begin));

        //关闭资源
        bufferedMutator.close();
        //hbaseConn.close();
    }


    //****************************删除记录 - 一次删一行****************************
    public static void delete_oneRow() throws java.lang.Exception {
        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);
        //设置要删除的行的rowkey
        Delete delete = new Delete(Bytes.toBytes("lishi"));
        //根据family删,可同时删除多个列
        delete.addFamily(Bytes.toBytes("grade"));
        //删除一个列
        delete.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"));
        //删除一个列,并指定删除哪个时间戳的数据
        //delete.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"),1);
        //删除一行
        table.delete(delete);

        //关闭资源
        table.close();
        //hbaseConn.close();
    }

    //****************************删除记录 - 一次删多行****************************
    public static void delete_MultiRow() throws java.lang.Exception {
        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);
        //要删除的行的列表
        ArrayList<Delete> deleteList = new ArrayList<>();
        //设置要删除的行的rowkey
        Delete del_1 = new Delete(Bytes.toBytes("lishi"));
        del_1.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"));

        Delete del_2 = new Delete(Bytes.toBytes("wangwu"));
        del_2.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"));
        del_2.addFamily(Bytes.toBytes("grade"));

        deleteList.add(del_1);
        deleteList.add(del_2);
        table.delete(deleteList);

        //关闭资源
        table.close();
        //hbaseConn.close();
    }

    //****************************先经过某些条件的验证,只有满足条件的才put或delete****************************
    public static void putAndCheck() throws java.lang.Exception {
        TableName tableName = TableName.valueOf("ns1:mytest_4");
        //取得一个要操作的表
        Table table = hbaseConn.getTable(tableName);
        //设置rowkey
        Put lishi = new Put(Bytes.toBytes("lishi"));
        //为列赋值,要为字符型,否则读出来会乱码
        lishi.addColumn(Bytes.toBytes("course"), Bytes.toBytes("yuwen"), Bytes.toBytes("50"));
        lishi.addColumn(Bytes.toBytes("course"), Bytes.toBytes("shuxue"), Bytes.toBytes("40"));
        lishi.addColumn(Bytes.toBytes("course"), Bytes.toBytes("english"), Bytes.toBytes("30"));
        lishi.addColumn(Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("bujige"));

        /**
         * 个人理解:前面4个参数为查询条件,通过 rowkey,列族,列,最新的时间戳,值 到hbase中查找,如果查询的条件匹配,就执行相应的命令,否则不执行
         * 如 table.checkAndPut(Bytes.toBytes("lishi"), Bytes.toBytes("grade"), Bytes.toBytes(""), null, lishi);
         * 先检查 rowkey为lishi,列族grade,没有值的时候,才能put
         * 另外注意,如果想查询的条件中,只有列族,没有列,看第3个参数要这样写 Bytes.toBytes("")
         */
        boolean b = table.checkAndPut(Bytes.toBytes("lishi"), Bytes.toBytes("grade"), Bytes.toBytes(""), Bytes.toBytes("chunhuo"), lishi);
        System.out.println(b);

        //先检查再delete
        Delete del_1 = new Delete(Bytes.toBytes("xiaoming"));
        boolean b1 = table.checkAndDelete(Bytes.toBytes("xiaoming"), Bytes.toBytes("grade"), Bytes.toBytes(""), CompareFilter.CompareOp.EQUAL,Bytes.toBytes("chunhuo"), del_1);
        System.out.println(b1);

        //关闭资源
        table.close();
        //hbaseConn.close();
    }

    //****************************一次清空表的所有记录****************************
    public static void truncate_table() throws java.lang.Exception {
        TableName tableName = TableName.valueOf("ns1:mytest_4");

        //禁用表
        hbaseAdmin.disableTable(tableName);
        //清空表,参数1 表名,参数2,是否保留拆分
        //保留拆分的话,hbase库的meta表,还是有些信息,hdfs的region目录也有
        hbaseAdmin.truncateTable(tableName, false);

        hbaseAdmin.close();
    }

}