提供了
1.創建鏈接
2.創建命名空間
3.判斷表存在與否
4.創建表
5.修改表
6.洗掉表
7.插入資料
8.查詢資料
9.掃描資料
10.洗掉資料
Maven添加如下依賴
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>2.0.5</version>
<exclusions>
<exclusion>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b06</version>
</dependency>
</dependencies>
HbaseUtils.java
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.ArrayList;
import java.util.List;
public class HbaseUtils {
/*
* @author 70493
* @Description 使用前必看!!!!!
* 修改下方value為自己的zookeeper組,也可以只寫一臺,但是容錯率低
* @Date 19:34 2021/10/29
**/
private static String value="hadoop102,hadoop103,hadoop104";
private static Connection connection;
static{
try {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", value);
connection = ConnectionFactory.createConnection(conf);
System.out.println("連接成功");
}catch (Exception e) {
System.out.println(e);
}
}
/*
* @author 70493
* @Description 創建命名空間
* @Date 19:34 2021/10/29
* @Param namespace:命名空間名
* @return boolean
**/
public static Boolean createNameSpace(String namespace){
boolean flag = false;
try {
Admin admin = connection.getAdmin();
NamespaceDescriptor db = NamespaceDescriptor.create(namespace).build();
admin.createNamespace(db);
flag = true;
}catch (Exception e) {
System.out.println(e);
}
return flag;
}
/*
* @author 70493
* @Description 檢查表是否存在
* @Date 19:34 2021/10/29
* @Param namespace:命名空間名
* @return boolean
**/
public static boolean isTableExist(String namespace,String tableName) throws IOException {
Admin admin = connection.getAdmin();
boolean flag = admin.tableExists(TableName.valueOf(namespace, tableName));
admin.close();
return flag;
}
/*
* @author 70493
* @Description 創建表
* @Date 19:34 2021/10/29
* @Param namespace:命名空間名 tableName:表名 familyNames:列族名 默認維護版本為2
* @return
**/
public static boolean createTable(String namespace,String tableName,String... familyNames) throws IOException {
if (familyNames.length <= 0) {
System.out.println("請輸入列族數");
return false;
}
if (isTableExist(namespace,tableName)) {
System.out.println("表格已存在");
return false;
}
Admin admin = connection.getAdmin();
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, tableName));
for (String familyName : familyNames) {
ColumnFamilyDescriptorBuilder builder1 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(familyName));
builder1.setMaxVersions(2);
builder.setColumnFamily(builder1.build());
}
admin.createTable(builder.build());
admin.close();
return true;
}
/*
* @author 70493
* @Description 修改表
* @Date 20:00 2021/10/29
* @Param
* @return
**/
public static boolean modifyTable(String nameSpace,String tableName,String familyName,int version) throws IOException {
Admin admin = connection.getAdmin();
TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(nameSpace, tableName));
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor);
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(familyName)).setMaxVersions(version);
tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());
admin.modifyTable(tableDescriptorBuilder.build());
admin.close();
return true;
}
/*
* @author 70493
* @Description 洗掉表
* @Date 20:02 2021/10/29
* @Param
* @return
**/
public static boolean deleteTable(String nameSpace,String tableName) throws IOException {
if (!isTableExist(nameSpace, tableName)){
System.out.println("表格不存在");
return false;
}
Admin admin = connection.getAdmin();
admin.disableTable(TableName.valueOf(nameSpace, tableName));
admin.deleteTable(TableName.valueOf(nameSpace, tableName));
admin.close();
return true;
}
/*
* @author 70493
* @Description 插入資料
* @Date 20:02 2021/10/29
* @Param
* @return
**/
public static void putCell(String nameSpace,String tableName,String rowKey,String family,String column,String value) throws IOException {
Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
table.put(put);
table.close();
}
/*
* @author 70493
* @Description 獲取資料
* @Date 20:03 2021/10/29
* @Param
* @return
**/
public static String getCell(String nameSpace,String tableName,String rowKey,String family,String column) throws IOException {
Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
Result result = table.get(get);
Cell[] cells = result.rawCells();
String value = "";
for (Cell cell : cells) {
value += Bytes.toString(CellUtil.cloneValue(cell)) + "-";
}
table.close();
return value;
}
/*
* @author 70493
* @Description 掃描行
* @Date 20:04 2021/10/29
* @Param
* @return
**/
public static List<String> scanRows(String nameSpace, String tableName, String startRow, String stopRow) throws IOException {
Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));
Scan scan = new Scan().withStartRow(Bytes.toBytes(startRow)).withStopRow(Bytes.toBytes(stopRow));
ResultScanner scanner = table.getScanner(scan);
ArrayList<String> arrayList = new ArrayList<>();
for (Result result : scanner) {
arrayList.add(Bytes.toString(result.value()));
}
// 5.關閉資源
scanner.close();
table.close();
return arrayList;
}
/*
* @author 70493
* @Description 洗掉資料
* @Date 20:05 2021/10/29
* @Param
* @return
**/
public static void deleteColumn(String nameSpace, String tableName, String rowKey, String family, String column) throws IOException {
Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumns(Bytes.toBytes(family), Bytes.toBytes(column));
table.delete(delete);
table.close();
}
public static void close(){
try {
connection.close();
}catch (Exception e) {
System.out.println("關閉連接失敗");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342143.html
標籤:其他
上一篇:RabbitMQ如何保證順序消費
下一篇:擺爛的計劃(懂得都懂)
