自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

大數(shù)據(jù)架構(gòu)-使用HBase和Solr配置存儲與索引

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

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

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

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

一、編寫HBase協(xié)處理器Coprocessor

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

  1. /* 
  2.  
  3.  *版權(quán):王安琪 
  4.  
  5.  *描述:監(jiān)視HBase,一有數(shù)據(jù)postPut就向Solr發(fā)送,本類要作為觸發(fā)器添加到HBase 
  6.  
  7.  *修改時間:2014-05-27 
  8.  
  9.  *修改內(nèi)容:新增 
  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協(xié)處理器Coprocessor的精髓所在,它可以把HBase內(nèi)的Put里的內(nèi)容轉(zhuǎn)化成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請求建了一張表,并將模擬的數(shù)據(jù),向HBase連續(xù)地提交數(shù)據(jù)內(nèi)容,在HBase中不斷地插入數(shù)據(jù),同時記錄時間,測試插入性能。

  1. /* 
  2.  
  3.  *版權(quán):王安琪 
  4.  
  5.  *描述:測試HBaseInsert,HBase插入性能 
  6.  
  7.  *修改時間:2014-05-27 
  8.  
  9.  *修改內(nèi)容:新增 
  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相關(guān)的操作,把它封裝到一個類中,這里就只有建表與插入數(shù)據(jù)的相關(guān)代碼。

  1. /* 
  2.  
  3.  *版權(quán):王安琪 
  4.  
  5.  *描述:與HBase相關(guān)操作,建表與插入數(shù)據(jù) 
  6.  
  7.  *修改時間:2014-05-27 
  8.  
  9.  *修改內(nèi)容:新增 
  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.  

三、編寫模擬數(shù)據(jù)Put

向HBase中寫入數(shù)據(jù)需要構(gòu)造Put,下面是我構(gòu)造模擬數(shù)據(jù)Put的方式,有字符串的生成,我是由mmseg提供的詞典words.dic中隨機讀取一些詞語連接起來,生成一句字符串的,下面的代碼沒有體現(xiàn),不過很easy,你自己造你自己想要的數(shù)據(jù)就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.     } 

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

四、直接對Solr性能測試

如果你不想對HBase與Solr的相結(jié)合進行測試,只想單獨對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.     } 

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

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

責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2017-03-08 10:29:06

HBase大數(shù)據(jù)存儲

2018-10-29 13:07:15

HBase存儲遷移

2018-09-04 12:03:31

HBase大數(shù)據(jù)存儲

2015-06-12 14:20:35

2014-04-15 09:30:08

Cassandra\H

2017-12-20 15:10:09

HBaseHadoop數(shù)據(jù)

2017-08-07 09:39:52

HBase大數(shù)據(jù)存儲

2023-10-16 22:13:57

HBase開源數(shù)據(jù)庫

2017-11-01 14:29:38

2017-09-04 17:34:17

HBASESolr查詢

2023-08-07 09:00:00

2020-02-02 16:59:31

HBase大數(shù)據(jù)二級索引

2023-07-26 08:51:08

大數(shù)據(jù)服務(wù)架構(gòu)

2020-09-02 10:17:10

大數(shù)據(jù)數(shù)據(jù)分析數(shù)據(jù)

2019-05-05 09:03:06

HBase大數(shù)據(jù)存儲數(shù)據(jù)存儲

2018-01-31 18:10:15

數(shù)據(jù)庫HBase

2019-06-03 15:15:09

MySQL索引數(shù)據(jù)庫

2012-08-24 18:31:52

紅帽虛擬化

2022-08-14 14:52:45

數(shù)據(jù)存儲實踐

2013-08-08 10:07:43

大數(shù)據(jù)存儲結(jié)構(gòu)化數(shù)據(jù)
點贊
收藏

51CTO技術(shù)棧公眾號