Hbase -案例2- 将数据导入hbase中、利用api完成在hbase建立命名空间(建立表和列簇)

数据准备:

myuser.txt
id	name	age
1	bing	20
2	zhangfei	30
3	maliu	40
4	wangwu	50
5	xiaoli	50

题目:

  1. 创建hive表myuser
  2. 导入hbase表myuser(列簇名字base_info)
  3. 通过hbaseapi直接完成在hbase中创建表myuser1(列簇base_info) 、添加数据到表中

解答:

  1、 创建hive表myuser   -- 注:

#创建临时表 

create table if not exists myuser_temp(
id int,
name string,
age int
)
row format delimited fields terminated by '\t'
lines terminated by '\n';

#加载数据到临时表中 

load data local inpath '/usr/local/hive/hivedata/myuser.txt' into table myuser_temp;

#创建myuser表,并在hbase中建立

create table if not exists myuser1 (
id int,
name string,
age int
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties(
"hbase.columns.mapping"=":key,base_info:name,base_info:age"
)
tblproperties(
"hbase.table.name"="myuser_namespace:myuser1"
);

#加载数据到hbase的表中 

 注:这里不能使用load方法 因为hbase表中还没有建立

而是应该使用 insert into 

insert into myuser1
select * from myuser_temp;

2、3题合在一起 在下面代码中

 #创建一个HbaseUtils 工具类

public class HBaseUtils {

    private static final Logger logger = Logger.getLogger(HBaseUtils.class);

    private final static String CONNECT_KEY = "hbase.zookeeper.quorum";
    private final static String CONNECT_VALUE = "master:2181,slave1:2181,slave2:2181";
    private static Connection connection;


    static {
        //1. 获取连接配置对象
        Configuration configuration = HBaseConfiguration.create();
        //2. 设置连接hbase的参数
        configuration.set(CONNECT_KEY, CONNECT_VALUE);
        //3. 获取connection对象
        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            logger.warn("连接HBase的时候异常!", e);
        }
    }

    /**
     * 获取Admin对象
     */
    public static Admin getAdmin() {
        Admin admin = null;
        try {
            admin = connection.getAdmin();
        } catch (IOException e) {
            logger.warn("连接HBase的时候异常!", e);
        }
        return admin;
    }

    public static void close(Admin admin) {
        if(null != admin) {
            try {
                admin.close();
            } catch (IOException e) {
                logger.warn("关闭admin的时候异常!", e);
            }
        }
    }

    public static Table getTable() {
        return getTable("myuser_namespace1:myuser1");
    }

    public static Table getTable(String tablename) {
        Table table = null;
        if(StringUtils.isNotEmpty(tablename)) {
            try {
                table = connection.getTable(TableName.valueOf(tablename));
            } catch (IOException e) {
                logger.warn("获取表产生异常!", e);
            }
        }
        return table;
    }

    public static void close(Table table) {
        if(table != null) {
            try {
                table.close();
            } catch (IOException e) {
                logger.warn("关闭table的时候产生异常!", e);
            }
        }
    }
}

 #创建连接Hbase的类、并实现java代码对hbase的操作

/**
 * 1、准备数据
 * 2、在hbase上创建命名空间myuser_namespace1,并创建myuser1.添加列簇base_info
 * 3、完成上传
 */

public class Myuser_Conn1 {

//    private static HBaseAdmin hBaseAdmin = null;

    //1、读取数据
    public static List<String> getData() throws IOException {

       return Files.readAllLines(Paths.get("F:\\ideaUProgram\\HbaseTest\\src\\data\\myuser.txt"));

    }

//    @Before
//    //创建连接
    public void connectHbase() throws IOException {
        //1、获取连接配置对象
        Configuration configuration = new Configuration();
        //2、设置连接hbase的参数
//        configuration.set("hbase.zookeeper.quorum", "master,slave1,slave2");
        //3、获取Admin对象
        HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);
//        Connection connection = ConnectionFactory.createConnection(configuration);
//        Admin admin = connection.getAdmin();
        //4、检验指定表是否存在,来判断是否连接到hbase  -- 仅仅是测试
//        boolean flag = admin.tableExists(TableName.valueOf("myuser_namespace:myuser1"));
//        System.out.println(flag);

    }

//    @After
//    public void close() throws IOException {
//        hBaseAdmin.close();
//    }

    @Test
    //3、在hbase上创建命名空间myuser_namespace1,并创建myuser1.添加列簇base_info
    public void createNamespace() throws IOException {
        //1、创建namespace对象
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("myuser_namespace1").build();
        //2. 提交hbase中创建对象 -- 最终还是admin对象来干活
        HBaseUtils.getAdmin().createNamespace(namespaceDescriptor);
    }

    @Test
    //3、创建表
    public void createTable() throws IOException {
        //1、创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor("myuser_namespace1:myuser1");
        //2、创建列簇描述器
        HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("base_info");
        //3、将列簇添加到表中
        hTableDescriptor.addFamily(hColumnDescriptor);

        //4、提交到hbase
        HBaseUtils.getAdmin().createTable(hTableDescriptor);
    }

    @Test
    //4、完成上传 -- 添加数据
    public void addData() throws IOException {

        //1. 获取Table对象
        Table table = HBaseUtils.getTable();

        //2. 获取Put对象,通过rowkey指定
        Put put = new Put(Bytes.toBytes("1001"));

        //3. 设置插入的数据 -- 列簇、列名、value
        put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
        put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("15"));
        put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("sex"), Bytes.toBytes("male"));

        //4、提交
        table.put(put);

        HBaseUtils.close(table);
    }

    //批量添加数据
    @Test
    public void batchaddDatas() throws IOException {
        //获取数据
        List<String> list = getData();

        //获取表对象
        Table table = HBaseUtils.getTable();

        //获取所有的Put对象,代表加入的数据
        List<Put> putList = new ArrayList<>();

        for (String s : list) {
            String[] strings = s.split("\t");

            Put put = new Put(Bytes.toBytes(strings[0]));

            put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes(strings[1]));
            put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes(strings[2]));

            putList.add(put);
        }
        table.put(putList);


        System.out.println("批量上传成功!");
        HBaseUtils.close(table);
    }

    /**
     * 列举所有的namespace
     * @throws IOException
     */
    @Test
    public void listNamespace() throws IOException {
        Admin admin = HBaseUtils.getAdmin();
        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
        for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
            System.out.println(namespaceDescriptor);
        }

        HBaseUtils.close(admin);
    }

    /**
     * 列举Namespace对应的表名
     * @throws IOException
     */
    @Test
    public void listNamespaceTables() throws IOException {
        Admin admin = HBaseUtils.getAdmin();

        TableName[] tableNames = admin.listTableNamesByNamespace("myuser_namespace1");

        for (TableName tableName : tableNames) {

            System.out.println(tableName.getNameAsString());

        }
        HBaseUtils.close(admin);
    }

    /**
     * 列举所有namesacpe对应表描述器
     */
    @Test
    public void listAllNamespaceTableDescriptor() throws IOException {
        Admin admin = HBaseUtils.getAdmin();

        HTableDescriptor[] tableDescriptors = admin.listTableDescriptorsByNamespace("myuser_namespace1");

        for (HTableDescriptor tableDescriptor : tableDescriptors) {
            System.out.println(tableDescriptor.getTableName());
        }
        HBaseUtils.close(admin);

    }

}

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值