一、环境
在 IDEA 中创建一个 maven 工程,添加依赖,pom 文件如下:
<!--阿里云搭建了一个国内镜像http://maven.aliyun.com,跑起来速度很快,可以进行配置-->
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>nexus-aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
二、客户端代码
这里使用的是 hbase 的新版 API,过时的一些 API 方法没有使用。
package point1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* @description: hbase 基础操作
* @author: hyr
* @time: 2020/3/1 21:00
*/
public class HbaseBasic {
private static Connection connection = null;
private static Admin admin = null;
// 连接配置
static {
// 1、获取一个客户端配置信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.2.151,192.168.2.152,192.168.2.153");
// 2、创建连接对象
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭资源
public static void close() {
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 一、查看表是否存在
private static boolean isTableExist(String tableName) throws IOException {
return admin.tableExists(TableName.valueOf(tableName));
}
// 二、创建表
public static void createTable(String tableName, String... columnFamily) throws IOException {
if (columnFamily.length <= 0) {
System.out.println("请设置列族信息!");
return;
}
if (isTableExist(tableName)) {
System.out.println("表" + tableName + "已存在");
return;
} else {
// 创建表属性对象,表名需要转字节
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
// 创建多个列族
for (String cf : columnFamily) {
descriptor.addFamily(new HColumnDescriptor(cf));
}
// 根据对表的配置,创建表
admin.createTable(descriptor);
if (isTableExist(tableName)) {
System.out.println("表" + tableName + "创建成功!");
} else {
System.out.println("表" + tableName + "创建失败!");
}
}
}
// 三、删除表
private static void dropTable(String tableName) throws IOException {
// 将字符串封装为 TableName 对象
TableName tableNameNew = TableName.valueOf(tableName);
if (isTableExist(tableName)) {
admin.disableTable(tableNameNew);
admin.deleteTable(tableNameNew);
if (!isTableExist(tableName)) {
System.out.println("表" + tableName + "删除成功!");
}
} else {
System.out.println("表" + tableName + "不存在!");
}
}
// 四、创建命名空间
private static void createNameSpace(String ns) {
// 创建命名空间描述器
NamespaceDescriptor build = NamespaceDescriptor.create(ns).build();
// 创建命名空间
try {
admin.createNamespace(build);
} catch (NamespaceExistException e) {
System.out.println("命名空间已存在!");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("异常处理后,不影响后续代码执行。");
}
// 五、向表中插入数据
private static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) {
Table table = null;
try {
// 创建 Table 对象
table = connection.getTable(TableName.valueOf(tableName));
// 向表中插入数据
Put put = new Put(Bytes.toBytes(rowKey));
// 向 Put 对象中组装数据
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 六、获取数据(get)
private static void getData(String tableName, String rowKey) {
Table table = null;
try {
// 1、获取表对象
table = connection.getTable(TableName.valueOf(tableName));
// 2、创建 Get 对象
Get get = new Get(Bytes.toBytes(rowKey));
// 3、获取数据
Result result = table.get(get);
// 4、解析 result 并打印
for (Cell cell : result.rawCells()) {
// 5、打印数据
System.out.println(
"行:" + Bytes.toString(CellUtil.cloneRow(cell))
+ ",列族:" + Bytes.toString(CellUtil.cloneFamily(cell))
+ ",列:" + Bytes.toString(CellUtil.cloneQualifier(cell))
+ ",值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 6、关闭流
if (table != null) {
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 七、获取数据(scan)
private static void scanTable(String tableName) {
Table table = null;
try {
// 1、获取表对象
table = connection.getTable(TableName.valueOf(tableName));
// 2、构造 scan 对象(可以设置参数)
Scan scan = new Scan();
// 3、扫描表
ResultScanner resultScanner = table.getScanner(scan);
// 4、解析 resultScanner
for (Result result : resultScanner) {
// 5、解析 result 并打印
for (Cell cell : result.rawCells()) {
// 6、打印数据
System.out.println("行:" + Bytes.toString(CellUtil.cloneRow(cell))
+ ",列族:" + Bytes.toString(CellUtil.cloneFamily(cell))
+ ",列:" + Bytes.toString(CellUtil.cloneQualifier(cell))
+ ",值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 7、关闭流
if (table != null) {
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 八、删除数据
private static void deleteData(String tableName, String rowKey) {
Table table = null;
try {
// 1、获取表对象
table = connection.getTable(TableName.valueOf(tableName));
// 2、构造删除对象
Delete delete = new Delete(Bytes.toBytes(rowKey));
// 按照列族、时间戳、列的删除可调用下面两个 api
// delete.addColumn()
// delete.addColumns()
// 3、执行删除操作
table.delete(delete);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4、关闭流
try {
if (table != null) {
table.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
// 1、判断表是否存在
// System.out.println(isTableExist("student"));
// 2、创建表
// String[] info = {"info"};
// createTable("student1",info);
// 3、删除表
// dropTable("student");
// 4、创建命名空间
// createNameSpace("0688");
// 5、向表中插入数据
// addRowData("student1","1","info","sex", "男");
// 6、获取数据(get)
// getData("student1", "1");
// 7、获取数据(scan)
// scanTable("student1");
// 8、删除数据
// deleteData("student1","1");
// 关闭资源
// admin.close();
}
}