HBase-各种API操作

初始化 
  1. Configuration HBASE_CONFIG = new Configuration();  
  2. HBASE_CONFIG.set("hbase.zookeeper.quorum""IP");  
  3. HBASE_CONFIG.set("hbase.zookeeper.property.clientPort""端口");  
  4. cfg = new HBaseConfiguration(HBASE_CONFIG);  
  5. cfg.set("hbase.master""master ip:60000");  




创建表 
  1. public void createTable(String tablename) throws IOException {  
  2.     HBaseAdmin admin = new HBaseAdmin(cfg);  
  3.     if (admin.tableExists(tablename)) {  
  4.         System.out.println("Exists!!!");  
  5.     } else {  
  6.         HTableDescriptor tableDesc = new HTableDescriptor(tablename);  
  7.         tableDesc.addFamily(new HColumnDescriptor("name"));  
  8.         admin.createTable(tableDesc);  
  9.         System.out.println("create table ok .");  
  10.     }  
  11. }  



查询表的所有数据 
  1. public void getAll(String tablename) throws IOException {  
  2.     HTable table = new HTable(cfg, tablename);  
  3.     Scan s = new Scan();  
  4.     ResultScanner rs = table.getScanner(s);  
  5.     for (Result r : rs) {  
  6.         KeyValue[] kv = r.raw();  
  7.         for (int i = 0; i < kv.length; i++) {  
  8.             System.out.print(new String(kv[i].getRow()) + "\t");  
  9.             System.out.print("column=" + new String(kv[i].getFamily())+ ":");  
  10.             System.out.print(new String(kv[i].getQualifier()) + ", ");  
  11.             System.out.print("timestamp=" + kv[i].getTimestamp() + ", ");  
  12.             System.out.println("value=" + new String(kv[i].getValue()));  
  13.         }  
  14.     }  
  15. }  



查询一条数据 
  1. public void get(String tablename, String rowKey) throws IOException {  
  2.     HTable table = new HTable(cfg, tablename);  
  3.     Get g = new Get(rowKey.getBytes());  
  4.     Result rs = table.get(g);  
  5.     for (KeyValue kv : rs.raw()) {  
  6.         System.out.print(new String(kv.getRow()) + ":");  
  7.         System.out.print("column=" + new String(kv.getFamily()) + ":");  
  8.         System.out.print(new String(kv.getQualifier()) + ", ");  
  9.         System.out.print("timestamp=" + kv.getTimestamp() + ", ");  
  10.         System.out.println("value=" + new String(kv.getValue()));  
  11.     }  
  12. }  



更新数据 
  1. public void update(String tablename,String rowKey,String value) throws IOException {  
  2.     HTable table = new HTable(cfg, tablename);  
  3.     Put put = new Put(Bytes.toBytes(rowKey));  
  4.     put.add(Bytes.toBytes("name"), Bytes.toBytes("column"),Bytes.toBytes(value));  
  5.     table.put(put);  
  6. }  



删除一条数据(也可以删除多条) 
  1. public void delete(String tablename, String rowKey) throws IOException {  
  2.     HTable table = new HTable(cfg, tablename);  
  3.     List list = new ArrayList();  
  4.     Delete d1 = new Delete(rowKey.getBytes());  
  5.     list.add(d1);  
  6.     table.delete(list);  
  7. }  




查询多条 
  1. public void multiGet(String tablename,String[] keys) throws IOException {  
  2.     HTable table = new HTable(cfg, tablename);  
  3.     List list = new ArrayList();  
  4.     for(String k:keys) {  
  5.         Get g = new Get(k.getBytes());  
  6.         list.add(g);  
  7.     }  
  8.     Result[] rs = table.get(list);  
  9.        }  



删除表 
  1. public void removeTable(String tablename) throws IOException {  
  2.     HBaseAdmin admin = new HBaseAdmin(cfg);  
  3.     admin.disableTable(tablename);  
  4.     admin.deleteTable(tablename);  
  5. }  



增加过滤器 
  1. public void filter(String tablename, String key,int count) throws IOException {  
  2.     HTable table = new HTable(cfg, tablename);  
  3.     Scan s = new Scan();  
  4.     List list = new ArrayList();  
  5.     list.add(new PrefixFilter(key.getBytes()));  
  6.     list.add(new PageFilter(count));  
  7.     FilterList filter = new FilterList(list);  
  8.     s.setFilter(filter);  
  9.     ResultScanner rs = table.getScanner(s);  
  10.         }  

你可能感兴趣的:(分布式,HBase)