这篇文章主要介绍了hbase developer API 1.22版是怎么样的,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
示例代码
package com.woozoom.hbase.test;import java.io.IOException;import java.util.*;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;public class BaseOperation {private static final String TABLE_NAME = "demo_table";public static Configuration conf = null;public HTable table = null;public HBaseAdmin admin = null;static {conf = HBaseConfiguration.create();
}/** * 创建一张表 */ public static void creatTable(String strTN, String[] familys)throws Exception {Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
TableName tn = TableName.valueOf(strTN);if (admin.tableExists(tn)) {
System.out.println("table already exists!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tn);for (String colFamily : familys) {
tableDesc.addFamily(new HColumnDescriptor(colFamily));
}
admin.createTable(tableDesc);
System.out.println("create table " + strTN + " ok.");
}
}/** * 删除表 */ public static void deleteTable(String strTN) throws Exception {try {Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
TableName tn = TableName.valueOf(strTN);admin.disableTable(tn);
admin.deleteTable(tn);
System.out.println("delete table " + strTN + " ok.");
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
}/** * 插入一行记录 */ public static void addRecord(String strTN, String rowKey,
String family, String qualifier, String value) throws Exception {try {Connection conn = ConnectionFactory.createConnection(conf);
TableName tn = TableName.valueOf(strTN);
Table table = conn.getTable(tn);Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),
Bytes.toBytes(value));
table.put(put);
System.out.println("insert recored " + rowKey + " to table " + strTN + " ok.");
} catch (IOException e) {
e.printStackTrace();
}
}/** * 删除一行记录 */ public static void delRecord(String strTN, String rowKey)throws IOException {Connection conn = ConnectionFactory.createConnection(conf);
TableName tn = TableName.valueOf(strTN);
Table table = conn.getTable(tn);List list = new ArrayList();
Delete del = new Delete(rowKey.getBytes());
list.add(del);
table.delete(list);
System.out.println("del recored " + rowKey + " ok.");
}/** * 查找一行记录 */ public static void getOneRecord(String strTN, String rowKey)throws IOException { Connection conn = ConnectionFactory.createConnection(conf);
TableName tn = TableName.valueOf(strTN);
Table table = conn.getTable(tn);
Get get = new Get(rowKey.getBytes());
Result rs = table.get(get);for (Cell cell : rs.listCells()) {
KeyValue kv = (KeyValue)cell;
System.out.println("### " + new String(kv.getKey()) + " ###");
System.out.println("### " + new String(Arrays.copyOfRange(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyOffset() + cell.getFamilyLength())) + " ###");
System.out.println("### " + new String(Arrays.copyOfRange(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierOffset() + cell.getQualifierLength())) + " ###");
System.out.println("### " + new String(Arrays.copyOfRange(cell.getValueArray(), cell.getValueOffset(), cell.getValueOffset() + cell.getValueLength())) + " ###");
System.out.println("### " + cell.getTimestamp() + " ###");
}NavigableMap<byte[], NavigableMap<byte[], byte[]>> nm = rs.getNoVersionMap();for(byte[] family : nm.keySet()){
System.out.println(new String(family));
NavigableMap<byte[], byte[]> nmSon = nm.get(family);for (byte[] qualifier : nmSon.keySet()){
System.out.println(new String(qualifier));byte[] value = nmSon.get(qualifier);
System.out.println(new String(value));
}
}
}/** * 显示所有数据 */ public static void getAllRecord(String strTN) {try { Connection conn = ConnectionFactory.createConnection(conf);
TableName tn = TableName.valueOf(strTN);
Table table = conn.getTable(tn);
Scan s = new Scan();
ResultScanner ss = table.getScanner(s);for (Result r : ss) {for (Cell cell : r.listCells()) {
KeyValue kv = (KeyValue)cell;
System.out.print(new String(kv.getKey()) + " ");
System.out.print(new String(Arrays.copyOfRange(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyOffset() + kv.getFamilyLength())) + ":");
System.out.print(new String(Arrays.copyOfRange(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierOffset() + kv.getQualifierLength())) + " ");
System.out.print(kv.getTimestamp() + " ");
System.out.println(new String(Arrays.copyOfRange(kv.getValueArray(), kv.getValueOffset(), kv.getValueOffset() + kv.getValueLength())));
}
}} catch (IOException e) {
e.printStackTrace();
}
}public static void main(String[] agrs) {try {
String tablename = "scores1";
String[] familys = {"grade", "course"};
BaseOperation.creatTable(tablename, familys);// add record zkb BaseOperation.addRecord(tablename, "zkb", "grade", "", "5");
BaseOperation.addRecord(tablename, "zkb", "course", "", "90");
BaseOperation.addRecord(tablename, "zkb", "course", "math", "97");
BaseOperation.addRecord(tablename, "zkb", "course", "art", "87");// add record baoniu BaseOperation.addRecord(tablename, "baoniu", "grade", "", "4");
BaseOperation
.addRecord(tablename, "baoniu", "course", "math", "89");
System.out.println("===========get one record========");
BaseOperation.getOneRecord(tablename, "zkb");
System.out.println("===========show all record========");
BaseOperation.getAllRecord(tablename);
System.out.println("===========del one record========");
BaseOperation.delRecord(tablename, "baoniu");
BaseOperation.getAllRecord(tablename);
System.out.println("===========show all record========");
BaseOperation.getAllRecord(tablename);
} catch (Exception e) {
e.printStackTrace();
}
}
}
感谢你能够认真阅读完这篇文章,希望小编分享的“hbase developer API 1.22版是怎么样的”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/dongtianxi/blog/726901