1、启动
2、ZK客户端
3、springboot+hbase实例
1)、pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>spring-boot-starter-hbase</artifactId>
<version>1.0.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.11</version>
</dependency>
</dependencies>
2)、HbaseCase
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class HbaseCase {
public static void main(String[] args) throws IOException {
//insertData("tb_user","r6","tf","username","6name");
insertData("tb_user","r6","tf","id","6");
insertData("tb_user","r6","tf","username","6name");
insertData("tb_user","r6","tf","pwd","pwd6");
getData("tb_user","r6","tf","id");
//createTable("tb_role",new String[]{"tf"});
}
/**
* 获取数据
* @param tableName 表名
* @param rowkey 行键
* @param colFamily 列族
* @param col 列
* @throws IOException 可能出现的异常
*/
public static void getData(String tableName,String rowkey,String colFamily,String col) throws IOException {
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","127.0.0.1");
Connection conn =ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(rowkey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
System.out.println(new String(result.getValue(colFamily.getBytes(),col.getBytes())));
table.close();
}
/**
* 添加数据
* @param tableName 表名
* @param rowkey 行键
* @param colFamily 列族
* @param col 列
* @param value 值
* @throws IOException 可能出现的异常
*/
public static void insertData(String tableName,String rowkey,String colFamily,String col,
String value) throws IOException {
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","127.0.0.1");
Connection conn =ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf(tableName));
Put put = new Put(rowkey.getBytes());
put.addColumn(colFamily.getBytes(),col.getBytes(),value.getBytes());
table.put(put);
table.close();
}
/**
* 创建表
* @param myTableName 表名
* @param colFamily 列族名的数组
* @throws IOException 可能出现的异常
*/
public static void createTable(String myTableName, String[] colFamily) throws IOException {
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","127.0.0.1");
Connection conn =ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
TableName tableName = TableName.valueOf(myTableName);
if (admin.tableExists(tableName)) {
System.out.println(myTableName + "表已经存在");
} else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for (String str : colFamily) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
}
/**
* 根据行键删除数据
* @param tableName 表名
* @param rowkey 行键
* @throws IOException 可能出现的异常
*/
public static void deleteData(String tableName,String rowkey) throws IOException {
Configuration conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","127.0.0.1");
Connection conn =ConnectionFactory.createConnection(conf);
Table table = conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowkey.getBytes());
table.delete(delete);
table.close();
}
}
4、实例记录
新增记录
创建表
标签:String,tableName,param,conf,org,hbase,20230509,Springboot From: https://www.cnblogs.com/smallfa/p/17385612.html