zoukankan      html  css  js  c++  java
  • HBase操作一

      1 package Hbase;
      2 
      3 import java.io.IOException;
      4 import org.apache.hadoop.conf.Configuration;
      5 import org.apache.hadoop.hbase.Cell;
      6 import org.apache.hadoop.hbase.CellUtil;
      7 import org.apache.hadoop.hbase.HBaseConfiguration;
      8 import org.apache.hadoop.hbase.client.Delete;
      9 import org.apache.hadoop.hbase.client.Get;
     10 import org.apache.hadoop.hbase.client.HTable;
     11 import org.apache.hadoop.hbase.client.Put;
     12 import org.apache.hadoop.hbase.client.Result;
     13 import org.apache.hadoop.hbase.client.ResultScanner;
     14 import org.apache.hadoop.hbase.client.Scan;
     15 import org.apache.hadoop.hbase.util.Bytes;
     16 import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.GetDataEncryptionKeyRequestProto;
     17 import org.apache.hadoop.io.IOUtils;
     18 
     19 public class HbaseOperation {
     20 
     21     public static HTable getHTableByTableName(String tableName) throws IOException {
     22         // get instance of default configuration
     23         Configuration configuration = HBaseConfiguration.create();
     24         // get table instance
     25         HTable table = new HTable(configuration, tableName);
     26         
     27         return table;
     28     }
     29     
     30     public static void getData(String tableName) throws IOException{
     31         //String tableName = "user";
     32         HTable table = getHTableByTableName(tableName);
     33         //create get with rowkey
     34         Get rowkey = new Get(Bytes.toBytes("10001"));
     35         //****************************************************
     36         //add column
     37         rowkey.addColumn(//
     38                 Bytes.toBytes("info"),//
     39                 Bytes.toBytes("name")
     40                 );
     41         
     42         rowkey.addColumn(//
     43                 Bytes.toBytes("info"),//
     44                 Bytes.toBytes("age")
     45                 );
     46         
     47         //get data
     48         Result result = table.get(rowkey);
     49         //key : rewkey + cf +c +version
     50         //value :value
     51         for(Cell cell:result.rawCells()){
     52             System.out.println(//
     53                Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
     54                + Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->" //
     55                + Bytes.toString(CellUtil.cloneValue(cell))
     56             );
     57         }
     58         //table close
     59         table.close();
     60 
     61     }
     62     
     63     public static void putData(String tableName) throws IOException {
     64         //String tableName = "user";
     65         HTable table = getHTableByTableName(tableName);
     66         
     67         Put put = new Put(Bytes.toBytes("10003"));
     68         //add a column with value
     69         put.add(//
     70             Bytes.toBytes("info"),//
     71             Bytes.toBytes("name"),//
     72             Bytes.toBytes("wangwu")//
     73             );
     74         
     75         put.add(//
     76                 Bytes.toBytes("info"),//
     77                 Bytes.toBytes("age"),//
     78                 Bytes.toBytes("26")//
     79                 );
     80         
     81         put.add(//
     82                 Bytes.toBytes("info"),//
     83                 Bytes.toBytes("address"),//
     84                 Bytes.toBytes("tianjing")//
     85                 );
     86         
     87         table.put(put);
     88         
     89         table.close();
     90     }
     91     
     92     public static void deleteData(String tableName) throws IOException {
     93         HTable table = getHTableByTableName(tableName);
     94         Delete delete = new Delete(Bytes.toBytes("10003"));
     95         //delete a certain column 
     96 //        delete.deleteColumn(Bytes.toBytes("info"), //
     97 //                Bytes.toBytes("address"));
     98         
     99         //delete a familycolumn
    100                 delete.deleteFamily(Bytes.toBytes("info"));
    101                 
    102         table.delete(delete);
    103         table.close();
    104     }
    105 
    106     public static void main(String[] args) throws IOException {
    107         String tableName = "user";
    108 //        HTable table = getHTableByTableName(tableName);
    109 //        getData(tableName);
    110 //        putData(tableName);
    111 //        deleteData(tableName);
    112         
    113         HTable table = null;
    114         ResultScanner resultScanner = null;
    115         try{
    116             table = getHTableByTableName(tableName);
    117             
    118             Scan scan = new Scan();
    119             //the range of scan
    120             scan.setStartRow(Bytes.toBytes("10001"));
    121             scan.setStartRow(Bytes.toBytes("10004"));
    122             
    123             //scan the certain column or familycolumn
    124 //            scan.addColumn(family, qualifier);
    125 //            scan.addFamily(family);
    126             
    127             //another way to scan
    128             //Scan scan2 = new Scan(startRow, stopRow);
    129             
    130             //PrefixFilter
    131             //PageFilter
    132 //            scan.setFilter(filter);
    133             
    134 //            scan.setCacheBlocks(cacheBlocks);
    135 //            scan.setCaching(caching);
    136             
    137             resultScanner = table.getScanner(scan);
    138             for (Result result:resultScanner) {
    139                 System.out.println(Bytes.toString(result.getRow()));
    140                 //System.out.println(result);
    141                 
    142                 for(Cell cell:result.rawCells()){
    143                     System.out.println(//
    144                        Bytes.toString(CellUtil.cloneFamily(cell)) + ":" //
    145                        + Bytes.toString(CellUtil.cloneQualifier(cell)) + "-->" //
    146                        + Bytes.toString(CellUtil.cloneValue(cell))
    147                     );
    148                 }
    149                 System.out.println("------------------------------");
    150             }
    151         }catch(Exception e){
    152             e.printStackTrace();
    153         }finally{
    154             IOUtils.closeStream(resultScanner);
    155             IOUtils.closeStream(table);
    156         }
    157     }
    158 }
  • 相关阅读:
    201671030122杨凡亿《英文文本统计分析》结对项目报告
    201671030122杨凡亿 实验二 词频统计软件项目报告
    201671030122杨凡亿 实验三作业互评与改进报告
    通读《现代软件工程—构建之法》后的疑惑
    201671030126 赵佳平 实验十四 团队项目评审&课程学习总结
    201671030126赵佳平《英文文本统计分析》结对项目报告
    201671030126 词频统计软件项目报告
    201671030126 赵佳平 实验三 作业互评与改进报告
    我的问题
    201671030128周琳 实验十四 团队项目评审&课程学习总结
  • 原文地址:https://www.cnblogs.com/perfectdata/p/10136627.html
Copyright © 2011-2022 走看看