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

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

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

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

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

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

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

​一旦有數(shù)據(jù)postPut,就立即對(duì)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.  *修改時(shí)間: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是這個(gè)HBase協(xié)處理器Coprocessor的精髓所在,它可以把HBase內(nèi)的Put里的內(nèi)容轉(zhuǎn)化成Solr需要的值。其中String fieldName = key.substring(key.indexOf(columnFamily) + 3, key.indexOf("我在這")).trim();這里有一個(gè)亂碼字符,在這里看不到,請(qǐng)大家注意一下。

  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.     } 

二、編寫測(cè)試程序入口代碼main

​這段代碼向HBase請(qǐng)求建了一張表,并將模擬的數(shù)據(jù),向HBase連續(xù)地提交數(shù)據(jù)內(nèi)容,在HBase中不斷地插入數(shù)據(jù),同時(shí)記錄時(shí)間,測(cè)試插入性能。

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

  1. /* 
  2.  
  3.  *版權(quán):王安琪 
  4.  
  5.  *描述:與HBase相關(guān)操作,建表與插入數(shù)據(jù) 
  6.  
  7.  *修改時(shí)間: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中隨機(jī)讀取一些詞語(yǔ)連接起來(lái),生成一句字符串的,下面的代碼沒(méi)有體現(xiàn),不過(guò)很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)然在運(yùn)行上面這個(gè)程序之前,需要先在Solr里面配置好你需要的列信息,HBase、Solr安裝與配置,它們的基礎(chǔ)使用方法將會(huì)在之后的文章中介紹。在這里,Solr的列配置就跟你使用createPut生成的Put搞成一樣的列名就行了,當(dāng)然也可以使用動(dòng)態(tài)列的形式。

四、直接對(duì)Solr性能測(cè)試

如果你不想對(duì)HBase與Solr的相結(jié)合進(jìn)行測(cè)試,只想單獨(dú)對(duì)Solr的性能進(jìn)行測(cè)試,這就更簡(jiǎn)單了,完全可以利用上面的代碼段來(lái)測(cè)試,稍微組裝一下就可以了。

  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)格-功夫到家。這次的文章代碼又偏多了點(diǎn),但代碼是解釋思想的***的語(yǔ)言,我的提倡就是盡可能的減少代碼的注釋,盡力簡(jiǎn)化你的代碼,使你的代碼足夠的清晰易懂,甚至于相似于偽代碼了,這也是《重構(gòu)》這本書里所提倡的。

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

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

2017-03-08 10:29:06

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

2018-10-29 13:07:15

HBase存儲(chǔ)遷移

2015-06-12 14:20:35

2018-09-04 12:03:31

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

2017-12-20 15:10:09

HBaseHadoop數(shù)據(jù)

2014-04-15 09:30:08

Cassandra\H

2017-08-07 09:39:52

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

2017-11-01 14:29:38

2023-10-16 22:13:57

HBase開(kāi)源數(shù)據(jù)庫(kù)

2023-07-26 08:51:08

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

2020-09-02 10:17:10

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

2013-08-08 10:07:43

大數(shù)據(jù)存儲(chǔ)結(jié)構(gòu)化數(shù)據(jù)

2023-08-07 09:00:00

2022-08-14 14:52:45

數(shù)據(jù)存儲(chǔ)實(shí)踐

2012-08-24 18:31:52

紅帽虛擬化

2020-02-02 16:59:31

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

2017-09-04 17:34:17

HBASESolr查詢

2016-12-04 16:46:51

大數(shù)據(jù)架構(gòu)機(jī)器學(xué)習(xí)

2019-05-05 09:03:06

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

2018-03-20 10:37:33

存儲(chǔ)大數(shù)據(jù)管理
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 在线国产小视频 | 久久com| 欧美一级做性受免费大片免费 | 精品久久久一区 | 夜夜爽99久久国产综合精品女不卡 | 五月激情婷婷网 | 精品在线一区二区 | 精品一二三 | 美女天天干天天操 | 最新黄色在线观看 | 欧美一区二区三区精品 | 天天操天天干天天爽 | 欧美精品一区二区三区在线四季 | 亚洲精品视频在线观看视频 | 成人在线观看免费 | 久久精品国产一区 | 久久久久久国产精品免费免费男同 | 国产成人精品在线播放 | 久久网一区二区 | 一级电影免费看 | www.久久| 久在线观看 | 日韩电影中文字幕在线观看 | 麻豆精品国产91久久久久久 | 人人爽日日躁夜夜躁尤物 | 国产视频不卡一区 | 在线观看中文字幕 | 欧美精品日韩 | 天天综合久久网 | m豆传媒在线链接观看 | 日本爱爱 | 成人国产一区二区三区精品麻豆 | 91 视频网站 | 国产激情一区二区三区 | 毛片在线免费播放 | 一区二区视频在线 | 精品国产91久久久久久 | 久久久久国产 | 国产a视频 | 精品国产乱码久久久久久闺蜜 | 韩日精品视频 |