大数据之Hbase[JavaAPI-CRUD]

package com.sanmao.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.*;

/**
 * HBase的相关java API 操作
 */
public class HbaseCRUDTest {
    private Admin admin;
    private Configuration configuration;
    private Connection connection;
    @Before
    public void setup() throws IOException {
        //获取配置信息
        configuration = new Configuration();
        //设置配置信息
//        自动加载 resources下的hbase-site.xml
//        也可以手动配置
        configuration.set("hbase.rootdir","hdfs://master:9000/hbase");
        configuration.set("hbase.zookeeper.quorum","master,slave01,slave02");
        //根据配置文件,创建连接
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
//        System.out.println(admin);
    }
    //获取Hbase数据库中,表的信息
    @Test
    public void testList() throws IOException {
        TableName[] tableNames = admin.listTableNames();
        System.out.println(Arrays.toString(tableNames));
        HTableDescriptor[] htds = admin.listTables();
        for (HTableDescriptor htd:htds) {
            TableName tn =htd.getTableName();
            System.out.println(tn + " ");
        }
    }
    /**
     * 创建一个表
     * */
    @Test
    public void testAddTable() throws IOException {
        System.out.println("=----------testAddTable---------------------");
        //创建一个表名称 sanmao
        TableName tn = TableName.valueOf("sanmao1");
        //构造一个表的描述器
        HTableDescriptor htd = new HTableDescriptor(tn);
        //列族描述描述器
        HColumnDescriptor family = new HColumnDescriptor("cf");
        //添加列族
        htd.addFamily(family);
        //创建表
        admin.createTable(htd);
    }
    /**
     * 往表中添加记录信息
     * */
    @Test
    public void testAddRecord() throws IOException {
        Table table = connection.getTable(TableName.valueOf("sanmao1"));
        byte[] rowkey = "1".getBytes();
        Put put = new Put(rowkey);
        byte[] family = "cf".getBytes();
        byte[] qualifier = "name".getBytes();
        byte[] value = "maomao".getBytes();
        put.addColumn(family,qualifier,value);
        byte[] qualifier1 = "age".getBytes();
        byte[] value1="18".getBytes();
        put.addColumn(family,qualifier1,value1);
        table.put(put);
    }
    /**
     * 获取其中一条记录
     * 列族
     * 列
     * 值
     * */
    @Test
    public void testQueryRecord() throws IOException {
        Table table = connection.getTable(TableName.valueOf("sanmao1"));
        String rowkey="1";
        Get get = new Get(rowkey.getBytes());
        Result result =  table.get(get);
        List<Cell> cells = result.listCells();
        for (Cell cell:cells){
             Map<String,String> map = getCell(cell);
             for (Map.Entry<String,String> me : map.entrySet()){
                 System.out.println(me.getKey()+"=="+me.getValue());
             }
        }
        System.out.println("--------------------------");
    }

    //从HFile 上提取cell 详细信息
    public Map<String,String> getCell(Cell cell){
        Map<String,String> map = new TreeMap<String, String>(new Comparator<String>() {
            public int compare(String o1, String o2) {
                return 1;//按存放顺序输出
            }
        });
        String rk = new String(cell.getRowArray()).substring(cell.getRowOffset(),cell.getRowOffset()+cell.getRowLength());
        String cf = new String(cell.getFamilyArray()).substring(cell.getQualifierOffset(),cell.getQualifierOffset()+cell.getQualifierLength());
        String qualifier = new String(cell.getQualifierArray()).substring(cell.getQualifierOffset(),cell.getQualifierOffset()+cell.getQualifierLength());
        String value = new String(cell.getValueArray()).substring(cell.getValueOffset(),cell.getValueOffset()+cell.getValueLength());
        map.put("rk",rk);
        map.put("cf",cf);
        map.put("cq",qualifier);
        map.put("v",value);
        return map;
    }
    /**
     * 显示Result内容
     * */
    private void showResult(Result result){
        List<Cell> cells = result.listCells();
        for (Cell cell : cells){
            Map<String,String> map = getCell(cell);
            for (Map.Entry<String,String> me : map.entrySet()){
                System.out.println(me.getValue()+" ");
            }
            System.out.println();
        }
    }
    /**
     * 获取全表记录
     * 列族
     * 列
     * 值
     * */
    @Test
    public void testQueryAllRecord() throws IOException {
        Table table = connection.getTable(TableName.valueOf("sanmao1"));
        Scan scan = new Scan();
        ResultScanner rs = table.getScanner(scan);
        for (Result result : rs){
            showResult(result);
        }
        table.close();
    }
    /**
     * 添加过滤器查询(条件查询)
     * */
    @Test
    public void testQueryByCodition() throws IOException {
        Table table = connection.getTable(TableName.valueOf("sanmao1"));
        Scan scan = new Scan();
        byte[] family = "cf".getBytes();
        byte[] qualifer = "age".getBytes();
        /**
         *  family 指定要在那个列族上进行数据过滤
         *  qualifier 指定要在哪个列族上的列进行数据过滤
         *  CompareOp 具体要比较的方式
         *  ByteArrayComparable 要和谁【字节数组】进行比较
         * */
         Filter filter = new SingleColumnValueExcludeFilter(family,qualifer,
                 CompareFilter.CompareOp.LESS_OR_EQUAL,"18".getBytes());
         scan.setFilter(filter);
        ResultScanner rs= table.getScanner(scan);
        for (Result result :rs){
            showResult(result);
        }
        table.close();
    }
    @Test
    public void testDropTable() throws IOException {
        TableName tn = TableName.valueOf("sanmao1");
        if (admin.tableExists(tn)){//判断表是否存在
            if (!admin.isTableDisabled(tn)){
                //判断表状态是否是disable
                admin.disableTable(tn);
            }
            admin.deleteTable(tn);
        }
    }
    @After
    public void cleanup() throws IOException {
        admin.close();
        connection.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值