posts - 495,comments - 227,trackbacks - 0
          http://jeffxie.blog.51cto.com/1365360/305538

          我在Hadoop的用戶郵件列表中看到一些國內的用 戶在訊問一些關于如何操作的HBase問題,還看到了HBase中沒有Example。覺得有 必要跟大家分享自己的經驗。
          在下面的例子中我們分析Apache的log并把這些log進行分析并把分析完 的結果按用戶IP為ROW,把log中用戶的訪問時間,請求方法,用戶請求的協議,用戶的瀏覽器,服務狀態等寫到HBase的表中。


          首先我們要在HBase中建立我們的一個表來存儲數據。  
          1. public static void creatTable(String table) throws IOException{
          2.             HConnection conn = HConnectionManager.getConnection(conf);
          3.             HBaseAdmin admin = new HBaseAdmin(conf);
          4.             if(!admin.tableExists(new Text(table))){
          5.               System.out.println("1. " + table + " table creating ... please wait");
          6.               HTableDescriptor tableDesc = new HTableDescriptor(table);
          7.               tableDesc.addFamily(new HColumnDescriptor("http:"));
          8.               tableDesc.addFamily(new HColumnDescriptor("url:"));
          9.               tableDesc.addFamily(new HColumnDescriptor("referrer:"));
          10.               admin.createTable(tableDesc);
          11.             } else {
          12.               System.out.println("1. " + table + " table already exists.");
          13.             }
          14.             System.out.println("2. access_log files fetching using map/reduce");
          15.   }
          復制代碼

          然后我們運行一個MapReduce任務來取得log中的每一行 數據。因為我們只要取得數據而不需要對結果進行規約,我們只要編寫一個Map程序即可。   
          1. public static class MapClass extends MapReduceBase implements
          2.       Mapper<WritableComparable, Text, Text, Writable> {

          3.     @Override
          4.     public void configure(JobConf job) {
          5.       tableName = job.get(TABLE, "");
          6.     }

          7.     public void map(WritableComparable key, Text value,
          8.         OutputCollector<Text, Writable> output, Reporter reporter)
          9.         throws IOException {
          10.       try {
          11.              AccessLogParser log = new AccessLogParser(value.toString());
          12.         if(table==null)
          13.                 table = new HTable(conf, new Text(tableName));
          14.         long lockId = table.startUpdate(new Text(log.getIp()));
          15.         table.put(lockId, new Text("http:protocol"), log.getProtocol().getBytes());
          16.         table.put(lockId, new Text("http:method"), log.getMethod().getBytes());
          17.         table.put(lockId, new Text("http:code"), log.getCode().getBytes());
          18.         table.put(lockId, new Text("http:bytesize"), log.getByteSize().getBytes());
          19.         table.put(lockId, new Text("http:agent"), log.getAgent().getBytes());
          20.         table.put(lockId, new Text("url:" + log.getUrl()), log.getReferrer().getBytes());
          21.         table.put(lockId, new Text("referrer:" + log.getReferrer()), log.getUrl().getBytes());

          22.         table.commit(lockId, log.getTimestamp());
          23.       } catch (ParseException e) {
          24.         e.printStackTrace();
          25.       } catch (Exception e) {
          26.         e.printStackTrace();
          27.       }
          28.     }
          29.   }
          復制代碼

          我們在Map程序中對于傳進來的每一行先交給AccessLogParser去處理在AccessLogParser德構造器中用一個正則表達式"([^ ]*) ([^ ]*) ([^ ]*) \\[([^]]*)\\] \"([^\"]*)\" " ([^ ]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\".*"來匹配每一行的log。接下來我們把這些AccessLogParser處理出來的結果更新到HBase的表中去,好的, 我們的程序寫完了。我們要啟動一個MapReduce的話我們要對工作進行配置。   
          1. public static void runMapReduce(String table,String dir) throws IOException{
          2.           Path tempDir = new Path("log/temp");
          3.           Path InputDir = new Path(dir);
          4.           FileSystem fs = FileSystem.get(conf);
          5.           JobConf jobConf = new JobConf(conf, LogFetcher.class);
          6.           jobConf.setJobName("apache log fetcher");
          7.           jobConf.set(TABLE, table);
          8.           Path[] in = fs.listPaths(InputDir);
          9.           if (fs.isFile(InputDir)) {
          10.               jobConf.setInputPath(InputDir);
          11.           } else {
          12.               for (int i = 0; i < in.length; i++) {
          13.                 if (fs.isFile(in[i])) {
          14.                   jobConf.addInputPath(in[i]);
          15.                 } else {
          16.                   Path[] sub = fs.listPaths(in[i]);
          17.                   for (int j = 0; j < sub.length; j++) {
          18.                     if (fs.isFile(sub[j])) {
          19.                       jobConf.addInputPath(sub[j]);
          20.                     }
          21.                   }
          22.                 }
          23.               }
          24.             }
          25.             jobConf.setOutputPath(tempDir);
          26.             jobConf.setMapperClass(MapClass.class);

          27.             JobClient client = new JobClient(jobConf);
          28.             ClusterStatus cluster = client.getClusterStatus();
          29.             jobConf.setNumMapTasks(cluster.getMapTasks());
          30.             jobConf.setNumReduceTasks(0);

          31.             JobClient.runJob(jobConf);
          32.             fs.delete(tempDir);
          33.             fs.close();
          34.   }
          復制代碼

          在上面的代碼中我們先產生一個jobConf對象,然后設定我們的InputPath和OutputPath,告訴MapReduce我們的Map類,設 定我們用多少個Map任務和Reduce任務,然后我們不任務提交給JobClient,關于MapReduce跟詳細的資料Hadoop Wiki上。
          下載:源碼和已編譯好的jar文件example-src.tgz
          例子的運行命令是:

          bin/hadoop jar examples.jar logfetcher <access_log file or directory> <table_name>

          如何運行上面的應用程序呢?我們假定解壓縮完Hadoop分發包的目錄為%HADOOP%
          拷貝%HADOOP%\contrib\hbase\bin下的文件到%HADOOP%\bin下,拷貝%HADOOP%\contrib\hbase \conf的文件到%HADOOP%\conf下,拷貝%HADOOP%\src\contrib\hbase\lib的文件到%HADOOP%\lib 下,拷貝%HADOOP%\src\contrib\hbase\hadoop-*-hbase.jar的文件到%HADOOP%\lib下.然后編輯配 置文件hbase-site.xml設定你的hbase.master例子:192.168.2.92:60000。把這些文件分發到運行Hadoop的 機器上去。在regionservers文件添加上這些已分發過的地址。運行bin/start-hbase.sh命令啟動HBase,把你的 apache log文件拷貝到HDFS的apache-log目錄下,等啟動完成后運行下面的命令。

          bin/hadoop jar examples.jar logfetcher apache-log apache

          訪問http://localhost:50030/能 看到你的MapReduce任務的運行情況,訪問http://localhost:60010/能 看到HBase的運行情況。

          hbaseguiinterface.jpg

          等任務MapReduce完成后訪問http://localhost:60010/hql.jsp,在Query輸入框中輸入 SELECT * FROM apache limit=50;。將會看到已經插入表中的數據。 hqlguiinterface.jpg
          posted on 2013-02-22 14:12 SIMONE 閱讀(2470) 評論(0)  編輯  收藏 所屬分類: hbase
          主站蜘蛛池模板: 屯留县| 林周县| 江西省| 武平县| 神木县| 永兴县| 合水县| 岳西县| 上栗县| 迁安市| 丘北县| 湖州市| 正阳县| 崇左市| 嘉兴市| 阳东县| 华宁县| 探索| 福州市| 岳西县| 安陆市| 内江市| 公主岭市| 阳江市| 昌乐县| 江西省| 彭阳县| 贡觉县| 南宁市| 乌海市| 麦盖提县| 清水县| 北宁市| 元谋县| 田东县| 高雄县| 泰来县| 昌乐县| 宾阳县| 电白县| 白沙|