java操作cloudera版的hbase数据库

gradle引入依赖:

repositories {
    maven{ url 'https://repository.cloudera.com/artifactory/cloudera-repos/'}
    maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}

ext {
    hbaseVersion = "1.2.0-cdh5.16.2" //hbase版本号
}

dependencies {
    implementation "org.apache.hbase:hbase-client:$hbaseVersion"
    implementation "org.apache.hbase:hbase-server:$hbaseVersion"
}

获取表

    public static Table getTable(String name) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(configuration);
        TableName tablename = TableName.valueOf(name);
        Table table = connection.getTable(tablename);
        return table;
    }

插入数据:

    public static void insertData(Table table) throws IOException {
        Put put = new Put("1001".getBytes());
        put.addColumn("info".getBytes(), "name".getBytes(), "fanghailiang".getBytes());
        put.addColumn("info".getBytes(), "age".getBytes(), "27".getBytes());
        put.addColumn("info".getBytes(), "sex".getBytes(), "mail".getBytes());
        table.put(put);

        put = new Put("1002".getBytes());
        put.addColumn("info".getBytes(), "name".getBytes(), "sunyu".getBytes());
        put.addColumn("info".getBytes(), "age".getBytes(), "26".getBytes());
        put.addColumn("info".getBytes(), "sex".getBytes(), "femail".getBytes());
        table.put(put);

        put = new Put("1003".getBytes());
        put.addColumn("info".getBytes(), "name".getBytes(), "fangfang".getBytes());
        put.addColumn("info".getBytes(), "age".getBytes(), "26".getBytes());
        put.addColumn("info".getBytes(), "sex".getBytes(), "femail".getBytes());
        table.put(put);
    }

遍历数据

    public static void scanData(Table table) throws IOException {
        Scan scan = new Scan();
        ResultScanner resutScanner = table.getScanner(scan);
        for(Result result: resutScanner){
            String id = Bytes.toString(result.getRow());
            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
            System.out.println("id=" + id + ",name=" + name);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mustafa3264

扔个包子砸我一下吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值