HBase学习四之JAVA编程

导入pom

     <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.1.0</version>
        </dependency>

创建表

HBase 初始化

构建一个Configuration实例,该实例包含了一些客户端配置项,最重要的必须的两个配置项是HBase集群中的ZooKeeper地址与接口。配置cfg,使客户端连接到ZooKeeper,从而和HBase取得连接

ConnectionFactory根据Configuration实例创建了一个Connection对象该Connection对象线程安全,封装了连接到一个HBase集群所需要的所有信息,如元数据缓存,客户端与HMaster和HRegionServer的连接等。

从Connection获取一个Admin实例,获得管理权,实现创建表、删除表等操作。

 Java 客户端配置

java 客户端使用的配置保存在HBaseConfiguration实例中。

HBaseConfiguration 的工厂方法,HBaseConfiguration.create();,在调用时会读取客户端上的第一个 hbase-site.xml 的内容(CLASSPATH如果存在的话)(调用也将包含在任何发现的 hbase-default.xml 中;hbase-default.xml 在 hbase.X.X.X.jar 里面)。也可以直接指定配置,而无需从 hbase-site.xml 中读取数据。例如,要以编程方式设置集群的 ZooKeeper 集成,请执行以下操作:

  •  设置配置信息
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "localhost");  // Here we are running zookeeper locally
  • 根据配置信息取得与数据库的连接
    Connection connection = ConnectionFactory.createConnection(configuration);
  • 通过getAdmin()方法获得管理权
    Admin admin = connection.getAdmin();

  完整代码 

@RunWith(SpringRunner.class)
@SpringBootTest(classes = WebApplication.class)
public class HBaseTableTest {

    private Configuration configuration;

    private Connection connection;

    private Admin admin;

    @Before
    public void init() throws Exception {
        configuration = HBaseConfiguration.create();

        configuration.set(HConstants.ZOOKEEPER_QUORUM, "127.0.0.1:2181");
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    }


    @After
    public void close() throws Exception {
        admin.close();
        connection.close();
    }
}

创建表

    /**
     * 创建表
     */
    @Test
    public void testCreateTable() throws Exception {
        TableName tableName = TableName.valueOf("student");

        // 表是否存在
        if(admin.tableExists(tableName)){
            return;
        }

        // 构建表描述构建器
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
        // 构建列簇描述构建器
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"));

        // 构建列簇描述
        ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();
        // 添加列簇
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
        // 构建表描述
        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();

        // 创建表
        admin.createTable(tableDescriptor);
    }

修改表

    /**
     * 修改表
     */
    @Test
    public void testUpdateTable() throws Exception {
        TableName tableName = TableName.valueOf("student");

        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);

        // 构建列簇描述构建器
        ColumnFamilyDescriptorBuilder infoColumnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"));
        ColumnFamilyDescriptorBuilder courseColumnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("course"));


        ColumnFamilyDescriptor infoColumnFamilyDescriptor = infoColumnFamilyDescriptorBuilder.build();
        ColumnFamilyDescriptor courseColumnFamilyDescriptor = courseColumnFamilyDescriptorBuilder.build();

        Collection<ColumnFamilyDescriptor> families = Arrays.asList(infoColumnFamilyDescriptor, courseColumnFamilyDescriptor);
        tableDescriptorBuilder.setColumnFamilies(families);

        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();

        admin.modifyTable(tableDescriptor);
    }

删除表

    /**
     * 删除表
     */
    @Test
    public void testDelTable() throws Exception {
        TableName tableName = TableName.valueOf("student");

        // 表是否存在
        if (!admin.tableExists(tableName)) {
            return;
        }

        // 禁用表
        admin.disableTable(tableName);
        // 删除表
        admin.deleteTable(tableName);
    }

数据操作

HBase 初始化

@SpringBootTest(classes = WebApplication.class)
@RunWith(SpringRunner.class)
public class HBaseDataTest {

    private Configuration configuration;

    private Connection connection;

    /**
     * 初始化数据
     */
    @Before
    public void init() throws Exception {
        org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
        conf.set(HConstants.ZOOKEEPER_QUORUM, "127.0.0.1:2181");
        configuration = conf;

        connection = ConnectionFactory.createConnection(configuration);
    }


