<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ivaneeo's blog

    自由的力量,自由的生活。

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

    做了幾天工程,對HBase中的表操作熟悉了一下。下面總結一下常用的表操作和容易出錯的幾個方面。當然主要來源于大牛們的文章。我在前人的基礎上稍作解釋。

    1.連接HBase中的表testtable,用戶名:root,密碼:root

    public void ConnectHBaseTable()
     {
      Configuration conf = new Configuration();       
            conf.set("hadoop.job.ugi", "root,root");      
      HBaseConfiguration config = new HBaseConfiguration();
      try
      {
       table = new HTable(config, "testtable");
      }catch(Exception e){e.printStackTrace();}
     }

    2.根據行名name獲得一行數據,存入Result.注意HBase中的表數據是字節存儲的。

       下面的例子表示獲得行名為name的行的famA列族col1列的數據。

          String rowId = "name";
          Get get = new Get(rowId);
          Result result = hTable.get(get);
          byte[] value = result.getValue(famA, col1);
          System.out.println(Bytes.toString(value));

    3.向表中存數據

          下面的例子表示寫入一行。行名為abcd,famA列族col1列的數據為"hello world!"。

          byte[] rowId = Bytes.toBytes("abcd");
          byte[] famA = Bytes.toBytes("famA");
          byte[] col1 = Bytes.toBytes("col1");
          Put put = new Put(rowId).
             add(famA, col1, Bytes.toBytes("hello world!"));
          hTable.put(put);
         

    4.掃描的用法(scan):便于獲得自己需要的數據,相當于SQL查詢。

          byte[] famA = Bytes.toBytes("famA");
          byte[] col1 = Bytes.toBytes("col1");  

          HTable hTable = new HTable("test");  

          //表示要查詢的行名是從a開始,到z結束。
          Scan scan = new Scan(Bytes.toBytes("a"), Bytes.toBytes("z"));
         

          //用scan.setStartRow(Bytes.toBytes(""));設置起始行

          //用scan.setStopRow(Bytes.toBytes(""));設置終止行

          //表示查詢famA族col1列

          scan.addColumn(famA, col1);  

          //注意,下面是filter的寫法。相當于SQL的where子句

          //表示famA族col1列的數據等于"hello world!"
          
    SingleColumnValueFilter singleColumnValueFilterA = new SingleColumnValueFilter(
               famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello world!"));
          singleColumnValueFilterA.setFilterIfMissing(true);  

          //表示famA族col1列的數據等于"hello hbase!"
          
    SingleColumnValueFilter singleColumnValueFilterB = new SingleColumnValueFilter(
               famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello hbase!"));
          singleColumnValueFilterB.setFilterIfMissing(true);  
          

          //表示famA族col1列的數據是兩者中的一個
          FilterList filter = new FilterList(Operator.MUST_PASS_ONE, Arrays
               .asList((Filter) singleColumnValueFilterA,
                    singleColumnValueFilterB));  

          scan.setFilter(filter);  

          ResultScanner scanner = hTable.getScanner(scan);  
          //遍歷每個數據
          for (Result result : scanner) {
             System.out.println(Bytes.toString(result.getValue(famA, col1)));
          }

    5.上面的代碼容易出錯的地方在于,需要導入HBase的類所在的包。導入時需要選擇包,由于類可能出現在HBase的各個子包中,所以要選擇好,下面列出常用的包。盡量用HBase的包

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.Get;
    import org.apache.hadoop.hbase.client.HTable;
    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.filter.Filter;
    import org.apache.hadoop.hbase.filter.FilterList;
    import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
    import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
    import org.apache.hadoop.hbase.filter.FilterList.Operator;
    import org.apache.hadoop.hbase.util.Bytes;

    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Arrays;
    import java.util.Date;

    6.下面列出HBase常用的操作

    (1)時間戳到時間的轉換.單一的時間戳無法給出直觀的解釋。

    public String GetTimeByStamp(String timestamp)
     {

      long datatime= Long.parseLong(timestamp); 
         Date date=new Date(datatime);   
         SimpleDateFormat   format=new   SimpleDateFormat("yyyy-MM-dd HH:MM:ss");   
         String timeresult=format.format(date);
         System.out.println("Time : "+timeresult);
         return timeresult;
     }

    (2)時間到時間戳的轉換。注意時間是字符串格式。字符串與時間的相互轉換,此不贅述

    public String GetStampByTime(String time)
     {
      String Stamp="";
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date date;
      try
      {
       date=sdf.parse(time);
       Stamp=date.getTime()+"000";
       System.out.println(Stamp);
      }catch(Exception e){e.printStackTrace();}
      return Stamp;
     }

    上面就是我的一點心得。以后碰到什么問題,再來解決。

    參考文獻:http://www.nearinfinity.com/blogs/aaron_mccurry/using_hbase-dsl.html

    posted on 2011-06-15 17:17 ivaneeo 閱讀(506) 評論(0)  編輯  收藏 所屬分類:
    主站蜘蛛池模板: jzzijzzij在线观看亚洲熟妇| 亚洲 无码 在线 专区| 亚洲AV综合色区无码另类小说| 国产成人亚洲精品电影| 国产精品酒店视频免费看| 亚洲国产高清国产拍精品| 18禁无遮挡无码网站免费| 亚洲欧美成人av在线观看| 国产免费久久精品久久久| 亚洲乱色伦图片区小说| 国产免费av片在线无码免费看| 久久水蜜桃亚洲AV无码精品| 中文字幕av免费专区| 久久亚洲国产精品一区二区| 国产成人免费AV在线播放 | 午夜免费福利网站| 亚洲精华国产精华精华液好用| 日本一区免费电影| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 婷婷亚洲天堂影院| 成人免费视频观看无遮挡| 亚洲另类小说图片| 热99re久久精品精品免费| 看免费毛片天天看| 亚洲成色在线综合网站| 四虎精品视频在线永久免费观看| 亚洲男人的天堂久久精品| 国产hs免费高清在线观看| 国产在线观看无码免费视频| 久久亚洲AV成人无码软件| 久久精品国产亚洲AV天海翼 | 成人毛片视频免费网站观看| 另类专区另类专区亚洲| 国产亚洲精久久久久久无码| 88av免费观看入口在线| 久久精品国产亚洲av天美18 | 一进一出60分钟免费视频| 亚洲国产精品久久66| 好吊妞在线成人免费| 中文字幕免费观看视频| 亚洲人和日本人jizz|