HBase API操作表和数据
package HomeWork1;
import java.io.IOException;
import java.util.List;
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import iter.HBaseDemoInterface;
import util.HBasePrintUtil;
public class Work1 implements HBaseDemoInterface{
private static final String cOnnectkey= "hbase.zookeeper.quorum";
private static final String cOnnectvalue= "potter2:2181,potter3:2181,potter4:2181,potter5:2181,";
static Configuration cOnf= null;
private static Admin admin = null;
private static Connection con = null;
static{
cOnf= HBaseConfiguration.create();
conf.set(connectkey, connectvalue);
try {
con = ConnectionFactory.createConnection(conf);
admin = con.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 查询所有表
@Override
public void getAllTables() throws Exception {
HTableDescriptor[] listTables = admin.listTables();
for(HTableDescriptor htd : listTables){
System.out.print(htd.getTableName()+"\t");
}
}
// 创建表,传参,表名和列簇的名字
@Override
public void createTable(String tableName, String[] family) throws Exception {
TableName tn = TableName.valueOf(tableName);
HTableDescriptor htd = new HTableDescriptor(tn);
for (int i = 0; i putList) throws Exception {
Table table = con.getTable(TableName.valueOf("user_info"));
Put put1 = new Put("rk07".getBytes());
put1.addColumn("base_info".getBytes(), "kk1".getBytes(), "pp1".getBytes());
Put put2 = new Put("rk077".getBytes());
put2.addColumn("base_info".getBytes(), "kk2".getBytes(), "pp2".getBytes());
putList.add(put1);
putList.add(put2);
table.put(putList);
System.out.println("成功");
}
// 根据rowkey查询数据
@Override
public Result getResult(String tableName, String rowKey) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn);
Get get = new Get(Bytes.toBytes(rowKey));
// System.out.println(get+"0000");
Result result = table.get(get);
// System.out.println(result+"2222222222222");
HBasePrintUtil.printResult(result);
// Cell[] rawCells = result.rawCells();
// for(Cell cell : rawCells){
// System.out.println(cell.toString());
// System.out.println(cell.getF);
// }
// Scan scan = new Scan();
// HTableDescriptor htd = new HTableDescriptor(tn);
return result;
}
// 根据rowkey查询数据
@Override
public Result getResult(String tableName, String rowKey, String familyName) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn);
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(familyName));
Result result = table.get(get);
HBasePrintUtil.printResult(result);
return null;
}
@Override
public Result getResult(String tableName, String rowKey, String familyName, String columnName) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn);
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(familyName));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
Result result = table.get(get);
HBasePrintUtil.printResult(result);
return null;
}
// 查询指定version
@Override
public Result getResultByVersion(String tableName, String rowKey, String familyName,
String columnName, int versions) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn );
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(familyName));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));
get.setMaxVersions(versions);
Result result = table.get(get);
HBasePrintUtil.printResult(result);
return null;
}
// scan全表数据
@Override
public ResultScanner getResultScann(String tableName) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn);
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
return null;
}
@Override
public ResultScanner getResultScann(String tableName, Scan scan) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
return null;
}
// 删除数据(指定的列)
@Override
public void deleteColumn(String tableName, String rowKey) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn);
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
System.out.println("删除成功");
}
// 删除数据(指定的列)
@Override
public void deleteColumn(String tableName, String rowKey, String falilyName) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn);
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addFamily(Bytes.toBytes(falilyName));
table.delete(delete);
System.out.println("删除成功");
}
// 删除数据(指定的列)
@Override
public void deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws Exception {
TableName tn = TableName.valueOf(tableName);
Table table = con.getTable(tn);
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(falilyName), Bytes.toBytes(columnName));
table.delete(delete);
System.out.println("删除成功");
}
}
测试类:
package HomeWork1;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.yecht.Data.Str;
public class Test1 {
public static void main(String[] args) throws Exception {
Work1 work1 = new Work1();
work1.getAllTables();
/*String tableName = "blog";
String[] family = {"article","author"};
work1.createTable(tableName, family);*/
// work1.descTable("user_info");
// work1.existTable("aaa");
// work1.disableTable("blog");
// work1.dropTable("blog");
// work1.modifyTable("stu");
/*String[] addColumn = {"xxxx","xxxx"};
String[] removeColumn = {"xxx"};
work1.modifyTable("user_info", addColumn, removeColumn);*/
/*String[] value = { "yyy","yyx"};
String[] column = {"base_info","zzz"};
work1.addData("user_info", "rk055", column,value);*/
/*String rowKey = "rk022";
String familyName = "extra_info";
String columnName = "qqqqq";
String value = "lllll";
work1.putData("user_info", rowKey, familyName, columnName, value);
*/
// work1.putData("user_info", "rk08", "base_info", "ttt", "ooo", 999);
// work1.putData(new Put("rk09".getBytes()));
/*List pList = new ArrayList<>();
work1.putData(pList);*/
// work1.getResult("user_info", "zhangsan_20150701_0004");
// work1.getResult("user_info", "zhangsan_20150701_0004", "base_info");
// work1.getResult("user_info", "zhangsan_20150701_0004", "base_info","age");
// work1.getResultByVersion("user_info", "zhangsan_20150701_0004", "base_info","age",4);
// work1.getResultScann("user_info");
/*Scan scan = new Scan();
work1.getResultScann("user_info",scan);*/
// work1.deleteColumn("user_info", "rk01");
// work1.deleteColumn("user_info", "zhangsan_20150701_0001","base_info");
// work1.deleteColumn("user_info", "rk07","base_info","kk1");
}
}