package hbase.test;
import java.io.IOException;
import org.apache.avro.generic.GenericData;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseTest {
static Configuration configuration = null;
static {
configuration = HBaseConfiguration.create();
}
//创建表并指定列簇
public static void createTable(String tableName, String cols[]) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
HBaseAdmin baseAdmin = new HBaseAdmin(configuration);
if (baseAdmin.tableExists(tableName)) {
System.out.println("表【" + tableName + "】存在");
} else {
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
for (String s : cols) {
HColumnDescriptor columnDescriptor = new HColumnDescriptor(s);
tableDescriptor.addFamily(columnDescriptor);
}
baseAdmin.createTable(tableDescriptor);
baseAdmin.close();
}
}
//删除表
public static void deleteTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
}
//添加数据
/**
* @param tableName 表名
* @param rowkey 行号
* @param colFamily 列簇
* @param column 列
* @param value 列值
*/
public static void insertData(String tableName, String rowkey, String colFamily, String column, String value) throws IOException {
HTable table = new HTable(configuration, tableName);
Put put = new Put(Bytes.toBytes(rowkey));
put.add(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
}
//查询一条数据
public static void getOneByeRowkey(String tableName, String rowkey) throws IOException {
HTable table = new HTable(configuration, tableName);
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (KeyValue k : result.raw()) {
System.out.println("行号:" + Bytes.toStringBinary(k.getRow()));
System.out.println("时间戳:" + k.getTimestamp());
System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily()));
System.out.println("列:" + Bytes.toStringBinary(k.getQualifier()));
System.out.println("值:" + Bytes.toString(k.getValue()));
}
table.close();
}
//查询指定数据
public static void scanData(String tableName) throws IOException {
HTable table = new HTable(configuration, tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
for (Result result : rs) {
for (KeyValue k : result.raw()) {
System.out.println("行号:" + Bytes.toStringBinary(k.getRow()));
System.out.println("时间戳:" + k.getTimestamp());
System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily()));
System.out.println("列:" + Bytes.toStringBinary(k.getQualifier()));
System.out.println("值:" + Bytes.toString(k.getValue()));
}
}
table.close();
}
//删除一条数据
@SuppressWarnings("empty-statement")
public static void deleteByRow(String tableName, String rowkey) throws IOException {
HTable table = new HTable(configuration, tableName);
//删除一条数据
Delete delete = new Delete(Bytes.toBytes(rowkey));
table.delete(delete);
//删除多条数据
String[] rowkeys = {};
java.util.List<Delete> list = new java.util.ArrayList<Delete>();
for (String rk : rowkeys) {
Delete d = new Delete(Bytes.toBytes(rk));
list.add(d);
}
table.delete(list);
//
table.close();
}
public static void main(String[] args) throws ZooKeeperConnectionException, IOException {
String tableName = "mytest";
String cols[] = {"a", "b", "c"};
// createTable(tableName, cols);
// insertData(tableName, "1", "a", "abc", "11111");
// getOneByeRowkey(tableName, "1");
//scanData(tableName);
// deleteByRow(tableName, "1");
// scanData(tableName);
}
}
hbase-java-client
最新推荐文章于 2022-07-11 13:57:21 发布