Hbase建新表,查询数据[超详细的过程]

清除hbase的一张表数据可以参考下面的文章 

https://blog.youkuaiyun.com/LL9504/article/details/98206857

最近有空总结一下对hbase的操作,下面是Java 新建表,以及新增数据和查看数据等

    public static void main(String[] args) throws IOException {
        ResourceBundle resourceBundle= ResourceBundle.getBundle("hbase");

        //初始化Hbase链接
        Configuration configuration= HBaseConfiguration.create();
        configuration.set("zookeeperUrl",resourceBundle.getString("resourceBundle"));
        configuration.set("hbaseMaster",resourceBundle.getString("hbaseMaster"));
        configuration.set("zookeeperPort",resourceBundle.getString("zookeeperPort"));
        configuration.set("threadMax",resourceBundle.getString("threadMax"));
        configuration.set("KeyValueMaxSize",resourceBundle.getString("KeyValueMaxSize"));
        Connection connection=null;
        try {
             connection = ConnectionFactory.createConnection(configuration);
            //设置表名
            TableName tableName = TableName.valueOf("HbaseTestDemo");
            //将表名传递给HTableDescriptor
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            //创建列簇,这里只创建一个列簇,如果需要多个列簇,创建后都需要执行hTableDescriptor.addFamily();
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(Bytes.toBytes("cf"));
            //将列簇添加到表中
            hTableDescriptor.addFamily(hColumnDescriptor);
            //Admin是操作表的类,具有创建
            Admin admin = connection.getAdmin();
            //admin.createTable(hTableDescriptor);
            if(admin.tableExists(hTableDescriptor.getTableName())){
                admin.disableTable(hTableDescriptor.getTableName());
                admin.deleteTable(hTableDescriptor.getTableName());
            }
            admin.createTable(hTableDescriptor);

            //表已经建好,接下来向表中添加数据
            Table table = connection.getTable(tableName);
            Put put = new Put(Bytes.toBytes("2651564541515151"));//2651564541515151是rowkey
            put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("name"),Bytes.toBytes("小明"));// 列簇  列   值
            put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("age"),Bytes.toBytes("25"));// 列簇  列   值
            //多个列簇时情况类似写即可
            //存储过程结束
            table.put(put);

            //*******************结束****************************
            //如果想要查询某一条数据
            Get get = new Get(Bytes.toBytes("2651564541515151"));//获取rowkey
            byte [] data =table.get(get).getValue(Bytes.toBytes("cf"),Bytes.toBytes("name"));
            System.out.println("name: "+new String(data));

            //查询多条数据
            Scan scan = new Scan();
            scan.setStartRow(Bytes.toBytes("001"));
            scan.setStopRow(Bytes.toBytes("003"));//输出 rowkey 001 002
            scan.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("name"));
            scan.setCaching(20);
            ResultScanner resultScanner=table.getScanner(scan);
            for (Result result : resultScanner) {
                //循环输出
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭连接
            if(connection !=null){
               connection.close(); 
            }
        }
    }

参考博客:

https://www.cnblogs.com/xuehu666/p/10599741.html

https://blog.youkuaiyun.com/u010919083/article/details/99618115

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值