1.DDL
主要就是通过Admin的对象进行操作即可
package ddl;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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 MyDDL {
static Connection connection;
static {
//1.创建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//2.获取与HBase的连接
try {
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// System.out.println(isTableExists("t1"));
// createTable("t2", "info","name");
// deleteTable("t2");
// createNamespace("n1");
addColumnFamilyTable("t1", "test");
}
/**
* 判断表是否存在 默认使用default命名空间(数据库) 测试 ,若要使用其他数据库 则需要添加一个参数 namespace
* @param tableName
* @return
* @throws IOException
*/
public static boolean isTableExists(String tableName) throws IOException {
boolean exists;
// 1.获取DDL操作对象 admin
Admin admin = connection.getAdmin();
// 2.判断表是否存在
exists = admin.tableExists(TableName.valueOf(tableName));
admin.close();
// connection.close(); // 静态方法在编译类时就会被调用,所以就算不调用 也能执行代码
return exists;
}
/**
* 创建表
* @param tableName
* @param cfs
* @throws IOException
*/
public static void createTable(String tableName,String ...cfs) throws IOException {
if (cfs.length < 1){
System.out.println("请设置列族信息");
return;
}
Admin admin = connection.getAdmin();
if (isTableExists(tableName)){
System.out.println(tableName + "已存在");
return;
}
// 1.创建表描述构造器
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
// 2.循环添加列族信息
for (String cf : cfs) {
// 1.创建列描述构造器
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
// 2.通过列描述构造器 创建列族描述器
ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();
// 3.添加到表描述构造器中
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
}
// 4.通过 表描述构造器 创建 表描述器
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
// 5.创建表
admin.createTable(tableDescriptor);
// 6.关闭资源
admin.close();
// connection.close();
}
/**
* 删除表
* @param tableName
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
if (isTableExists(tableName)){
// 1.获取admin 对象
Admin admin = connection.getAdmin();
// 2.使该表下线
admin.disableTable(TableName.valueOf(tableName));
// 3.删除该表
admin.deleteTable(TableName.valueOf(tableName));
// 4.关闭连接
admin.close();
}else
System.out.println(tableName+"不存在");
}
/**
* 创建命名空间
* @param nameSpace
* @throws IOException
*/
public static void createNamespace(String nameSpace) throws IOException {
// 1.获取admin对象
Admin admin = connection.getAdmin();
// 2.创建namespace描述构造器
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(nameSpace);
NamespaceDescriptor build = builder.build();
// 3.通过admin添加命名空间
try {
admin.createNamespace(build);
} catch (IOException e) {
System.out.println(nameSpace+"已存在");
} finally {
admin.close();
}
}
/**
* 删除指定表的列族
* @param tableName
* @param cf
* @throws IOException
*/
public static void addColumnFamilyTable(String tableName,String cf) throws IOException {
// 0.判断表是否存在
if (!isTableExists(tableName)){
System.out.println(tableName+"已存在");
return;
}
// 1.获取admin对象
Admin admin = connection.getAdmin();
// 2.获取列族描述器
ColumnFamilyDescriptor of = ColumnFamilyDescriptorBuilder.of(Bytes.toBytes(cf));
try {
admin.addColumnFamily(TableName.valueOf(tableName), of);
} catch (IOException e) {
e.printStackTrace();
} finally {
admin.close();
}
}
/**
* 删除指定表的列族
* @param tableName
* @param cf
* @throws IOException
*/
public static void deleteColumnFamilyTable(String tableName,String cf) throws IOException {
// 0.判断表是否存在
if (!isTableExists(tableName)){
System.out.println(tableName+"已存在");
return;
}
// 1.获取admin对象
Admin admin = connection.getAdmin();
admin.deleteColumnFamily(TableName.valueOf(tableName), Bytes.toBytes(cf));
}
}
2.DML
数据的增删改查
package dml;
import ddl.MyDDL;
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;
import java.util.Arrays;
/**
* @ClassName: MyDML
* @Description: 依旧使用 default 测试
* @Author: jjj
* @Date: 2021/12/19 19:17
**/
public class MyDML {
static Connection connection;
static {
//1.创建配置信息并配置
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
//2.获取与HBase的连接
try {
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// putColumnData("t1", "1003", "f1", "name", "lw");
// getColumnData("t1", "1001");
// scanTable("t1");
deleteRowKey("t1", "1003");
}
/**
* 插入一个字段的数据 逻辑的一个字段值
* @param tableName
* @param rowKey
* @param cf
* @param cl
* @param value
* @throws IOException
*/
public static void putColumnData(String tableName,String rowKey,String cf,String cl,String value) throws IOException {
// 0.若表不存在则创建该表
if (!MyDDL.isTableExists(tableName)){
MyDDL.createTable(tableName, cf);
}
// 1.获取 table 操作对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 2.创建put对象
Put put = new Put(Bytes.toBytes(rowKey));
// 3.添加数据
put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cl),Bytes.toBytes(value));
// 4.将put添加到表中
table.put(put);
// 5.关闭资源
table.close();
}
/**
* 获取指定rowKey 的所有信息
* @param tableName
* @param rowKey
*/
public static void getColumnData(String tableName,String rowKey) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
// 1.指定get对象
Get get = new Get(Bytes.toBytes(rowKey));
// 2.获取结果
Result result = table.get(get);
// 3.解析数据
for (Cell cell : result.rawCells()) {
System.out.println("row:"+ Bytes.toString(CellUtil.cloneRow(cell))+
" cf:"+ Bytes.toString(CellUtil.cloneFamily(cell))+
" cl:"+ Bytes.toString(CellUtil.cloneQualifier(cell))+
" value:"+ Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
/**
* 扫描表数据
* @param tableName
*/
public static void scanTable(String tableName) throws IOException {
// 1.获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 2.获取scan对象
Scan scan = new Scan();
// 3.获取结果
ResultScanner scanner = table.getScanner(scan);
// 4.解析数据
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println("row:"+ Bytes.toString(CellUtil.cloneRow(cell))+
" cf:"+ Bytes.toString(CellUtil.cloneFamily(cell))+
" cl:"+ Bytes.toString(CellUtil.cloneQualifier(cell))+
" value:"+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
// 5. 关闭资源
table.close();
}
/**
* 删除一个rowKey
* @param tableName
* @param rowKey
* @throws IOException
*/
public static void deleteRowKey(String tableName,String rowKey) throws IOException {
// 1.获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 2.创建delete对象
Delete delete = new Delete(Bytes.toBytes(rowKey));
// 3.将delete对象放入table中
table.delete(delete);
// 4.关闭资源
table.close();
}
}