Java API 连接和操作 Hbase

1、修改pom文件
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>1.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-common -->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-common</artifactId>
      <version>1.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>1.2.0</version>
    </dependency>
2、基本增删改查操作
package com.njbdqn.services;

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 org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * @Author: Stephen
 * @Date: 2019/12/23 10:06
 * @Content: java 连接 hbase
 */
public class Mydemo {
    public static void main(String[] args) throws Exception{
        // 连接数据库 先添加配置
        Configuration conf = HBaseConfiguration.create();
        // 驱动
        conf.set("hbase.zookeeper.quorum","192.168.56.122:2181");
        // 开启连接
        Connection connection = ConnectionFactory.createConnection(conf);
        Table customer = connection.getTable(TableName.valueOf("firstdemo:users"));
        /**
         * 查询所有
         */
//        Scan scan = new Scan();
//        ResultScanner res = customer.getScanner(scan);
//        for (Result re : res) {
//            System.out.println(Bytes.toString(re.getValue("base".getBytes(),"username".getBytes())));
//        }
//        res.close();

        /**
         * 添加数据
         */
//        Put pt = new Put("5".getBytes());
//        pt.addColumn("base".getBytes(),"username".getBytes(),"sq".getBytes());
//        pt.addColumn("base".getBytes(),"age".getBytes(),"22".getBytes());
//        pt.addColumn("details".getBytes(),"like".getBytes(),"sleep,eat".getBytes());
//        customer.put(pt);
//        connection.close();

        /**
         * 模糊查询
         */
//        Scan scan = new Scan();
//        SingleColumnValueFilter filter = new SingleColumnValueFilter("base".getBytes(), "username".getBytes(),
//                CompareFilter.CompareOp.EQUAL, new RegexStringComparator("l.*"));
//        scan.setFilter(filter);
//        ResultScanner res = customer.getScanner(scan);
//        for (Result re : res) {
//            System.out.println(Bytes.toString(re.getValue("base".getBytes(),"username".getBytes())));
//        }
//        res.close();
        /**
         * 查询 by id
         */
        Get get = new Get("1".getBytes());
        Result result = customer.get(get);
        System.out.println(Bytes.toString(result.getValue("base".getBytes(),"username".getBytes())));

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值