成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

大數據架構-使用HBase和Solr配置存儲與索引

運維 數據庫運維 數據庫
HBase可以通過協處理器Coprocessor的方式向Solr發出請求,Solr對于接收到的數據可以做相關的同步:增、刪、改索引的操作,這樣就可以同時使用HBase存儲量大和Solr檢索性能高的優點了,更何況HBase和Solr都可以集群。這對海量數據存儲、檢索提供了一種方式,將存儲與索引放在不同的機器上,是大數據架構的必須品。

HBase和Solr可以通過協處理器Coprocessor的方式向Solr發出請求,Solr對于接收到的數據可以做相關的同步:增、刪、改索引的操作。將存儲與索引放在不同的機器上,這是大數據架構的必須品,但目前還有很多不懂得此道的同學,他們對于這種思想感到很新奇,不過,這絕對是好的方向,所以不懂得抓緊學習吧。

有個朋友給我的那篇博客留言,說CDH也可以做這樣的事情,我還沒有試過,他還問我要與此相關的代碼,于是我就稍微整理了一下,作為本篇文章的主要內容。關于CDH的事,我會盡快嘗試,有知道的同學可以給我留言。

下面我主要講述一下,我測試對HBase和Solr的性能時,使用HBase協處理器向HBase添加數據所編寫的相關代碼,及解釋說明。

一、編寫HBase協處理器Coprocessor

