使用Java代码实现HBase中的API

首先导入pom依赖:

 <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.12</version>
             <scope>test</scope>
         </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
         <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.17</version>
         </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/jdk.tools/jdk.tools -->
        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <!--            <systemPath>C:/Program Files/Java/jdk1.8.0_101/lib/tools.jar</systemPath>-->
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
    </dependencies>

demo:

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.Test;

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

public class MyTest {

    private Connection con = null;
    //获取HBase的连接
    public Connection getConnection() throws IOException {
        //获取配置信息
        Configuration configuration = HBaseConfiguration.create();
        //建立连接
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection;
    }

    @Test
    //创建表
    public void createTable() throws IOException {
        //1.获取Connection
        con = getConnection();
        //2.使用连接获得Admin(创建表需要Admin)
        Admin admin = con.getAdmin();
        //3.使用Admin创建一个表
        //创建一个表需要一个表名  TableName
        TableName tableName = TableName.valueOf("bigdata");
        //再创建这个表的描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        //通过表的描述器  设置表的列簇
        //首先创建一个列簇描述器
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("info");
        //再通过列簇描述器创建列簇
        hTableDescriptor.addFamily(hColumnDescriptor);
        //创建表
        admin.createTable(hTableDescriptor);
    }

    @Test
    //查看表的详情
    public void descTable() throws IOException {
        //1.获取连接
        con = getConnection();
        //2.根据连接获取表
        TableName tableName = TableName.valueOf("bigdata");
        Table table = con.getTable(tableName);
        //3.根据表获得表的描述器
        HTableDescriptor tableDescriptor = table.getTableDescriptor();
        //根据描述器获得列簇描述器
        HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
        for(HColumnDescriptor t : columnFamilies){
            //获得列簇的名称
            byte[] name = t.getName();
            //注意 HBase中的数据类型Java不识别  必须要转化为Java类型的
            System.out.println("列簇的名称为:"+ Bytes.toString(name));
        }
        //关闭表
        table.close();
    }

    @Test
    //向表中添加数据
    public void putTable() throws IOException {
        //1.获取连接
        con = getConnection();
        //2.获取表
        TableName tableName = TableName.valueOf("bigdata");
        Table table = con.getTable(tableName);
        //3.向表里面放数据
        //需要将数据封装成一个Put(rowkey,列簇,列限定符,value)
        String rowkey = "1";
        String colFamily = "info";
        String colName = "name";
        String value = "habaoxin";
        //Put初始化时需要设置一下rowkey
        Put put = new Put(Bytes.toBytes(rowkey));
        //将列簇、列限定符和value放入Put中
        put.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(colName),Bytes.toBytes(value));
        table.put(put);
        //关闭表
        table.close();
    }

    @Test
    //取表中的一行数据
    public void getTable() throws IOException {
        //1.获取连接
        con = getConnection();
        //2.获取表
        TableName tableName = TableName.valueOf("bigdata");
        Table table = con.getTable(tableName);
        //3.从表中获取某一行的数据
        //Get初始化的时候要设置rowkey的值
        String rowKey = "1";
        Get get = new Get(Bytes.toBytes(rowKey));
        //获得结果Result
        Result result = table.get(get);
        //4.获取这一行中具体的值
        //获取这一行中的所有单元格
        List<Cell> cells = result.listCells();
        for(Cell cell : cells){
            //从cell里面获取列限定符和value
            //获得列限定符
            byte[] qualifier = CellUtil.cloneQualifier(cell);
            //获得列的value
            byte[] values = CellUtil.cloneValue(cell);
            System.out.println("get查询到的列限定符为" + Bytes.toString(qualifier));
            System.out.println("get查询到的值为" + Bytes.toString(values));
        }
    }

    @Test
    //查询表中的数据
    public void scanTable() throws IOException {
        //1.获取连接
        con = getConnection();
        //2.获得表
        TableName tableName = TableName.valueOf("bigdata");
        Table table = con.getTable(tableName);
        //3.获取表的扫描器
        Scan scan = new Scan();
        //Scan有很多种过滤方式
        //例如 : 设置startRowKey和stopRowKey
        scan.setStartRow(Bytes.toBytes("1"));
        scan.setStopRow(Bytes.toBytes("10"));
        ResultScanner scanner = table.getScanner(scan);
        //4.通过扫描器 逐行扫描整张表
        Result result = null;
        //遍历每一行
        while((result = scanner.next()) != null){
            //获取该行的所有单元格
            List<Cell> cells = result.listCells();
            for(Cell cell : cells){
                //获取这行的rowKey
                byte[] rowKey = CellUtil.cloneRow(cell);
                //获得这行的列簇
                byte[] colFamily = CellUtil.cloneFamily(cell);
                //获得这行的列限定符
                byte[] qualifier = CellUtil.cloneQualifier(cell);
                //获得这行的Value
                byte[] value = CellUtil.cloneValue(cell);
                System.out.println("rowKey = " + Bytes.toString(rowKey) + "  colFamily = " + Bytes.toString(colFamily)
                + "  qualifies = " + Bytes.toString(qualifier) + "  value = " + Bytes.toString(value));
            }
        }
    }

    @Test
    //表的分区
    public void splitTable(){

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值