HBase的java api应用

1、新建一个IDEA的Maven工程


2、引入依赖

    


3、新建测试类

     

package com.motoon;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

/**
 * @author rjsong
 */
public class HBaseTest {
    public static void main( String[] args )
    {
        // 建立连接
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://192.168.61.138:9000/hbase");

        Connection conn = null;
        Admin admin = null;
        try {
            conn = ConnectionFactory.createConnection(conf);
            admin = conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String TABLE_NAME = "test";
        String COLUMN_FAMILY_NAME = "columnfamily";
        String COLUMN_FAMILY_NAME2 = "columnfamily1";
        String[] COLUMNS = {"column0", "column1", "column2"};
        // Some data
        String ROW_KEYS = "row0";
        String[] VALUES = {"value0", "value1", "value2"};

        System.out.println("CREATE TABLE ==========================");
        // 创建表
        TableName tableName = TableName.valueOf(TABLE_NAME);
        try {
            // 存在则删除
            if (admin.tableExists(tableName)) {
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
            }
            TableDescriptor descriptor = TableDescriptorBuilder.newBuilder(tableName)
                    .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(COLUMN_FAMILY_NAME.getBytes()).build())
                    .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(COLUMN_FAMILY_NAME2.getBytes()).build())
                    .build();
            admin.createTable(descriptor);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("PUT VALUE ==========================");
        // 添加数据
        try {
            Table table = conn.getTable(tableName);
            Put put = new Put(ROW_KEYS.getBytes());
            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[0].getBytes(), VALUES[0].getBytes());
            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[1].getBytes(), VALUES[1].getBytes());
            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[2].getBytes(), VALUES[2].getBytes());
            table.put(put);
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("DELETE VALUE ==========================");
        // 删除数据: 删除row0column2        try {
            Table table = conn.getTable(tableName);
            Delete delete = new Delete(ROW_KEYS.getBytes());
            delete.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[2].getBytes());
            table.delete(delete);
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("GET VALUE ==========================");
        // 查询数据
        try {
            Table table = conn.getTable(tableName);
            Get get = new Get(ROW_KEYS.getBytes());
            //get.addFamily(COLUMN_FAMILY_NAME.getBytes());                         // 获取特定列族的数据
            //get.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[1].getBytes());  // 获取特定列的数据
            Result result = table.get(get);
            // get column family
            result.getFamilyMap(COLUMN_FAMILY_NAME.getBytes()).forEach((k,v) -> System.out.println(new String(k) + ":" + new String(v)));
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭连接
        try {
            if (admin != null) {
                admin.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值