​一旦有數據postPut,就立即對Solr里相應的Core更新。這里使用了ConcurrentUpdateSolrServer,它是Solr速率性能的保證,使用它不要忘記在Solr里面配置autoCommit喲。

  1. /* 
  2.  
  3.  *版權:王安琪 
  4.  
  5.  *描述:監視HBase,一有數據postPut就向Solr發送,本類要作為觸發器添加到HBase 
  6.  
  7.  *修改時間:2014-05-27 
  8.  
  9.  *修改內容:新增 
  10.  
  11.  */ 
  12.  
  13. package solrHbase.test; 
  14.  
  15.   
  16.  
  17. import java.io.UnsupportedEncodingException; 
  18.  
  19.   
  20.  
  21. import ***; 
  22.  
  23.   
  24.  
  25. public class SorlIndexCoprocessorObserver extends BaseRegionObserver { 
  26.  
  27.   
  28.  
  29.     private static final Logger LOG = LoggerFactory 
  30.  
  31.             .getLogger(SorlIndexCoprocessorObserver.class); 
  32.  
  33.     private static final String solrUrl = "http://192.1.11.108:80/solr/core1"
  34.  
  35.     private static final SolrServer solrServer = new ConcurrentUpdateSolrServer( 
  36.  
  37.             solrUrl, 10000, 20); 
  38.  
  39.   
  40.  
  41.     /** 
  42.  
  43.      * 建立solr索引 
  44.  
  45.      *  
  46.  
  47.      * @throws UnsupportedEncodingException 
  48.  
  49.      */ 
  50.  
  51.     @Override 
  52.  
  53.     public void postPut(final ObserverContext<RegionCoprocessorEnvironment> e, 
  54.  
  55.             final Put put, final WALEdit edit, final boolean writeToWAL) 
  56.  
  57.             throws UnsupportedEncodingException { 
  58.  
  59.         inputSolr(put); 
  60.  
  61.     } 
  62.  
  63.   
  64.  
  65.     public void inputSolr(Put put) { 
  66.  
  67.         try { 
  68.  
  69.             solrServer.add(TestSolrMain.getInputDoc(put)); 
  70.  
  71.         } catch (Exception ex) { 
  72.  
  73.             LOG.error(ex.getMessage()); 
  74.  
  75.         } 
  76.  
  77.     } 
  78.  

注意:getInputDoc是這個HBase協處理器Coprocessor的精髓所在,它可以把HBase內的Put里的內容轉化成Solr需要的值。其中String fieldName = key.substring(key.indexOf(columnFamily) + 3, key.indexOf("我在這")).trim();這里有一個亂碼字符,在這里看不到,請大家注意一下。

  1. public static SolrInputDocument getInputDoc(Put put) { 
  2.  
  3.         SolrInputDocument doc = new SolrInputDocument(); 
  4.  
  5.         doc.addField("test_ID", Bytes.toString(put.getRow())); 
  6.  
  7.         for (KeyValue c : put.getFamilyMap().get(Bytes.toBytes(columnFamily))) { 
  8.  
  9.             String key = Bytes.toString(c.getKey()); 
  10.  
  11.             String value = Bytes.toString(c.getValue()); 
  12.  
  13.             if (value.isEmpty()) { 
  14.  
  15.                 continue; 
  16.  
  17.             } 
  18.  
  19.             String fieldName = key.substring(key.indexOf(columnFamily) + 3, 
  20.  
  21.                     key.indexOf("")).trim(); 
  22.  
  23.             doc.addField(fieldName, value); 
  24.  
  25.         } 
  26.  
  27.         return doc; 
  28.  
  29.     } 

二、編寫測試程序入口代碼main

​這段代碼向HBase請求建了一張表,并將模擬的數據,向HBase連續地提交數據內容,在HBase中不斷地插入數據,同時記錄時間,測試插入性能。

  1. /* 
  2.  
  3.  *版權:王安琪 
  4.  
  5.  *描述:測試HBaseInsert,HBase插入性能 
  6.  
  7.  *修改時間:2014-05-27 
  8.  
  9.  *修改內容:新增 
  10.  
  11.  */ 
  12.  
  13. package solrHbase.test; 
  14.  
  15.   
  16.  
  17. import hbaseInput.HbaseInsert; 
  18.  
  19.   
  20.  
  21. import ***; 
  22.  
  23.   
  24.  
  25. public class TestHBaseMain { 
  26.  
  27.   
  28.  
  29.     private static Configuration config; 
  30.  
  31.     private static String tableName = "angelHbase"
  32.  
  33.     private static HTable table = null
  34.  
  35.     private static final String columnFamily = "wanganqi"
  36.  
  37.   
  38.  
  39.     /** 
  40.  
  41.      * @param args 
  42.  
  43.      */ 
  44.  
  45.     public static void main(String[] args) { 
  46.  
  47.         config = HBaseConfiguration.create(); 
  48.  
  49.         config.set("hbase.zookeeper.quorum", "192.103.101.104"); 
  50.  
  51.         HbaseInsert.createTable(config, tableName, columnFamily); 
  52.  
  53.         try { 
  54.  
  55.             table = new HTable(config, Bytes.toBytes(tableName)); 
  56.  
  57.             for (int k = 0; k < 1; k++) { 
  58.  
  59.                 Thread t = new Thread() { 
  60.  
  61.                     public void run() { 
  62.  
  63.                         for (int i = 0; i < 100000; i++) { 
  64.  
  65.                             HbaseInsert.inputData(table, 
  66.  
  67.                                     PutCreater.createPuts(1000, columnFamily)); 
  68.  
  69.                             Calendar c = Calendar.getInstance(); 
  70.  
  71.                             String dateTime = c.get(Calendar.YEAR) + "-" 
  72.  
  73.                                     + c.get(Calendar.MONTH) + "-" 
  74.  
  75.                                     + c.get(Calendar.DATE) + "T" 
  76.  
  77.                                     + c.get(Calendar.HOUR) + ":" 
  78.  
  79.                                     + c.get(Calendar.MINUTE) + ":" 
  80.  
  81.                                     + c.get(Calendar.SECOND) + ":" 
  82.  
  83.                                     + c.get(Calendar.MILLISECOND) + "Z 寫入: " 
  84.  
  85.                                     + i * 1000; 
  86.  
  87.                             System.out.println(dateTime); 
  88.  
  89.                         } 
  90.  
  91.                     } 
  92.  
  93.                 }; 
  94.  
  95.                 t.start(); 
  96.  
  97.             } 
  98.  
  99.         } catch (IOException e1) { 
  100.  
  101.             e1.printStackTrace(); 
  102.  
  103.         } 
  104.  
  105.     } 
  106.  
  107.   
  108.  

​下面的是與HBase相關的操作,把它封裝到一個類中,這里就只有建表與插入數據的相關代碼。

  1. /* 
  2.  
  3.  *版權:王安琪 
  4.  
  5.  *描述:與HBase相關操作,建表與插入數據 
  6.  
  7.  *修改時間:2014-05-27 
  8.  
  9.  *修改內容:新增 
  10.  
  11.  */ 
  12.  
  13. package hbaseInput; 
  14.  
  15. import ***; 
  16.  
  17. import org.apache.hadoop.hbase.client.Put; 
  18.  
  19.   
  20.  
  21. public class HbaseInsert { 
  22.  
  23.   
  24.  
  25.     public static void createTable(Configuration config, String tableName, 
  26.  
  27.             String columnFamily) { 
  28.  
  29.         HBaseAdmin hBaseAdmin; 
  30.  
  31.         try { 
  32.  
  33.             hBaseAdmin = new HBaseAdmin(config); 
  34.  
  35.             if (hBaseAdmin.tableExists(tableName)) { 
  36.  
  37.                 return; 
  38.  
  39.             } 
  40.  
  41.             HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); 
  42.  
  43.             tableDescriptor.addFamily(new HColumnDescriptor(columnFamily)); 
  44.  
  45.             hBaseAdmin.createTable(tableDescriptor); 
  46.  
  47.             hBaseAdmin.close(); 
  48.  
  49.         } catch (MasterNotRunningException e) { 
  50.  
  51.             e.printStackTrace(); 
  52.  
  53.         } catch (ZooKeeperConnectionException e) { 
  54.  
  55.             e.printStackTrace(); 
  56.  
  57.         } catch (IOException e) { 
  58.  
  59.             e.printStackTrace(); 
  60.  
  61.         } 
  62.  
  63.     } 
  64.  
  65.   
  66.  
  67.     public static void inputData(HTable table, ArrayList<Put> puts) { 
  68.  
  69.         try { 
  70.  
  71.             table.put(puts); 
  72.  
  73.             table.flushCommits(); 
  74.  
  75.             puts.clear(); 
  76.  
  77.         } catch (IOException e) { 
  78.  
  79.             e.printStackTrace(); 
  80.  
  81.         } 
  82.  
  83.     } 
  84.  

三、編寫模擬數據Put

向HBase中寫入數據需要構造Put,下面是我構造模擬數據Put的方式,有字符串的生成,我是由mmseg提供的詞典words.dic中隨機讀取一些詞語連接起來,生成一句字符串的,下面的代碼沒有體現,不過很easy,你自己造你自己想要的數據就OK了。

  1. public static Put createPut(String columnFamily) { 
  2.  
  3.         String ss = getSentence(); 
  4.  
  5.         byte[] family = Bytes.toBytes(columnFamily); 
  6.  
  7.         byte[] rowKey = Bytes.toBytes("" + Math.abs(r.nextLong())); 
  8.  
  9.         Put put = new Put(rowKey); 
  10.  
  11.         put.add(family, Bytes.toBytes("DeviceID"), 
  12.  
  13.                 Bytes.toBytes("" + Math.abs(r.nextInt()))); 
  14.  
  15.         ****** 
  16.  
  17.         put.add(family, Bytes.toBytes("Company_mmsegsm"), Bytes.toBytes("ss")); 
  18.  
  19.   
  20.  
  21.         return put; 
  22.  
  23.     } 

當然在運行上面這個程序之前,需要先在Solr里面配置好你需要的列信息,HBase、Solr安裝與配置,它們的基礎使用方法將會在之后的文章中介紹。在這里,Solr的列配置就跟你使用createPut生成的Put搞成一樣的列名就行了,當然也可以使用動態列的形式。

四、直接對Solr性能測試

如果你不想對HBase與Solr的相結合進行測試,只想單獨對Solr的性能進行測試,這就更簡單了,完全可以利用上面的代碼段來測試,稍微組裝一下就可以了。

  1. private static void sendConcurrentUpdateSolrServer(final String url, 
  2.  
  3.             final int count) throws SolrServerException, IOException { 
  4.  
  5.         SolrServer solrServer = new ConcurrentUpdateSolrServer(url, 10000, 20); 
  6.         for (int i = 0; i < count; i++) {      solrServer.add(getInputDoc(PutCreater.createPut(columnFamily))); 
  7.         } 
  8.     } 

希望可以幫助到你規格嚴格-功夫到家。這次的文章代碼又偏多了點,但代碼是解釋思想的***的語言,我的提倡就是盡可能的減少代碼的注釋,盡力簡化你的代碼,使你的代碼足夠的清晰易懂,甚至于相似于偽代碼了,這也是《重構》這本書里所提倡的。

原文鏈接:http://www.cnblogs.com/wgp13x/p/3927979.html

責任編輯:彭凡 來源: 博客園
相關推薦

2017-03-08 10:29:06

HBase大數據存儲

2018-10-29 13:07:15

HBase存儲遷移

2015-06-12 14:20:35

2018-09-04 12:03:31

HBase大數據存儲

2017-12-20 15:10:09

HBaseHadoop數據

2014-04-15 09:30:08

Cassandra\H

2017-08-07 09:39:52

HBase大數據存儲

2017-11-01 14:29:38

2023-10-16 22:13:57

HBase開源數據庫

2023-07-26 08:51:08

大數據服務架構

2013-08-08 10:07:43

大數據存儲結構化數據

2020-02-02 16:59:31

HBase大數據二級索引

2023-08-07 09:00:00

2020-09-02 10:17:10

大數據數據分析數據

2022-08-14 14:52:45

數據存儲實踐

2016-12-04 16:46:51

大數據架構機器學習

2012-08-24 18:31:52

紅帽虛擬化

2017-09-04 17:34:17

HBASESolr查詢

2018-03-20 10:37:33

存儲大數據管理

2019-06-03 15:15:09

MySQL索引數據庫
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美一级片a | 欧美一区二区视频 | 久久久人成影片一区二区三区 | 呦呦在线视频 | 亚洲成人av | 亚洲国产精品久久 | 亚洲日本成人 | 欧美性受xxxx白人性爽 | 成人av网页 | 午夜伦4480yy私人影院 | 中文字幕视频在线 | 嫩草网 | 超碰免费观看 | 国产精品久久 | 日韩中文一区二区三区 | 国产日韩精品在线 | 亚洲精品久久 | 91精品国产日韩91久久久久久 | 91精品一区二区三区久久久久久 | 美女毛片免费看 | 亚洲不卡在线观看 | 9久9久9久女女女九九九一九 | 性一交一乱一伦视频免费观看 | 又爽又黄axxx片免费观看 | 亚洲精品视频三区 | 天天影视亚洲综合网 | 色av一区二区三区 | 91在线色视频 | 狠狠色综合网站久久久久久久 | 日韩中文字幕在线视频 | 日本精品一区二区三区在线观看视频 | 久在线| 中文字幕不卡 | 欧美综合国产精品久久丁香 | 欧美日韩亚洲国产 | 在线视频一区二区 | 黄网站免费在线 | 欧美国产日韩成人 | 在线免费视频一区 | 亚洲成人av在线播放 | 欧美成人视屏 |