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())));
}
}