系列文章目录
HBase第一章:集群搭建
HBase第二章:APT操作(一)
前言
这次博客我们记录一些api的常用操作,因为shell的方式在hbase之中并不常用,所以我们选择跳过。
一、创建项目
1.新建项目
项目名称随意写,因为我已经创建过了,所以这里就不再次创建了。
2.创建需要的类
3.添加依赖
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.13</version>
</dependency>
</dependencies>
4.设置log4j
log4j.properties
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
二、编写代码
1.数据库连接
HBaseConnection.java
package com.atguigu;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class HBaseConnection {
//设置静态熟悉hbase连接
public static Connection connection;
static {
try {
//配置参数留空,使用配置文件
connection = ConnectionFactory.createConnection();
} catch (IOException e) {
System.out.println("连接失败");
throw new RuntimeException(e);
}
}
//关闭连接
public static void closeConnection() throws IOException {
if (connection != null) {
connection.close();
}
}
//测试连接
public static void main(String[] args) throws IOException {
System.out.println(HBaseConnection.connection);
HBaseConnection.closeConnection();
}
}
在resource目录中添加配置文件。
建议从hbase的配置文件中复制,然后删除不需要的,留下hbase.zookeeper.quorum即可。
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.zookeeper.quorum</name>
<value>hadoop102,hadoop103,hadoop104</value>
<description>The directory shared by RegionServers.
</description>
</property>
</configuration>
当打印出连接对象时,说明连接成功。
2.api操作
HBaseDDL.java
package com.atguigu;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseDDL {
// 添加静态属性connection指向单例连接
public static Connection connection = HBaseConnection.connection;
/**
* 创建命名空间
* @param namespace 命名空间名称
*/
public static void creatNamespace(String namespace) throws IOException {
//1.获取admin
Admin admin = connection.getAdmin();
//2.调用方法创建空间
//2.1创建命令空间描述建造者
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);
//2.2给命令空间添加需求
builder.addConfiguration("user","atguigu");
//2.3使用 builder 构造出对应的添加完参数的对象 完成创建
try {
admin.createNamespace(builder.build());
} catch (IOException e) {
System.out.println("命名空间已存在");
throw new RuntimeException(e);
}
//3.关闭连接
admin.close();
}
/**
* 判断表格是否存在
* @param namespace 命名空间名称
* @param tableName 表格名称
* @return true 表格存在
*/
public static boolean isTableExists(String namespace , String tableName) throws IOException {
//1.获取admin
Admin admin = connection.getAdmin();
//2.判断结果参数
boolean b;
try {
b = admin.tableExists(TableName.valueOf(namespace, tableName));
} catch (IOException e) {
throw new RuntimeException(e);
}
//3.关闭admin
admin.close();
//4.返回结果
return b;
}
/**
* 创建表格
* @param namespace 命名空间名称
* @param tableName 表格名称
* @param columnFamilies 列族名称 可以有多个
*/
public static void createTable(String namespace ,String tableName ,String... columnFamilies) throws IOException {
//判断至少一个列族
if (columnFamilies.length==0){
System.out.println("创建表格至少有一个列族");
return;
}
//判断表格是否存在
if (isTableExists(namespace,tableName)){
System.out.println("表格已经存在");
return;
}
//1.获取admin
Admin admin = connection.getAdmin();
//2.调用方法创建表格
//2.1创建表格描述的建造者
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace ,tableName));
//2.2添加参数
for (String columnFamily : columnFamilies) {
//2.3创建列族描述的建造者
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
//2.4对应当前的列族添加参数
columnFamilyDescriptorBuilder.setMaxVersions(5);
//2.5创建添加完参数的列族描述
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
//2.6创建对应的表格描述
try {
admin.createTable(tableDescriptorBuilder.build());
} catch (IOException e) {
throw new RuntimeException(e);
}
//3.关闭admin
admin.close();
}
/**
* 修改表格中一个列族的版本
* @param namespace 命名空间名称
* @param tableName 表格名称
* @param columFamily 列族名称
* @param version 版本
*/
public static void modifyTable(String namespace , String tableName ,String columFamily , int version) throws IOException {
//判断表格是否存在
if (!isTableExists(namespace,tableName)){
System.out.println("表格不存在无法修改");
return;
}
//1.获取admin
Admin admin = connection.getAdmin();
try {
//2.调用方法修改表格
//2.0获取之前的表格描述
TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(namespace, tableName));
//2.1创建一个表格描述建造者
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor);
//2.2对应建造者进行表格数据的修改
ColumnFamilyDescriptor columnFamily = descriptor.getColumnFamily(Bytes.toBytes(columFamily));
//创建列族描述建造者
//需要填写旧的列族描述
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(columnFamily);
//修改对应的版本
columnFamilyDescriptorBuilder.setMaxVersions(version);
tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());
admin.modifyTable(tableDescriptorBuilder.build());
} catch (IOException e) {
throw new RuntimeException(e);
}
//3.关闭连接
admin.close();
}
/**
* 删除表格
* @param namespace 命名空间名称
* @param tableName 表格名称
* @return true 删除成功
*/
public static boolean deleTable(String namespace ,String tableName) throws IOException {
//1.判断表格是否存在
if (!isTableExists(namespace,tableName)){
System.out.println("表格不存在");
return false;
}
//2.获取admin
Admin admin = connection.getAdmin();
//3.调用相关方法
try {
//HBase 删除表格之前 一定要先标记表格为不可用
TableName tableName1 = TableName.valueOf(namespace, tableName);
admin.disableTable(tableName1);
admin.deleteTable(tableName1);
} catch (IOException e) {
throw new RuntimeException(e);
}
//4.关闭admin
admin.close();
return true;
}
public static void main(String[] args) throws IOException {
//创建命名空间
// creatNamespace("atguigu");
//创建表
// createTable("atguigu","student","info");
// System.out.println(isTableExists("atguigu", "student"));
//修改表
// modifyTable("atguigu","student","info",6);
//删除表
// deleTable("atguigu","student");
System.out.println("其它代码");
HBaseConnection.closeConnection();
}
}
这里创建了几个参数,包括测试函数也已经创建好啦,挨个测试即可。
提一下异常处理,这里有一部分将异常抛给了上一层,有一些则用try/catch捕捉,简单说一下区别放下,上一层的错误就抛出,比如创建表之前的连接,自身错误就捕捉,比如创建表时发生的异常。
-
创建命名空间
不报错误代表成功
-
创建表
-
修改表
我们修改其版本信息(其它也可以)
-
删除表
删除刚刚创建的(不常用)
总结
这次完成了hbase的增删改功能,下次再完成查询功能。