Hbase基本操作示例

Hadoop Hbase通过行关键字、列(列族名:列名)和时间戳的三元组确定一个存储单元(cell),即由

{row key, column family, column name, timestamp} 可以唯一确定一个存储值,即一个键值对:

{row key, column family, column name, timestamp} -> value


下面演示了Hbase的基本操作。包括

1、创建表

2、删除表

3、添加记录

4、删除记录

5、查询记录等


忘记的时候可以过来看看。

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.hbase.HBaseConfiguration;  
  7. import org.apache.hadoop.hbase.HColumnDescriptor;  
  8. import org.apache.hadoop.hbase.HTableDescriptor;  
  9. import org.apache.hadoop.hbase.KeyValue;  
  10. import org.apache.hadoop.hbase.client.Delete;  
  11. import org.apache.hadoop.hbase.client.Get;  
  12. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  13. import org.apache.hadoop.hbase.client.HTable;  
  14. import org.apache.hadoop.hbase.client.Put;  
  15. import org.apache.hadoop.hbase.client.Result;  
  16. import org.apache.hadoop.hbase.client.ResultScanner;  
  17. import org.apache.hadoop.hbase.client.Scan;  
  18.   
  19. public class HBaseOperation {  
  20.   
  21.     // 相关属性  
  22.     private Configuration conf;  
  23.     private HBaseAdmin admin;  
  24.   
  25.     public HBaseOperation(Configuration conf) throws IOException {  
  26.         this.conf = HBaseConfiguration.create(conf);  
  27.         this.conf.set("hbase.zookeeper.quorum""namenode");  
  28.         this.conf.set("hbase.zookeeper.property.clientPort""2181");  
  29.         this.admin = new HBaseAdmin(this.conf);  
  30.     }  
  31.   
  32.     public HBaseOperation() throws IOException {  
  33.         Configuration cnf = new Configuration();  
  34.         this.conf = HBaseConfiguration.create(cnf);  
  35.         this.conf.set("hbase.zookeeper.quorum""namenode");  
  36.         this.conf.set("hbase.zookeeper.property.clientPort""2181");  
  37.         this.admin = new HBaseAdmin(this.conf);  
  38.     }  
  39.   
  40.     /** 
  41.      * 创建表 
  42.      * @param tableName     表名 
  43.      * @param colFamilies   <span style="white-space:pre">    </span>列族 
  44.      * @throws IOException 
  45.      */  
  46.     public void createTable(String tableName, String colFamilies[])  
  47.             throws IOException {  
  48.   
  49.         if (this.admin.tableExists(tableName)) { // 表已经存在  
  50.             System.out.println("Table: " + tableName + " already exists !");  
  51.             this.admin.deleteTable(tableName); // 删除表  
  52.         }  
  53.   
  54.         HTableDescriptor dsc = new HTableDescriptor(tableName); // 新建表描述  
  55.         int len = colFamilies.length;  
  56.         for (int i = 0; i < len; i++) {  
  57.             HColumnDescriptor family = new HColumnDescriptor(colFamilies[i]);  
  58.             dsc.addFamily(family); // 在表描述中添加列族  
  59.         }  
  60.         admin.createTable(dsc); // 创建表  
  61.         System.out.println("create table " + tableName + " ok.");  
  62.     }  
  63.   
  64.     /** 
  65.      * 删除一张表 
  66.      * @param tableName 
  67.      * @throws IOException 
  68.      */  
  69.     public void deleteTable(String tableName) throws IOException {  
  70.         if (this.admin.tableExists(tableName)) {  
  71.             admin.disableTable(tableName);  
  72.             admin.deleteTable(tableName);  
  73.             System.out.println("delete table " + tableName + " ok.");  
  74.         } else {  
  75.             System.out.println("Table Not Exists !");  
  76.         }  
  77.     }  
  78.   
  79.     /** 
  80.      * 添加记录 
  81.      * @param tableName  表名字 
  82.      * @param rowkey     行关键字 
  83.      * @param family     列族 
  84.      * @param qualifier 列名 
  85.      * @param value     值 
  86.      * @throws IOException 
  87.      */  
  88.     public void insertRecord(String tableName, String rowkey, String family,  
  89.             String qualifier, String value) throws IOException {  
  90.   
  91.         HTable table = new HTable(this.conf, tableName);  
  92.         Put put = new Put(rowkey.getBytes());  
  93.         put.add(family.getBytes(), qualifier.getBytes(), value.getBytes());  
  94.         table.put(put);  
  95.   
  96.         System.out.println("insert recored " + rowkey + " to table "  
  97.                 + tableName + " ok.");  
  98.         table.close();  
  99.     }  
  100.   
  101.     /** 
  102.      * 添加一组记录 
  103.      * @param tableName 
  104.      * @param putList 
  105.      * @throws IOException 
  106.      */  
  107.     public void insertRecordList(String tableName, List<Put> putList)  
  108.             throws IOException {  
  109.         HTable table = new HTable(this.conf, tableName);  
  110.   
  111.         for (int i = 0; i < putList.size(); i++) {  
  112.             Put put = putList.get(i);  
  113.             table.put(put);  
  114.         }  
  115.         putList.clear();  
  116.         table.close();  
  117.     }  
  118.   
  119.     /** 
  120.      * 删除一行数据 
  121.      * @param tableName 表名 
  122.      * @param rowkey    行关键字 
  123.      * @throws IOException 
  124.      */  
  125.     public void deleteRecord(String tableName, String rowkey)  
  126.             throws IOException {  
  127.   
  128.         HTable table = new HTable(this.conf, tableName);  
  129.         Delete del = new Delete(rowkey.getBytes());  
  130.         table.delete(del);  
  131.         System.out.println("del recored " + rowkey + " ok.");  
  132.         table.close();  
  133.     }  
  134.   
  135.     /** 
  136.      * 获取一条记录 
  137.      * @param tableName 表名 
  138.      * @param rowkey    行关键字 
  139.      * @return 
  140.      * @throws IOException 
  141.      */  
  142.     public Result getOneRecord(String tableName, String rowkey)  
  143.             throws IOException {  
  144.         HTable table = new HTable(this.conf, tableName);  
  145.         Get get = new Get(rowkey.getBytes());       //get对象  
  146.         Result rs = table.get(get);  
  147.         for (KeyValue kv : rs.raw()) {  
  148.             System.out.print(new String(kv.getRow()) + " ");  
  149.             System.out.print(new String(kv.getFamily()) + ":");  
  150.             System.out.print(new String(kv.getQualifier()) + " ");  
  151.             System.out.print(kv.getTimestamp() + " ");  
  152.             System.out.println(new String(kv.getValue()));  
  153.         }  
  154.         table.close();  
  155.         return rs;  
  156.     }  
  157.   
  158.     /** 
  159.      * 获取一张表的所有数据 
  160.      * @param tableName 
  161.      * @return 
  162.      * @throws IOException 
  163.      */  
  164.     public List<Result> getAllRecord(String tableName) throws IOException {  
  165.   
  166.         HTable table = new HTable(this.conf, tableName);  
  167.         Scan scan = new Scan();         //Scan对象  
  168.         ResultScanner scanner = table.getScanner(scan);  
  169.         List<Result> list = new ArrayList<Result>();  
  170.         for (Result r : scanner) {  
  171.             list.add(r);  
  172.         }  
  173.         scanner.close();  
  174.         table.close();  
  175.   
  176.         for (Result r : scanner) {  
  177.             for (KeyValue kv : r.raw()) {  
  178.                 System.out.print(new String(kv.getRow()) + " ");  
  179.                 System.out.print(new String(kv.getFamily()) + ":");  
  180.                 System.out.print(new String(kv.getQualifier()) + " ");  
  181.                 System.out.print(kv.getTimestamp() + " ");  
  182.                 System.out.println(new String(kv.getValue()));  
  183.             }  
  184.         }  
  185.         return list;  
  186.     }  
  187. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值