標籤:即時 dir play keep topic rds 安裝 action 接收
背景:Kafka訊息匯流排的建成,使各個系統的資料得以在kafka節點中匯聚,接下來面臨的任務是最大化資料的價值,讓資料“慧”說話。
環境準備:
Kafka伺服器*3。
CDH 5.8.3伺服器*3,安裝Flume,Solr,Hue,HDFS,Zookeeper服務。
Flume提供了可擴充的即時資料傳輸通道,Morphline提供了輕量級的ETL功能,SolrCloud+Hue提供了高效能搜尋引擎和多樣的資料展現形式。
一.環境安裝(略)
二.修改CDH預設配置:
1.在Flume配置介面配置Flume依賴Solr。
2.在Solr配置介面配置Solr使用Zookeeper儲存設定檔,使用HDFS儲存索引檔案。
3.在Hue配置介面配置Hue依賴Solr
4.配置Hue介面可以被外網訪問。
三.按情境配置各CDH服務及開發代碼。
Kafka Topic: eventCount
Topic資料格式:
{ "timestamp": "1481077173000", "accountName": "旺小寶", "tagNames": [ "incoming" ], "account": "WXB", "eventType": "phone", "eventTags": [ { "value": 1, "name": "incoming" } ]}
1.Solr建立對應Collection。
1)登入任意CDH節點。產生collection設定檔骨架。
$ solrctl instancedir --generate $HOME/solr_configs
2)找到檔案夾中的schema.xml檔案,修改collection的schema。
第一步:修改filed。schema.xml中預定義了很多filed,除了name=id,_root_,_version_不能去掉之外,其他的全部可以去掉。field對應的是json中需要被索引的欄位。
(Notice:json中的timestamp對應的是下面的eventTime,而下面的timestamp是flume接受kafka資料的時間。這是通過Morphline配置實現的轉換)
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <!-- points to the root document of a block of nested documents. Required for nested document support, may be removed otherwise --> <field name="_root_" type="string" indexed="true" stored="false"/> <field name="account" type="string" indexed="true" stored="true"/> <field name="accountName" type="string" indexed="true" stored="true"/> <field name="subaccount" type="string" indexed="true" stored="true"/> <field name="subaccountName" type="string" indexed="true" stored="true"/> <field name="eventTime" type="tlong" indexed="false" stored="true"/> <field name="eventType" type="string" indexed="true" stored="true"/> <field name="eventTags" type="string" indexed="true" stored="true" multiValued="true"/> <field name="_attachment_body" type="string" indexed="false" stored="true"/> <field name="timestamp" type="tlong" indexed="false" stored="true"/> <field name="_version_" type="long" indexed="true" stored="true"/>
第二步:去掉所有copy field。
第三步:添加動態欄位dynamicFiled。
<dynamicField name="tws_*" type="text_ws" indexed="true" stored="true" multiValued="true"/>
3) 上傳配置,建立collection
$ solrctl instancedir --create event_count_records solr_configs$ solrctl collection --create event_count_records -s 3 -c event_count_records
2.Flume配置
建立一個新的角色群組kafka2solr,修改代理名稱為kafka2solr,並為該角色群組分配伺服器。
# 配置 source channel sink 的名字kafka2solr.sources = source_from_kafkakafka2solr.channels = mem_channelkafka2solr.sinks = solrSink# 配置Source類別為kafkakafka2solr.sources.source_from_kafka.type = org.apache.flume.source.kafka.KafkaSourcekafka2solr.sources.source_from_kafka.channels = mem_channelkafka2solr.sources.source_from_kafka.batchSize = 100kafka2solr.sources.source_from_kafka.kafka.bootstrap.servers= kafkanode0:9092,kafkanode1:9092,kafkanode2:9092kafka2solr.sources.source_from_kafka.kafka.topics = eventCountkafka2solr.sources.source_from_kafka.kafka.consumer.group.id = flume_solr_callerkafka2solr.sources.source_from_kafka.kafka.consumer.auto.offset.reset=latest#配置channel type為memory,通常生產環境中設定為file或者直接用kafka作為channelkafka2solr.channels.mem_channel.type = memorykafka2solr.channels.mem_channel.keep-alive = 60 # Other config values specific to each type of channel(sink or source) # can be defined as well # In this case, it specifies the capacity of the memory channel kafka2solr.channels.mem_channel.capacity = 10000 kafka2solr.channels.mem_channel.transactionCapacity = 3000 # 配置sink到solr,並使用morphline轉換資料kafka2solr.sinks.solrSink.type = org.apache.flume.sink.solr.morphline.MorphlineSolrSinkkafka2solr.sinks.solrSink.channel = mem_channelkafka2solr.sinks.solrSink.morphlineFile = morphlines.confkafka2solr.sinks.solrSink.morphlineId=morphline1kafka2solr.sinks.solrSink.isIgnoringRecoverableExceptions=true
3.Flume-NG的Solr接收器配置
SOLR_LOCATOR : { # Name of solr collection collection : event_count_records # ZooKeeper ensemble
#CDH的專有寫法,開源版本不支援。 zkHost : "$ZK_HOST" }morphlines : [ { id : morphline1 importCommands : ["org.kitesdk.**", "org.apache.solr.**"] commands : [ { #Flume傳過來的kafka的json資料是用二進位流的形式,需要先讀取json readJson{}}{ #讀出來的json欄位必須轉換成filed才能被solr索引到extractJsonPaths { flatten:true paths:{account:/accountaccountName:/accountNamesubaccount:/subaccountsubaccountName:/subaccountNameeventTime:/timestampeventType:/eventTypeeventTags:"/eventTags[]/name"#按分鐘存timestampeventTimeInMinute_tdt:/timestamp#按小時存timestampeventTimeInHour_tdt:/timestamp#按天存timestampeventTimeInDay_tdt:/timestamp#_tdt尾碼會被動態識別為日期類型的索引欄位#按不同時間間隔存索引以增加查詢效能} }}#轉換long型時間為Date格式{convertTimestamp { field : eventTimeInMinute_tdt inputFormats : ["unixTimeInMillis"] inputTimezone : UTC outputFormat : "yyyy-MM-dd‘T‘HH:mm:ss.SSS‘Z/MINUTE‘" outputTimezone : Asia/Shanghai}}{convertTimestamp { field : eventTimeInHour_tdt inputFormats : ["unixTimeInMillis"] inputTimezone : UTC outputFormat : "yyyy-MM-dd‘T‘HH:mm:ss.SSS‘Z/HOUR‘" outputTimezone : Asia/Shanghai}}{convertTimestamp { field : eventTimeInDay_tdt inputFormats : ["unixTimeInMillis"] inputTimezone : UTC outputFormat : "yyyy-MM-dd‘T‘HH:mm:ss.SSS‘Z/DAY‘" outputTimezone : Asia/Shanghai}}#kafka中的json資料傳到flume中時會被放入_attachment_body欄位,readJson後會變成JsonNode對象,需要toString之後才能儲存{toString { field : _attachment_body }}#為每一條記錄產生一個UUID{generateUUID { field : id}}#對未定義的Solr欄位加tws首碼,根據schema.xml中定義的tws_*為text_ws類型,會動態未未定義的欄位建索引。 { sanitizeUnknownSolrFields { # Location from which to fetch Solr schema solrLocator : ${SOLR_LOCATOR} renameToPrefix:"tws_" } } #將資料匯入到solr中 {loadSolr {solrLocator : ${SOLR_LOCATOR}}} ] }]
重啟被影響的Flume節點,資料開始匯入solr。
3.通過Hue查詢Solr中的資料。
見Solr+Hue實戰。
Kafka+Flume+Morphline+Solr+Hue資料群組合索引