    @Test
    public void close() throws Exception {
        connection.close();
    }
}

插入数据

 /**
     * 新增数据
     */
    @Test
    public void testInsert() throws Exception {
        TableName tableName = TableName.valueOf("student");
        Table table = connection.getTable(tableName);

        String rowKey = "0001";
        String familyName = "info";
        String columnName = "name";

        //构建Put对象,对应put命令
        Put put = new Put(Bytes.toBytes(rowKey));
        // 添加列
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes("tom"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("gender"), Bytes.toBytes("man"));
        //将数据插入数据库
        table.put(put);

        table.close();
    }

  shell 命令查看数据

hbase:020:0> get 'student','0001'
COLUMN                                        CELL                                                                                                                                 
 info:gender                                  timestamp=2022-06-13T21:26:16.119, value=man                                                                                         
 info:name                                    timestamp=2022-06-13T21:26:16.119, value=tom                                                                                         
1 row(s)
Took 0.0177 seconds   

查询数据 

    /**
     * 查询数据
     */
    @Test
    public void testQuery() throws Exception {
        TableName tableName = TableName.valueOf("student");
        Table table = connection.getTable(tableName);

        Get get = new Get(Bytes.toBytes("0001"));
        Result result = table.get(get);
        List<Cell> cells = result.listCells();

        System.out.println("rowKey= " + Bytes.toString(result.getRow()));

        // 遍历单元格
        for (Cell cell : cells) {
            System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));

            System.out.println(Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
        }

        table.close();
    }

批量查询 

     /**
     * 批量查询
     */
    @Test
    public void testScan() throws Exception {
        TableName tableName = TableName.valueOf("student");
        Table table = connection.getTable(tableName);

        Scan scan = new Scan();
        SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("name"), CompareOperator.GREATER, Bytes.toBytes("jack"));

        scan.setFilter(filter);
        ResultScanner results = table.getScanner(scan);
        for (Result result : results) {
            System.out.println("rowKey ->" + Bytes.toString(result.getRow()));

            List<Cell> cells = result.listCells();
            // 遍历单元格
            for (Cell cell : cells) {
                System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));

                System.out.println(Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
            }
        }

        results.close();
        table.close();
    }

编辑数据

    /**
     * 编辑数据
     */
    @Test
    public void testUpdate() throws Exception {
        TableName tableName = TableName.valueOf("student");
        Table table = connection.getTable(tableName);

        String rowKey = "0001";
        String familyName = "info";
        String columnName = "name";

        //构建Put对象,对应put命令
        Put put = new Put(Bytes.toBytes(rowKey));
        // 添加列
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes("jack"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("gender"), Bytes.toBytes("man"));

        put.addColumn(Bytes.toBytes("course"), Bytes.toBytes("chinese"), Bytes.toBytes("100"));
        //将数据插入数据库
        table.put(put);

        table.close();
    }

  shell 命令查看

hbase:021:0> get 'student','0001'
COLUMN                                        CELL                                                                                                                                 
 course:chinese                               timestamp=2022-06-13T21:28:26.169, value=100                                                                                         
 info:gender                                  timestamp=2022-06-13T21:28:26.169, value=man                                                                                         
 info:name                                    timestamp=2022-06-13T21:28:26.169, value=jack                                                                                        
1 row(s)
Took 0.0633 seconds  

删除数据

    /**
     * 删除数据
     */
    @Test
    public void testDel() throws Exception {
        TableName tableName = TableName.valueOf("student");
        Table table = connection.getTable(tableName);

        Delete delete = new Delete(Bytes.toBytes("0001"));
        table.delete(delete);

        table.close();
    }

 使用shell 命令查看

hbase:022:0> get 'student','0001'
COLUMN                                        CELL                                                                                                                                 
0 row(s)
Took 0.0318 seconds   

参考

Apache HBase APIs-HBase中文参考指南 3.0

猜你喜欢

转载自blog.csdn.net/yangyanping20108/article/details/125266603
今日推荐