cloudera search1.0.0环境搭建(2):利用flume-ng的MorphlineSolrSink实现近实时(NRT)搜索

要实现近实时搜索,就必须有一种机制来实时的处理数据然后生成到solr的索引中去,flume-ng刚好提供了这样一种机制,它可以实时收集数据,然后通过MorphlineSolrSink对数据进行ETL,最后写入到solr的索引中,这样就能在solr搜索引擎中近实时的查询到新进来的数据了。

搭建步骤:

1 我们这里只做演示效果,所以新建了一个文件file01来保存了两条数据,后面将通过flume-ng avro-client -H localhost -p 44444 -F file01将这两条数据提交给flume agent。

这两条数据如下:

{"id": "1234567890", "user_friends_count": 111, "user_location": "Palo Alto", "user_description": "desc1", "user_statuses_count": 11111, "user_followers_count": 111, "user_name": "name1", "user_screen_name": "fake_user1", "created_at": "1985-09-04T18:01:01Z", "text": "sample tweet one", "retweet_count": 0, "retweeted": false, "in_reply_to_user_id": 0, "source": "href=\"http:\/\/sample.com\"", "in_reply_to_status_id": 0, "media_url_https": null, "expanded_url": null}
{"id": "2345678901", "user_friends_count": 222, "user_location": "San Francisco", "user_description": "desc2", "user_statuses_count": 222222, "user_followers_count": 222, "user_name": "name2", "user_screen_name": "fake_user2", "created_at": "1985-09-04T19:14:34Z", "text": "sample tweet two", "retweet_count": 0, "retweeted": false, "in_reply_to_user_id": 0, "source": "href=\"http:\/\/sample.com\"", "in_reply_to_status_id": 0, "media_url_https": null, "expanded_url": null}

是两条JSON数据,后面我们会用morphlines来对json数据进行ETL抽取指定的几个字段。

2 在CM中flume的配置中配置Flume-NG Solr 接收器,如下图:

morphlines配置文件如下:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. # Specify server locations in a SOLR_LOCATOR variable; used later in   
  2. # variable substitutions:  
  3. SOLR_LOCATOR : {  
  4.   # Name of solr collection  
  5.   collection : collection1  
  6.     
  7.   # ZooKeeper ensemble  
  8.   zkHost : "master68:2181,slave69:2181,slave76:2181/solr"    
  9. }  
  10.   
  11. # Specify an array of one or more morphlines, each of which defines an ETL   
  12. # transformation chain. A morphline consists of one or more potentially   
  13. # nested commands. A morphline is a way to consume records such as Flume events,   
  14. # HDFS files or blocks, turn them into a stream of records, and pipe the stream   
  15. # of records through a set of easily configurable transformations on its way to   
  16. # Solr.  
  17. morphlines : [  
  18.   {  
  19.     # Name used to identify a morphline. For example, used if there are multiple   
  20.     # morphlines in a morphline config file.  
  21.     id : morphline1   
  22.       
  23.     # Import all morphline commands in these java packages and their subpackages.  
  24.     # Other commands that may be present on the classpath are not visible to this  
  25.     # morphline.  
  26.     importCommands : ["org.kitesdk.**", "org.apache.solr.**"]  
  27.       
  28.     commands : [                      
  29.       {  
  30.         readJson {}  
  31.       }  
  32.       {   
  33.         extractJsonPaths {  
  34.           flatten : false  
  35.           paths : {   
  36.             id : /id              
  37.             user_name : /user_screen_name  
  38.             created_at : /created_at  
  39.             text : /text        
  40.           }  
  41.         }  
  42.       }  
  43.         
  44.       # Consume the output record of the previous command and pipe another  
  45.       # record downstream.  
  46.       #  
  47.       # convert timestamp field to native Solr timestamp format  
  48.       # such as 2012-09-06T07:14:34Z to 2012-09-06T07:14:34.000Z  
  49.       {  
  50.         convertTimestamp {  
  51.           field : created_at  
  52.           inputFormats : ["yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd"]  
  53.           inputTimezone : America/Los_Angeles  
  54.           outputFormat : "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"                                   
  55.           outputTimezone : UTC  
  56.         }  
  57.       }  
  58.         
  59.       # Consume the output record of the previous command and pipe another  
  60.       # record downstream.  
  61.       #  
  62.       # This command deletes record fields that are unknown to Solr   
  63.       # schema.xml.  
  64.       #  
  65.       # Recall that Solr throws an exception on any attempt to load a document   
  66.       # that contains a field that is not specified in schema.xml.  
  67.       {  
  68.         sanitizeUnknownSolrFields {  
  69.           # Location from which to fetch Solr schema  
  70.           solrLocator : ${SOLR_LOCATOR}  
  71.         }  
  72.       }    
  73.               
  74.       # log the record at DEBUG level to SLF4J  
  75.       { logDebug { format : "output record: {}", args : ["@{}"] } }      
  76.         
  77.       # load the record into a Solr server or MapReduce Reducer  
  78.       {   
  79.         loadSolr {  
  80.           solrLocator : ${SOLR_LOCATOR}  
  81.         }  
  82.       }  
  83.     ]  
  84.   }  
  85. ]  

简单解释一下这个morphlines配置文件,首先执行了一个readJson命令,将读入的event的内容转换成了一个json对象,然后使用extractJsonPaths命令抽取json对象的具体字段值并重新赋值给另一个字段(例如user_name : /user_screen_name 是读取user_screen_name的值并赋值给user_name ),然后使用convertTimestamp对create_at字段进行格式化,最后执行sanitizeUnknownSolrFields命令舍弃solr的schema中没有配置的field字段,即通过ETL之后record最终只保留solr中已配置的字段。然后通过loadSolr指令将最终的record提交到solr。

3 接下来就是flume agent的配置:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. tier1.sources=source1  
  2. tier1.channels=channel1  
  3. tier1.sinks=sink1  
  4.   
  5. tier1.sources.source1.type = avro  
  6. tier1.sources.source1.bind = 0.0.0.0  
  7. tier1.sources.source1.port = 44444  
  8. tier1.sources.source1.channels=channel1  
  9.   
  10. tier1.channels.channel1.type=memory  
  11. tier1.channels.channel1.capacity=10000  
  12.   
  13. tier1.sinks.sink1.type = org.apache.flume.sink.solr.morphline.MorphlineSolrSink  
  14. tier1.sinks.sink1.channel = channel1  
  15. tier1.sinks.sink1.morphlineFile = morphlines.conf  
  16. tier1.sinks.sink1.morphlineId = morphline1  

这里一个注意点就是我们在CM中配置的Flume-NG Solr 接收器,所以morphlineFile直接写morphlines.conf就行了,否则需要写绝对路径,不然没法找到morphlines的配置文件。

4 上面三部准备好之后,启动agent,然后在shell控制台执行 flume-ng avro-client -H localhost -p 44444 -F file01命令,将我们第一步创建的数据文件提交给agent。

5执行完后,如果没报错的话,可以去solr中通过http://slave77:8983/solr/collection1/select?q=*:*查询一下这两条数据是不是已经创建到搜索引擎的索引库中


如果看到如下图示的结果,恭喜你,你已经成功完成了本篇文章的NRT架构的搭建。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值