Flume (二) Setup

Setup

Setting up an agent

Flume代理配置存儲在本地配置文件中。 這是一個遵循Java屬性文件格式的文本文件。 可以在同一配置文件中指定一個或多個代理的配置。 配置文件包括代理中每個Source,Sink和Channel的屬性以及它們如何連接在一起以形成數據流。

Configuring individual components (配置單個組件)

Each component (source, sink or channel) in the flow has a name, type, and set of properties that are specific to the type and instantiation. For example, an Avro source needs a hostname (or IP address) and a port number to receive data from. A memory channel can have max queue size (“capacity”), and an HDFS sink needs to know the file system URI, path to create files, frequency of file rotation (“hdfs.rollInterval”) etc. All such attributes of a component needs to be set in the properties file of the hosting Flume agent.

流中的每個組件(source, sink or channel)都一個name, type 和 特定於類型和實例化的屬性集。 例如,Avro source 需要主機名(hostname或IP地址)和端口號來接收數據。 內存通道是一個具有最大值得隊列(“capacity”),HDFS sink 需要知道文件系統URI,創建文件的路徑,文件輪換頻率(“hdfs.rollInterval”)等。組件的所有此類屬性需要在託管Flume代理的屬性文件中設置。

Wiring the pieces together

The agent needs to know what individual components to load and how they are connected in order to constitute the flow. This is done by listing the names of each of the sources, sinks and channels in the agent, and then specifying the connecting channel for each sink and source. For example, an agent flows events from an Avro source called avroWeb to HDFS sink hdfs-cluster1 via a file channel called file-channel. The configuration file will contain names of these components and file-channel as a shared channel for both avroWeb source and hdfs-cluster1 sink.

代理需要知道要加載哪些組件以及它們如何連接以構成流。 這是通過列出代理中每個源,接收器和通道的名稱,然後爲每個接收器和源指定連接通道來完成的。 例如,代理通過名爲file-channel的文件通道將事件從名爲avroWeb的Avro源流向HDFS sink hdfs-cluster1。 配置文件將包含這些組件的名稱和文件通道,作爲avroWeb源和hdfs-cluster1接收器的共享通道。

Starting an agent

使用名爲flume-ng的shell腳本啓動代理程序,該腳本位於Flume發行版的bin目錄中。 您需要在命令行上指定代理名稱,配置目錄和配置文件:

bin/flume-ng agent -n $agent_name -c conf -f conf/flume-conf.properties.template

現在,代理將開始運行配置在屬性文件中的source和sinks

A simple example

在這裏,我們給出一個示例配置文件,描述單節點Flume部署。 此配置允許用戶生成事件,然後將其打印到控制檯。

# example.conf: A single-node Flume configuration

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# Describe the sink
a1.sinks.k1.type = logger

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

這個配置定義了一個名爲a1的agent。該agent有一個監聽本地44444端口數據的source,一個將數據緩存到內存中的channel,和一個將事件數據打印到控制檯的sink。配置文件中命名各種組件,然後描述組件的types 和配置參數。一個給定的配置文件鐘可能會定義多個命名代理(named agent), 當一個給定的Flume進程啓動時,會傳遞一個標誌,告訴它要顯示哪個命名代理。

基於此配置文件,我們可以按如下方式啓動Flume:

bin/flume-ng agent --conf conf --conf-file example.conf --name a1 -Dflume.root.logger=INFO,console

請注意,在完整部署中,我們通常會包含一個選項: --conf = <conf-dir><conf-dir>目錄將包含一個shell腳本flume-env.sh以及一個log4j屬性文件。 在這個例子中,我們傳遞一個Java選項來強制Flume將日誌打印到控制檯,我們沒有自定義環境腳本。

從一個單獨的終端,我們可以telnet端口44444並向Flume發送一個事件:

$ telnet localhost 44444
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
Hello world! <ENTER>
OK

原始的Flume終端將在日誌中輸出事件。

12/06/19 15:32:19 INFO source.NetcatSource: Source starting
12/06/19 15:32:19 INFO source.NetcatSource: Created serverSocket:sun.nio.ch.ServerSocketChannelImpl[/127.0.0.1:44444]
12/06/19 15:32:34 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 77 6F 72 6C 64 21 0D          Hello world!. }

恭喜 - 您已成功配置並部署了Flume代理! 後續部分將更詳細地介紹代理配置。

Using environment variables in configuration files

Flume能夠替換配置中的環境變量。 例如:

a1.sources = r1
a1.sources.r1.type = netcat
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = ${NC_PORT}
a1.sources.r1.channels = c1

注意:它目前僅適用於值,不適用於鍵。 (即僅在配置行的=標記的“右側”。)

This can be enabled via Java system properties on agent invocation by setting propertiesImplementation = org.apache.flume.node.EnvVarResolverProperties.
可以通過設置propertiesImplementation = org.apache.flume.node.EnvVarResolverProperties,通過代理程序調用上的Java系統屬性啓用此功能。

For example::

NC_PORT=44444 bin/flume-ng agent –conf conf –conf-file example.conf –name a1 -Dflume.root.logger=INFO,console -DpropertiesImplementation=org.apache.flume.node.EnvVarResolverProperties

請注意,上面只是一個示例,可以通過其他方式配置環境變量,包括在conf/flume-env.sh中設置。

Logging raw data(記錄原始數據)

在許多生產環境中記錄流經攝取管道的原始數據流不是所希望的行爲,因爲這可能導致泄漏敏感數據或安全相關配置(例如密鑰)泄漏到Flume日誌文件。 默認情況下,Flume不會記錄此類信息。 另一方面,如果數據管道被破壞,Flume將嘗試提供調試問題的線索。

