能写一个Hbase java api测试程序嘛
时间: 2024-05-09 08:21:03 浏览: 103
当然可以,以下是一个简单的HBase Java API测试程序:
```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;
public class HBaseApiTest {
private static Configuration conf;
private static Connection connection;
private static Admin admin;
public static void main(String[] args) throws IOException {
// 创建配置对象
conf = HBaseConfiguration.create();
// 获取连接
connection = ConnectionFactory.createConnection(conf);
// 创建表
String tableName = "test_table";
String[] columnFamilies = {"cf1", "cf2"};
createTable(tableName, columnFamilies);
// 插入数据
String rowKey = "row1";
String columnFamily = "cf1";
String column = "column1";
String value = "value1";
putData(tableName, rowKey, columnFamily, column, value);
// 查询数据
getData(tableName, rowKey, columnFamily, column);
// 删除数据
deleteData(tableName, rowKey, columnFamily, column);
// 删除表
deleteTable(tableName);
// 关闭连接
connection.close();
}
// 创建表
private static void createTable(String tableName, String[] columnFamilies) throws IOException {
// 获取管理员
admin = connection.getAdmin();
// 创建表描述符
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
// 添加列族
for (String columnFamily : columnFamilies) {
HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);
tableDescriptor.addFamily(columnDescriptor);
}
// 创建表
admin.createTable(tableDescriptor);
System.out.println("Table " + tableName + " created successfully.");
}
// 插入数据
private static void putData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
// 获取表
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建行
Put put = new Put(Bytes.toBytes(rowKey));
// 添加列族、列和值
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
// 插入数据
table.put(put);
System.out.println("Data inserted successfully.");
}
// 查询数据
private static void getData(String tableName, String rowKey, String columnFamily, String column) throws IOException {
// 获取表
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建行
Get get = new Get(Bytes.toBytes(rowKey));
// 获取指定列族、列的值
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
System.out.println("Value retrieved: " + Bytes.toString(value));
}
// 删除数据
private static void deleteData(String tableName, String rowKey, String columnFamily, String column) throws IOException {
// 获取表
Table table = connection.getTable(TableName.valueOf(tableName));
// 创建行
Delete delete = new Delete(Bytes.toBytes(rowKey));
// 删除指定列族、列
delete.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
// 删除数据
table.delete(delete);
System.out.println("Data deleted successfully.");
}
// 删除表
private static void deleteTable(String tableName) throws IOException {
// 获取管理员
admin = connection.getAdmin();
// 禁用表
admin.disableTable(TableName.valueOf(tableName));
// 删除表
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("Table " + tableName + " deleted successfully.");
}
}
```
该程序包含了创建表、插入数据、查询数据、删除数据和删除表等常见操作。您可以根据自己的需要进行修改和扩展。
阅读全文