packagecom.test.hbase;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.CellUtil;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.MasterNotRunningException;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.ZooKeeperConnectionException;importorg.apache.hadoop.hbase.client.Delete;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.HBaseAdmin;importorg.apache.hadoop.hbase.client.HTable;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.util.Bytes;importorg.apache.hadoop.hbase.Cell;public classHbaseTest {private static Configuration conf = null;/*** 初始化配置*/
static{
conf=HBaseConfiguration.create();
}/*** 创建表操作
*
*@throwsIOException
*@throwsZooKeeperConnectionException
*@throwsMasterNotRunningException*/
public voidcreateTable(String tableName, String[] cfs)throwsMasterNotRunningException, ZooKeeperConnectionException,
IOException {
HBaseAdmin admin= newHBaseAdmin(conf);if(admin.tableExists(tableName)) {
System.out.println("表已经存在!");
}else{
HTableDescriptor tableDesc= newHTableDescriptor(
TableName.valueOf(tableName));for (int i = 0; i < cfs.length; i++) {
tableDesc.addFamily(newHColumnDescriptor(cfs[i]));
}
admin.createTable(tableDesc);
admin.close();
System.out.println("表创建成功!");
}
}/*** 删除表操作*/
public voiddeleteTable(String tableName) {
HBaseAdmin admin;try{
admin= newHBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
admin.close();
System.out.println("表删除成功!");
}catch(MasterNotRunningException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(ZooKeeperConnectionException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/*** 插入一行记录*/
public voidwriteRow(String tableName, String[] cfs) {try{
HTable table= newHTable(conf, tableName);
Put put= new Put(Bytes.toBytes("row1"));for (int j = 0; j < cfs.length; j++) {
put.add(Bytes.toBytes(cfs[j]),
Bytes.toBytes(String.valueOf(1)),
Bytes.toBytes("value_1"));
}
table.close();
System.out.println("添加成功!");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/*** 删除一行记录*/
public voiddeleteRow(String tableName, String rowKey) {try{
HTable table= newHTable(conf, tableName);
Delete dl= newDelete(rowKey.getBytes());
table.delete(dl);
table.close();
System.out.println("删除行成功!");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/*** 查找一条记录*/
public static voidselectRow(String tableName, String rowKey) {try{
HTable table= newHTable(conf, tableName);
Get g= newGet(rowKey.getBytes());
Result rs=table.get(g);
System.out.println(rs);
table.close();
System.out.println("查询完成!");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/*** 查询表中所有的行*/
public voidscanner(String tableName) {try{
HTable table= newHTable(conf, tableName);
Scan s= newScan();
ResultScanner rs=table.getScanner(s);for(Result r : rs) {//keyvalue
Cell[] cell =r.rawCells();
System.out.println("长度:" +cell.length);for (int i = 0; i < cell.length; i++) {
System.out.println("信息:"
+ new String(CellUtil.cloneFamily(cell[i])) + " "
+ newString(CellUtil.cloneQualifier(cell[i]))+ " " + newString(CellUtil.cloneValue(cell[i]))+ " " + newString(CellUtil.cloneRow(cell[i])));
}
System.out.println("\n-----------------------");
}
table.close();
System.out.println("执行结束!");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}public static voidmain(String[] args) {
HbaseTest hbase= newHbaseTest();
String tableName= "test";
hbase.scanner(tableName);
}
}