調試事件管道問題的一種方法是設置連接到Logger Sink的附加內存通道,它將所有事件數據輸出到Flume日誌。 但是,在某些情況下,這種方法是不夠的。

爲了能夠記錄事件和配置相關的數據,除了log4j屬性之外,還必須設置一些Java系統屬性。

要啓用與配置相關的日誌記錄,請設置Java系統屬性-Dorg.apache.flume.log.printconfig = true。 這可以在命令行上傳遞,也可以在flume-env.sh中的JAVA_OPTS變量中設置。

要啓用數據記錄,請按照上述相同方式設置Java系統屬性-Dorg.apache.flume.log.rawdata = true。 對於大多數組件,還必須將log4j日誌記錄級別設置爲DEBUGTRACE,以使特定於事件的日誌記錄顯示在Flume日誌中。

下面是啓用配置日誌記錄和原始數據日誌記錄的示例,同時還將Log4j日誌級別設置爲DEBUG以用於控制檯輸出:

bin/flume-ng agent --conf conf --conf-file example.conf --name a1 -Dflume.root.logger=DEBUG,console -Dorg.apache.flume.log.printconfig=true -Dorg.apache.flume.log.rawdata=true

Zookeeper based Configuration

Flume通過Zookeeper支持代理配置。 這是一個實驗性功能。 配置文件需要上傳到Zookeeper中,在可配置前綴下的。 配置文件存儲在Zookeeper節點數據中。 以下是agents a1和a2的Zookeeper節點樹的外觀

- /flume
 |- /a1 [Agent config file]
 |- /a2 [Agent config file]

上傳配置文件後,使用以下選項啓動agent

bin/flume-ng agent –conf conf -z zkhost:2181,zkhost1:2181 -p /flume –name a1 -Dflume.root.logger=INFO,console
參數名稱 默認值 描述
z - Zookeeper連接字符串。 以逗號分隔 hostname:port
p /flume Zookeeper中用於存儲Agent配置的基本路徑

Installing third-party plugins

Flume擁有完全基於插件的架構。 雖然Flume附帶了許多開箱即用的sources, channels, sinks, serializers等,但許多實現都與Flume分開提供。

雖然通過將自己的jar包添加到flume-env.sh文件中的FLUME_CLASSPATH變量中,始終可以包含自定義Flume組件,但Flume現在支持一個名爲plugins.d的特殊目錄,該目錄會自動獲取以特定格式打包的插件。 這樣可以更輕鬆地管理插件打包問題,以及更簡單的調試和幾類問題的故障排除,尤其是庫依賴性衝突。

The plugins.d directory

plugins.d目錄位於$FLUME_HOME/plugins.d。 在啓動時,flume-ng啓動腳本在plugins.d目錄中查找符合以下格式的插件,並在啓動java時將它們包含在正確的路徑中。

  • lib - 插件jar
  • libext - 插件依賴的jar文件
  • native - 任何必需的本機庫,例如.so文件

plugins.d目錄中的兩個插件示例:

plugins.d/
plugins.d/custom-source-1/
plugins.d/custom-source-1/lib/my-source.jar
plugins.d/custom-source-1/libext/spring-core-2.5.6.jar
plugins.d/custom-source-2/
plugins.d/custom-source-2/lib/custom.jar
plugins.d/custom-source-2/native/gettext.so

Data ingestion

Flume支持許多從外部源攝取數據的機制。

RPC

Flume發行版中包含的Avro客戶端可以使用avro RPC機制將給定文件發送到Flume Avro源:

bin/flume-ng avro-client -H localhost -p 41414 -F /usr/logs/log.10

上面的命令會將/usr/logs/log.10的內容發送到監聽該端口的Flume源。

Executing commands

There’s an exec source that executes a given command and consumes the output. A single ‘line’ of output ie. text followed by carriage return (‘\r’) or line feed (‘\n’) or both together.

Network streams

Flume支持以下機制從常用日誌流類型中讀取數據,例如:

  • Avro
  • Thrift
  • Syslog
  • Netcat

Setting multi-agent flow

這裏寫圖片描述

爲了跨多個agent或hops,前一個agent的sink和當前hop 的source需要是avro類型,sink指向source的主機名(或IP地址)和端口。

Consolidation

日誌收集中非常常見的場景是大量日誌生成客戶端將數據發送到連接到存儲子系統的少數消費者代理。 例如,從數百個Web服務器收集的日誌發送給寫入HDFS集羣的十幾個代理。
這裏寫圖片描述

這可以在Flume中實現, 通過使用avro sink配置多個第一層agents,所有這些都指向單個代理的avro源(同樣,您可以在這種情況下使用thrift sources/sinks/clients)。 第二層代理上的此源將接收的事件合併到單個信道中,該信道由接收器消耗到其最終目的地。

Multiplexing the flow

Flume支持將事件流多路複用到一個或多個目的地。 這是通過定義可以複製或選擇性地將事件路由到一個或多個通道的流複用器來實現的。
這裏寫圖片描述

上面的例子顯示了來自代理“foo”的source 將流扇出到三個不同的通道。 這個扇出可以是複製(replicating)或多路複用(multiplexing)。 在複製流的情況下,每個事件被髮送到所有三個通道。 對於多路複用情況,當事件的屬性與預配置的值匹配時,事件將傳遞到可用通道的子集。 例如,如果一個名爲“txnType”的事件屬性設置爲“customer”,那麼它應該轉到channel1和channel3,如果它是“vendor”,那麼它應該轉到channel2,否則轉到channel3。 可以在代理的配置文件中設置映射。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章