HBase 最新版本(1.0以上)测试案例

最近写HBase,使用最近的1.0版本,发现很多功能都不建议使用,依据官方的文档写了份关于增删改查的测试案例,供学习交流。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * Hbase 基本CRUD 样例代码   覆盖Put Get Delete checkAndPut checkAndDelete  Scan
 * 通过上面的各种操作的例子, 会基本覆盖Htable可以用的的所有方法
 * 这里不涉及Hbase 管理代码的操作
 * @author seagle
 *
 */
public class HbaseCRUDTest {

    @Before
    public void beforeTest(){

    }

    @Test
    public void createTest() throws IOException {
        Configuration conf = HBaseConfiguration.create();

        Connection connection = ConnectionFactory.createConnection(conf);
        String tablename = "test";
        String familyname="info";
        Admin admin = connection.getAdmin();
        HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tablename));
        table.addFamily(new HColumnDescriptor(familyname).setCompressionType(Compression.Algorithm.SNAPPY));

        System.out.print("Creating table. ");

        // 判断是否存在,如果存在,先删除.
        if (admin.tableExists(table.getTableName())) {
            admin.disableTable(table.getTableName());
            admin.deleteTable(table.getTableName());  // 删除存在的表
        }

        // 创建表
        admin.createTable(table);
        System.out.println(" Done.");
    }

    @Test
    public void putTest() throws IOException {
        Configuration conf = HBaseConfiguration.create();
// Use below code for HBase version 1.x.x or above.
        Connection connection = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("test");
        Table table = connection.getTable(tableName);

        Put put = new Put(Bytes.toBytes("8")); // row key

//        put.addColumn(Bytes.toBytes("family"),Bytes.toBytes("qualifier"),Bytes.toBytes("value"));

        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("level_1"),Bytes.toBytes("4"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("level_2"),Bytes.toBytes("4"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("level_3"),Bytes.toBytes("4"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("level_4"),Bytes.toBytes("4"));

        table.put(put);
    }


    @Test
    public void getTest() throws IOException {
        Configuration conf = HBaseConfiguration.create();
// Use below code for HBase version 1.x.x or above.
        Connection connection = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("test");
        Table table = connection.getTable(tableName);

        StringBuffer sb = null;

        Get get = new Get(Bytes.toBytes("8"));
        Result result = table.get(get);
        for (Cell c : result.rawCells()) {
            sb = new StringBuffer(Bytes.toString(CellUtil.cloneRow(c)));
            sb.append("==> ");
            sb.append(Bytes.toString(CellUtil.cloneFamily(c)));
            sb.append("{");
            sb.append(Bytes.toString(CellUtil.cloneQualifier(c)));
            sb.append(":");
            sb.append(Bytes.toString(CellUtil.cloneValue(c)));
            sb.append("}");
            System.out.println(sb);
        }
    }

    @Test
    public void scanTest() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("test");
        Table table = connection.getTable(tableName);

        StringBuffer sb = null;
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result res : scanner) {
            for (Cell c : res.rawCells()) {
                sb = new StringBuffer(Bytes.toString(CellUtil.cloneRow(c)));
                sb.append(" ==> ");
                sb.append(Bytes.toString(CellUtil.cloneFamily(c)));
                sb.append(" {");
                sb.append(Bytes.toString(CellUtil.cloneQualifier(c)));
                sb.append(":");
                sb.append(Bytes.toString(CellUtil.cloneValue(c)));
                sb.append("}");
                System.out.println(sb.toString());
            }
        }
    }

    @Test
    public void modifyTest() throws IOException{
        Configuration conf = HBaseConfiguration.create();
        try (Connection connection = ConnectionFactory.createConnection(conf);
             Admin admin = connection.getAdmin()) {

            TableName tableName = TableName.valueOf("test");
            if (!admin.tableExists(tableName)) {
                System.out.println("表不存在");
                System.exit(-1);
            }

            HTableDescriptor table = new HTableDescriptor(tableName);

            // add family
//            HColumnDescriptor newColumn = new HColumnDescriptor("testinfo");
//            newColumn.setCompactionCompressionType(Compression.Algorithm.GZ);
//            newColumn.setMaxVersions(HConstants.ALL_VERSIONS);
//            admin.addColumn(tableName, newColumn);

            // Update existing column family
            HColumnDescriptor existingColumn = new HColumnDescriptor("info");
            existingColumn.setCompactionCompressionType(Compression.Algorithm.GZ);
            existingColumn.setMaxVersions(HConstants.ALL_VERSIONS);
            table.modifyFamily(existingColumn);
            admin.modifyTable(tableName, table);

            // Disable an existing table
//            admin.disableTable(tableName);

            // Delete an existing column family
//            admin.deleteColumn(tableName, "testinfo".getBytes("UTF-8"));

            // Delete a table (Need to be disabled first)
//            admin.deleteTable(tableName);
        }
    }

    @Test
    public void deleteColumnDataTest() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("test");
        Table table = connection.getTable(tableName);
        Delete delete = new Delete(Bytes.toBytes("8"));
        table.delete(delete);
        System.out.println("删除数据成功");
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值