java操作Hbase API

前提条件,启动hadoop集群,zookeeper集群,Hbase集群,访问hbase的web页面

 

 

 

<!--添加hbase相关依赖-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>1.2.1</version>
        </dependency>
package ning.test.hadoop;

import lombok.SneakyThrows;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

/**
 * 操作hbase  java-API
 */

public class HbaseTest {

    private static Admin admin;
    private static Connection connection;

    @Before
    @SneakyThrows
    public void initHbase() {
        System.out.println("------------->>>>>>>>before<<<<<<<<-----------------");
        Configuration configuration = HBaseConfiguration.create();
        //configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("hbase.zookeeper.quorum", "192.168.72.136");
        //集群配置↓
        configuration.set("hbase.master", "192.168.72.136,192.168.72.137,192.168.72.138");
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    }

    @Test
    @SneakyThrows
    public void createTable() {
        String tableNmae = "table_dong";
        String[] cols = new String[]{"information", "contact", "meet", "teacher"};
        TableName tableName = TableName.valueOf(tableNmae);
        if (admin.tableExists(tableName)) {
            System.out.println("表已存在!");
        } else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String col : cols) {
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
            System.out.println("----------------->>>>>>>>>>finish<<<<<<<<<<-----------------");
        }
    }

    @Test
    @SneakyThrows
    public void insertData() {
        TableName tablename = TableName.valueOf("table_dong");
        Put put = new Put(("ning-" + "123456789").getBytes());
        //参数:1.列族名  2.列名  3.值
        put.addColumn("information".getBytes(), "age".getBytes(), "1".getBytes());
        put.addColumn("contact".getBytes(), "gender".getBytes(), "男".getBytes());
        put.addColumn("meet".getBytes(), "phone".getBytes(), "15076500954".getBytes());
        put.addColumn("teacher".getBytes(), "name".getBytes(), "张老师".toString().getBytes());
        Table table = connection.getTable(tablename);
        table.put(put);
    }


    //获取原始数据
    @Test
    @SneakyThrows
    public void getTableData() {
        Table table = connection.getTable(TableName.valueOf("table_dong"));
        Scan scan = new Scan();
        ResultScanner resutScanner = table.getScanner(scan);
        for (Result result : resutScanner) {
            System.out.println("scan:  " + result);
        }
    }

    //查询指定表名中所有的数据
    @Test
    @SneakyThrows
    public void getAllData() {
        String tableName = "table_dong";
        Table table = connection.getTable(TableName.valueOf(tableName));
        ResultScanner results = table.getScanner(new Scan());
        for (Result result : results) {
            String id = new String(result.getRow());
            System.out.println("rowKey值:" + new String(result.getRow()));
            for (Cell cell : result.rawCells()) {
                String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                //String family =  Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
                String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println("colName------>>>>" + colName + "<<<<------");
                System.out.println("value------>>>>" + value + "<<<<------");
            }
        }

    }

    //根据rowKey进行查询
    @Test
    @SneakyThrows
    public void getDataByRowKey() {
        String rowKey = "ning-123456789";
        //String rowKey = "user-a782d04b-8264-4118-8576-af2e8f2c349a";
        Table table = connection.getTable(TableName.valueOf("table_dong"));
        Get get = new Get(rowKey.getBytes());
        //先判断是否有此条数据
        if (!get.isCheckExistenceOnly()) {
            Result result = table.get(get);
            for (Cell cell : result.rawCells()) {
                String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println(value);
            }
        }
    }


    //查询指定cell内容
    @Test
    @SneakyThrows
    public void getCellData() {
        String tableName = "table_dong";
        String rowKey = "ning-123456789";
        String family = "meet";
        String col = "phone";
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        if (!get.isCheckExistenceOnly()) {
            get.addColumn(Bytes.toBytes(family), Bytes.toBytes(col));
            Result res = table.get(get);
            byte[] resByte = res.getValue(Bytes.toBytes(family), Bytes.toBytes(col));
            System.out.println(Bytes.toString(resByte));
        } else {
            System.out.println("暂无数据");
        }
    }


    //删除表名下,对应的rowKey下的值,或者删除rowKey
    @Test
    @SneakyThrows
    public void deleteByRowKey() {
        String tableName = "table_dong";
        String rowKey = "ning-123456789";
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        //删除指定列
        //delete.addColumns(Bytes.toBytes("meet"), Bytes.toBytes("email"));
        table.delete(delete);
    }

    //删除表
    @Test
    @SneakyThrows
    public void deleteTable() {
        String tableName = "table_dong";
        TableName tablename = TableName.valueOf(tableName);
        admin.disableTable(tablename);
        admin.deleteTable(tablename);
        System.out.println("删除操作执行完毕!");
    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值