ivaneeo's blog

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

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

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

          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.根據(jù)行名name獲得一行數(shù)據(jù),存入Result.注意HBase中的表數(shù)據(jù)是字節(jié)存儲的。

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

                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.向表中存數(shù)據(jù)

                下面的例子表示寫入一行。行名為abcd,famA列族col1列的數(shù)據(jù)為"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):便于獲得自己需要的數(shù)據(jù),相當于SQL查詢。

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

                HTable hTable = new HTable("test");  

                //表示要查詢的行名是從a開始,到z結(jié)束。
                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列的數(shù)據(jù)等于"hello world!"
                
          SingleColumnValueFilter singleColumnValueFilterA = new SingleColumnValueFilter(
                     famA, col1, CompareOp.EQUAL, Bytes.toBytes("hello world!"));
                singleColumnValueFilterA.setFilterIfMissing(true);  

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

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

                scan.setFilter(filter);  

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

          5.上面的代碼容易出錯的地方在于,需要導入HBase的類所在的包。導入時需要選擇包,由于類可能出現(xiàn)在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)時間戳到時間的轉(zhuǎn)換.單一的時間戳無法給出直觀的解釋。

          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)時間到時間戳的轉(zhuǎn)換。注意時間是字符串格式。字符串與時間的相互轉(zhuǎn)換,此不贅述

          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 閱讀(504) 評論(0)  編輯  收藏 所屬分類:
          主站蜘蛛池模板: 伊宁市| 泗洪县| 洞头县| 澄城县| 松潘县| 即墨市| 昌江| 德令哈市| 湖州市| 元江| 农安县| 天门市| 若尔盖县| 独山县| 石屏县| 康定县| 县级市| 石阡县| 那坡县| 沂水县| 商丘市| 通化市| 淮安市| 海宁市| 三都| 邻水| 彭泽县| 吉木萨尔县| 四子王旗| 宁陕县| 漯河市| 方山县| 大新县| 新乐市| 冕宁县| 庆城县| 尉犁县| 丰镇市| 辽中县| 义乌市| 嘉善县|