文章目录
1. DDL
1.1 hbase创建删除命名空间、表api
public class Hbase_DDL {
private static Connection connection;
private static Admin admin;
static {
//创建连接信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
try {
//创建连接器
connection = ConnectionFactory.createConnection(configuration);
//创建DDL操作对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
//创建命名空间
public static void createNS(String ns) throws IOException {
//创建命名空间
try {
admin.createNamespace(NamespaceDescriptor.create(ns).build());
} catch (NamespaceExistException e) {
System.out.println("命名空间已存在");
}
}
//创建表
public static void createTable(String tableName, String... cfs) throws IOException {
//判断是否有列族信息
if (cfs.length <= 0) {
System.out.println("未输入列族信息");
return;
}
//判断表是否存在
if (admin.tableExists(TableName.valueOf(tableName))) {
System.out.println(tableName + " 表存在");
return;
}
//创建表描述器
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
for (String cf : cfs) {
//创建列族描述器
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build();
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
}
TableDescriptor descriptor = tableDescriptorBuilder.build();
//创建表
admin.createTable(descriptor);
}
//删除表
public static void dropTable(String tableName) throws IOException {
TableName name = TableName.valueOf(tableName);
if(!admin.tableExists(name)){
System.out.println(tableName + " 表不存在");
return;
}
//使表不可用
admin.disableTable(TableName.valueOf(tableName));
//删除表
admin.deleteTable(TableName.valueOf(tableName));
}
public static void close() throws IOException {
//释放连接
admin.close();
connection.close();
}
}
1.2 创建命名空间、创建表
public static void main(String[] args) throws IOException {
createNS("sjj");
createTable("sjj:stu", "info1", "info2");
close();
}
1.3 删除表
public static void main(String[] args) throws IOException {
dropTable("sjj:stu");
close();
}
2. DML
2.1 hbase增删查改api
public class Hbase_DML {
private static Connection connection;
static {
//创建连接信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
try {
//创建连接器
connection = ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
//新增修改数据
public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
//获取DML的Table对象
Table table = connection.getTable(TableName.valueOf(tableName));
//创建Put对象
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes("age"), 1655607479312L, Bytes.toBytes("20"));
//执行插入数据操作
table.put(put);
//释放资源
table.close();
}
//get数据查询
public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
//获取DML的Table对象
Table table = connection.getTable(TableName.valueOf(tableName));
//创建Get对象
Get get = new Get(Bytes.toBytes(rowKey));
if (StringUtils.isNotEmpty(cn) ) {
//指定列族,列
get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
} else {
//指定列族
get.addFamily(Bytes.toBytes(cf));
}
//查询数据
Result result = table.get(get);
//获取结果
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(" CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) + " CN: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " value: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
//释放资源
table.close();
}
//扫描数据
public static void scanData(String tableName) throws IOException {
//获取DML的Table对象
Table table = connection.getTable(TableName.valueOf(tableName));
//创建scan对象
Scan scan = new Scan();
//指定 startRow stopRow
// scan.withStartRow(Bytes.toBytes("1002"));
// scan.withStopRow(Bytes.toBytes("1003"));
//扫描全表
ResultScanner resultScanner = table.getScanner(scan);
//迭代
Iterator<Result> resultIterator = resultScanner.iterator();
while (resultIterator.hasNext()) {
Result result = resultIterator.next();
//获取结果
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(" CF: " + Bytes.toString(CellUtil.cloneFamily(cell)) + " CN: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + " value: " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
//释放资源
table.close();
}
//删除数据
public static void deleteData(String tableName, String rowKey, String cf, String cn) throws IOException {
//获取DML的Table对象
Table table = connection.getTable(TableName.valueOf(tableName));
//创建delete对象
Delete delete = new Delete(Bytes.toBytes(rowKey));
if (StringUtils.isNotEmpty(cn)) { //指定列族 列删除
delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
} else if (StringUtils.isNotEmpty(cf)) { //指定列族删除
delete.addFamily(Bytes.toBytes(cf));
}
//删除数据
table.delete(delete);
//释放资源
table.close();
}
public static void close() throws IOException {
connection.close();
}
}
2.2 插入数据
public static void main(String[] args) throws IOException {
putData("sjj:stu", "1001", "info1", "name", "cz");
close();
}
2.3 查询数据
public static void main(String[] args) throws IOException {
getData("sjj:stu", "1002", "info1", null);
close();
}
2.4 扫描数据
public static void main(String[] args) throws IOException {
scanData("sjj:stu");
close();
}
2.5 删除数据
public static void main(String[] args) throws IOException {
deleteData("sjj:stu","1001",null,null);
close();
}
注:
DeleteFamily:执行删除整个RowKey数据所添加的标记,作用范围:当前列族
小于等于标记时间戳的数据
DeleteColumn:执行删除指定到列族的列数据所添加的标记,作用范围:当前列
小于等于标记时间戳的数据
Delete:执行删除指定到列族的列数据锁添加的标记,作用范围:只作用于标记中所携带的时间戳的范围