Flume安装及简单使用

来源:互联网 发布:折八百淘宝商城马甲 编辑:程序博客网 时间:2024/06/05 11:00

1下载解压到指定目录
22)修改 flume-env.sh 配置文件,主要是JAVA_HOME变量设置

1.root@m1:/home/hadoop/flume-1.5.0-bin# cp conf/flume-env.sh.template conf/flume-env.sh
2.root@m1:/home/hadoop/flume-1.5.0-bin# vi conf/flume-env.sh
3.# Licensed to the Apache Software Foundation (ASF) under one
4.# or more contributor license agreements. See the NOTICE file
5.# distributed with this work for additional information
6.# regarding copyright ownership. The ASF licenses this file
7.# to you under the Apache License, Version 2.0 (the
8.# “License”); you may not use this file except in compliance
9.# with the License. You may obtain a copy of the License at
10.#
11.# http://www.apache.org/licenses/LICENSE-2.0
12.#
13.# Unless required by applicable law or agreed to in writing, software
14.# distributed under the License is distributed on an “AS IS” BASIS,
15.# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16.# See the License for the specific language governing permissions and
17.# limitations under the License.
18.
19.# If this file is placed at FLUME_CONF_DIR/flume-env.sh, it will be sourced
20.# during Flume startup.
21.
22.# Enviroment variables can be set here.
23.
24.JAVA_HOME=/usr/lib/jvm/java-7-oracle
25.
26.# Give Flume more memory and pre-allocate, enable remote monitoring via JMX
27.#JAVA_OPTS=”-Xms100m -Xmx200m -Dcom.sun.management.jmxremote”
28.
29.# Note that the Flume conf directory is always included in the classpath.
30.#FLUME_CLASSPATH=””
复制代码

          3)验证是否安装成功

1.root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng version
2.Flume 1.5.0
3.Source code repository: https://git-wip-us.apache.org/repos/asf/flume.git
4.Revision: 8633220df808c4cd0c13d1cf0320454a94f1ea97
5.Compiled by hshreedharan on Wed May 7 14:49:18 PDT 2014
6.From source with checksum a01fe726e4380ba0c9f7a7d222db961f
7.root@m1:/home/hadoop#

复制代码
    出现上面的信息,表示安装成功了

  五、flume的案例
    1)案例1:Avro
    Avro可以发送一个给定的文件给Flume,Avro 源使用AVRO RPC机制。
      a)创建agent配置文件

  1. root@m1:/home/hadoop#vi /home/hadoop/flume-1.5.0-bin/conf/avro.conf

    1. a1.sources = r1
  2. a1.sinks = k1
  3. a1.channels = c1

    1. # Describe/configure the source
  4. a1.sources.r1.type = avro
  5. a1.sources.r1.channels = c1
  6. a1.sources.r1.bind = 0.0.0.0
  7. a1.sources.r1.port = 4141

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

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

    1. # Bind the source and sink to the channel
  12. a1.sources.r1.channels = c1
  13. a1.sinks.k1.channel = c1
    复制代码

          b)启动flume agent a1

  14. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/avro.conf -n a1 -Dflume.root.logger=INFO,console

  15. 复制代码

          c)创建指定文件

  16. root@m1:/home/hadoop# echo “hello world” > /home/hadoop/flume-1.5.0-bin/log.00
    复制代码

          d)使用avro-client发送文件

  17. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng avro-client -c . -H m1 -p 4141 -F /home/hadoop/flume-1.5.0-bin/log.00
    复制代码

          d)使用avro-client发送文件

  18. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng avro-client -c . -H m1 -p 4141 -F /
    复制代码

          f)在m1的控制台,可以看到以下信息,注意最后一行:

  19. root@m1:/home/hadoop/flume-1.5.0-bin/conf# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/avro.conf -n a1 -Dflume.root.logger=INFO,console

  20. Info: Sourcing environment configuration script /home/hadoop/flume-1.5.0-bin/conf/flume-env.sh
  21. Info: Including Hadoop libraries found via (/home/hadoop/hadoop-2.2.0/bin/hadoop) for HDFS access
  22. Info: Excluding /home/hadoop/hadoop-2.2.0/share/hadoop/common/lib/slf4j-api-1.7.5.jar from classpath
  23. Info: Excluding /home/hadoop/hadoop-2.2.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar from classpath
  24. 2014-08-10 10:43:25,112 (New I/O worker #1) [INFO - org.apache.avro.ipc.NettyServer$NettyServerAvroHandler.handleUpstream(NettyServer.java:171)] [id: 0x92464c4f, /192.168.1.50:59850 :> /192.168.1.50:4141] UNBOUND
  25. 2014-08-10 10:43:25,112 (New I/O worker #1) [INFO - org.apache.avro.ipc.NettyServer$NettyServerAvroHandler.handleUpstream(NettyServer.java:171)] [id: 0x92464c4f, /192.168.1.50:59850 :> /192.168.1.50:4141] CLOSED
  26. 2014-08-10 10:43:25,112 (New I/O worker #1) [INFO - org.apache.avro.ipc.NettyServer$NettyServerAvroHandler.channelClosed(NettyServer.java:209)] Connection to /192.168.1.50:59850 disconnected.
  27. 2014-08-10 10:43:26,718 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 68 65 6C 6C 6F 20 77 6F 72 6C 64 hello world }
  28. 复制代码

        2)案例2:Spool
        Spool监测配置的目录下新增的文件,并将文件中的数据读取出来。需要注意两点:
        1) 拷贝到spool目录下的文件不可以再打开编辑。
        2) spool目录下不可包含相应的子目录

          a)创建agent配置文件

  29. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/spool.conf


    1. a1.sources = r1
  30. a1.sinks = k1
  31. a1.channels = c1

    1. # Describe/configure the source
  32. a1.sources.r1.type = spooldir
  33. a1.sources.r1.channels = c1
  34. a1.sources.r1.spoolDir = /home/hadoop/flume-1.5.0-bin/logs
  35. a1.sources.r1.fileHeader = true

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

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

    1. # Bind the source and sink to the channel
  40. a1.sources.r1.channels = c1
  41. a1.sinks.k1.channel = c1
  42. 复制代码

          b)启动flume agent a1

  43. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/spool.conf -n a1 -Dflume.root.logger=INFO,console

  44. 复制代码

          c)追加文件到/home/hadoop/flume-1.5.0-bin/logs目录

  45. root@m1:/home/hadoop# echo “spool test1” > /home/hadoop/flume-1.5.0-bin/logs/spool_text.log

  46. 复制代码

         d)在m1的控制台,可以看到以下相关信息:

  47. 14/08/10 11:37:13 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.

  48. 14/08/10 11:37:13 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.
  49. 14/08/10 11:37:14 INFO avro.ReliableSpoolingFileEventReader: Preparing to move file /home/hadoop/flume-1.5.0-bin/logs/spool_text.log to /home/hadoop/flume-1.5.0-bin/logs/spool_text.log.COMPLETED
  50. 14/08/10 11:37:14 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.
  51. 14/08/10 11:37:14 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.
  52. 14/08/10 11:37:14 INFO sink.LoggerSink: Event: { headers:{file=/home/hadoop/flume-1.5.0-bin/logs/spool_text.log} body: 73 70 6F 6F 6C 20 74 65 73 74 31 spool test1 }
  53. 14/08/10 11:37:15 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.
  54. 14/08/10 11:37:15 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.
  55. 14/08/10 11:37:16 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.
  56. 14/08/10 11:37:16 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.
  57. 14/08/10 11:37:17 INFO source.SpoolDirectorySource: Spooling Directory Source runner has shutdown.
    复制代码

        3)案例3:Exec
        EXEC执行一个给定的命令获得输出的源,如果要使用tail命令,必选使得file足够大才能看到输出内容

          a)创建agent配置文件

  58. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/exec_tail.conf


    1. a1.sources = r1
  59. a1.sinks = k1
  60. a1.channels = c1

    1. # Describe/configure the source
  61. a1.sources.r1.type = exec
  62. a1.sources.r1.channels = c1
  63. a1.sources.r1.command = tail -F /home/hadoop/flume-1.5.0-bin/log_exec_tail

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

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

    1. # Bind the source and sink to the channel
  68. a1.sources.r1.channels = c1
  69. a1.sinks.k1.channel = c1
    复制代码

          b)启动flume agent a1

  70. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/exec_tail.conf -n a1 -Dflume.root.logger=INFO,console

  71. 复制代码

          c)生成足够多的内容在文件里

  72. root@m1:/home/hadoop# for i in {1..100};do echo “exec tail$i” >> /home/hadoop/flume-1.5.0-bin/log_
    复制代码

          e)在m1的控制台,可以看到以下信息:

  73. 2014-08-10 10:59:25,513 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 20 74 65 73 74 exec tail test }

  74. 2014-08-10 10:59:34,535 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 20 74 65 73 74 exec tail test }
  75. 2014-08-10 11:01:40,557 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 31 exec tail1 }
  76. 2014-08-10 11:01:41,180 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 32 exec tail2 }
  77. 2014-08-10 11:01:41,180 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 33 exec tail3 }
  78. 2014-08-10 11:01:41,181 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 34 exec tail4 }
  79. 2014-08-10 11:01:41,181 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 35 exec tail5 }
  80. 2014-08-10 11:01:41,181 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 36 exec tail6 }
  81. ….
  82. ….
  83. ….
  84. 2014-08-10 11:01:51,550 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 39 36 exec tail96 }
  85. 2014-08-10 11:01:51,550 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 39 37 exec tail97 }
  86. 2014-08-10 11:01:51,551 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 39 38 exec tail98 }
  87. 2014-08-10 11:01:51,551 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 39 39 exec tail99 }
  88. 2014-08-10 11:01:51,551 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:70)] Event: { headers:{} body: 65 78 65 63 20 74 61 69 6C 31 30 30 exec tail100 }
  89. 复制代码

        4)案例4:Syslogtcp
        Syslogtcp监听TCP的端口做为数据源

          a)创建agent配置文件

  90. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/syslog_tcp.conf


    1. a1.sources = r1
  91. a1.sinks = k1
  92. a1.channels = c1

    1. # Describe/configure the source
  93. a1.sources.r1.type = syslogtcp
  94. a1.sources.r1.port = 5140
  95. a1.sources.r1.host = localhost
  96. a1.sources.r1.channels = c1

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

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

    1. # Bind the source and sink to the channel
  101. a1.sources.r1.channels = c1
  102. a1.sinks.k1.channel = c1
    复制代码

          b)启动flume agent a1

  103. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/syslog_tcp.conf -n a1 -Dflume.root.logger=INFO,console

  104. 复制代码

          c)测试产生syslog

  105. root@m1:/home/hadoop# echo “hello idoall.org syslog” | nc localhost 5140
    复制代码

          d)在m1的控制台,可以看到以下信息:

  106. 14/08/10 11:41:45 INFO node.PollingPropertiesFileConfigurationProvider: Reloading configuration file:/home/hadoop/flume-1.5.0-bin/conf/syslog_tcp.conf

  107. 14/08/10 11:41:45 INFO conf.FlumeConfiguration: Added sinks: k1 Agent: a1
  108. 14/08/10 11:41:45 INFO conf.FlumeConfiguration: Processing:k1
  109. 14/08/10 11:41:45 INFO conf.FlumeConfiguration: Processing:k1
  110. 14/08/10 11:41:45 INFO conf.FlumeConfiguration: Post-validation flume configuration contains configuration for agents: [a1]
  111. 14/08/10 11:41:45 INFO node.AbstractConfigurationProvider: Creating channels
  112. 14/08/10 11:41:45 INFO channel.DefaultChannelFactory: Creating instance of channel c1 type memory
  113. 14/08/10 11:41:45 INFO node.AbstractConfigurationProvider: Created channel c1
  114. 14/08/10 11:41:45 INFO source.DefaultSourceFactory: Creating instance of source r1, type syslogtcp
  115. 14/08/10 11:41:45 INFO sink.DefaultSinkFactory: Creating instance of sink: k1, type: logger
  116. 14/08/10 11:41:45 INFO node.AbstractConfigurationProvider: Channel c1 connected to [r1, k1]
  117. 14/08/10 11:41:45 INFO node.Application: Starting new configuration:{ sourceRunners:{r1=EventDrivenSourceRunner: { source:org.apache.flume.source.SyslogTcpSource{name:r1,state:IDLE} }} sinkRunners:{k1=SinkRunner: { policy:org.apache.flume.sink.DefaultSinkProcessor@6538b14 counterGroup:{ name:null counters:{} } }} channels:{c1=org.apache.flume.channel.MemoryChannel{name: c1}} }
  118. 14/08/10 11:41:45 INFO node.Application: Starting Channel c1
  119. 14/08/10 11:41:45 INFO instrumentation.MonitoredCounterGroup: Monitored counter group for type: CHANNEL, name: c1: Successfully registered new MBean.
  120. 14/08/10 11:41:45 INFO instrumentation.MonitoredCounterGroup: Component type: CHANNEL, name: c1 started
  121. 14/08/10 11:41:45 INFO node.Application: Starting Sink k1
  122. 14/08/10 11:41:45 INFO node.Application: Starting Source r1
  123. 14/08/10 11:41:45 INFO source.SyslogTcpSource: Syslog TCP Source starting…
  124. 14/08/10 11:42:15 WARN source.SyslogUtils: Event created from Invalid Syslog data.
  125. 14/08/10 11:42:15 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 68 65 6C 6C 6F 20 69 64 6F 61 6C 6C 2E 6F 72 67 hello idoall.org }
    复制代码

        5)案例5:JSONHandler
          a)创建agent配置文件

  126. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/post_json.conf


    1. a1.sources = r1
  127. a1.sinks = k1
  128. a1.channels = c1

    1. # Describe/configure the source
  129. a1.sources.r1.type = org.apache.flume.source.http.HTTPSource
  130. a1.sources.r1.port = 8888
  131. a1.sources.r1.channels = c1

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

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

    1. # Bind the source and sink to the channel
  136. a1.sources.r1.channels = c1
  137. a1.sinks.k1.channel = c1
    复制代码

          b)启动flume agent a1

  138. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/post_json.conf -n a1 -Dflume.root.logger=INFO,console
    复制代码

          c)生成JSON 格式的POST request

  139. root@m1:/home/hadoop# curl -X POST -d ‘[{ “headers” :{“a” : “a1”,”b” : “b1”},”body” : “idoall.org_body”}]’ http://localhost:8888

  140. 复制代码

          d)在m1的控制台,可以看到以下信息:

  141. 14/08/10 11:49:59 INFO node.Application: Starting Channel c1

  142. 14/08/10 11:49:59 INFO instrumentation.MonitoredCounterGroup: Monitored counter group for type: CHANNEL, name: c1: Successfully registered new MBean.
  143. 14/08/10 11:49:59 INFO instrumentation.MonitoredCounterGroup: Component type: CHANNEL, name: c1 started
  144. 14/08/10 11:49:59 INFO node.Application: Starting Sink k1
  145. 14/08/10 11:49:59 INFO node.Application: Starting Source r1
  146. 14/08/10 11:49:59 INFO mortbay.log: Logging to org.slf4j.impl.Log4jLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
  147. 14/08/10 11:49:59 INFO mortbay.log: jetty-6.1.26
  148. 14/08/10 11:50:00 INFO mortbay.log: Started SelectChannelConnector@0.0.0.0:8888
  149. 14/08/10 11:50:00 INFO instrumentation.MonitoredCounterGroup: Monitored counter group for type: SOURCE, name: r1: Successfully registered new MBean.
  150. 14/08/10 11:50:00 INFO instrumentation.MonitoredCounterGroup: Component type: SOURCE, name: r1 started
  151. 14/08/10 12:14:32 INFO sink.LoggerSink: Event: { headers:{b=b1, a=a1} body: 69 64 6F 61 6C 6C 2E 6F 72 67 5F 62 6F 64 79 idoall.org_body }
  152. 复制代码

        6)案例6:Hadoop sink
          a)创建agent配置文件

  153. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/hdfs_sink.conf


    1. a1.sources = r1
  154. a1.sinks = k1
  155. a1.channels = c1

    1. # Describe/configure the source
  156. a1.sources.r1.type = syslogtcp
  157. a1.sources.r1.port = 5140
  158. a1.sources.r1.host = localhost
  159. a1.sources.r1.channels = c1

    1. # Describe the sink
  160. a1.sinks.k1.type = hdfs
  161. a1.sinks.k1.channel = c1
  162. a1.sinks.k1.hdfs.path = hdfs://m1:9000/user/flume/syslogtcp
  163. a1.sinks.k1.hdfs.filePrefix = Syslog
  164. a1.sinks.k1.hdfs.round = true
  165. a1.sinks.k1.hdfs.roundValue = 10
  166. a1.sinks.k1.hdfs.roundUnit = minute

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

    1. # Bind the source and sink to the channel
  170. a1.sources.r1.channels = c1
  171. a1.sinks.k1.channel = c1
    复制代码

          b)启动flume agent a1

  172. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/hdfs_sink.conf -n a1 -Dflume.root.logger=INFO,console
    复制代码

          c)测试产生syslog

  173. root@m1:/home/hadoop# echo “hello idoall flume -> hadoop testing one” | nc localhost 5140

  174. 复制代码

          d)在m1的控制台,可以看到以下信息:

  175. 14/08/10 12:20:39 INFO instrumentation.MonitoredCounterGroup: Monitored counter group for type: CHANNEL, name: c1: Successfully registered new MBean.

  176. 14/08/10 12:20:39 INFO instrumentation.MonitoredCounterGroup: Component type: CHANNEL, name: c1 started
  177. 14/08/10 12:20:39 INFO node.Application: Starting Sink k1
  178. 14/08/10 12:20:39 INFO node.Application: Starting Source r1
  179. 14/08/10 12:20:39 INFO instrumentation.MonitoredCounterGroup: Monitored counter group for type: SINK, name: k1: Successfully registered new MBean.
  180. 14/08/10 12:20:39 INFO instrumentation.MonitoredCounterGroup: Component type: SINK, name: k1 started
  181. 14/08/10 12:20:39 INFO source.SyslogTcpSource: Syslog TCP Source starting…
  182. 14/08/10 12:21:46 WARN source.SyslogUtils: Event created from Invalid Syslog data.
  183. 14/08/10 12:21:49 INFO hdfs.HDFSSequenceFile: writeFormat = Writable, UseRawLocalFileSystem = false
  184. 14/08/10 12:21:49 INFO hdfs.BucketWriter: Creating hdfs://m1:9000/user/flume/syslogtcp//Syslog.1407644509504.tmp
  185. 14/08/10 12:22:20 INFO hdfs.BucketWriter: Closing hdfs://m1:9000/user/flume/syslogtcp//Syslog.1407644509504.tmp
  186. 14/08/10 12:22:20 INFO hdfs.BucketWriter: Close tries incremented
  187. 14/08/10 12:22:20 INFO hdfs.BucketWriter: Renaming hdfs://m1:9000/user/flume/syslogtcp/Syslog.1407644509504.tmp to hdfs://m1:9000/user/flume/syslogtcp/Syslog.1407644509504
  188. 14/08/10 12:22:20 INFO hdfs.HDFSEventSink: Writer callback called.
    复制代码

          e)在m1上再打开一个窗口,去hadoop上检查文件是否生成

  189. root@m1:/home/hadoop# /home/hadoop/hadoop-2.2.0/bin/hadoop fs -ls /user/flume/syslogtcp

  190. Found 1 items
  191. -rw-r–r– 3 root supergroup 155 2014-08-10 12:22 /user/flume/syslogtcp/Syslog.1407644509504
  192. root@m1:/home/hadoop# /home/hadoop/hadoop-2.2.0/bin/hadoop fs -cat /user/flume/syslogtcp/Syslog.1407644509504
  193. SEQ!org.apache.hadoop.io.LongWritable”org.apache.hadoop.io.BytesWritable^;>Gv$hello idoall flume -> hadoop testing one
  194. 复制代码

        7)案例7:File Roll Sink
          a)创建agent配置文件

  195. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/file_roll.conf


    1. a1.sources = r1
  196. a1.sinks = k1
  197. a1.channels = c1

    1. # Describe/configure the source
  198. a1.sources.r1.type = syslogtcp
  199. a1.sources.r1.port = 5555
  200. a1.sources.r1.host = localhost
  201. a1.sources.r1.channels = c1

    1. # Describe the sink
  202. a1.sinks.k1.type = file_roll
  203. a1.sinks.k1.sink.directory = /home/hadoop/flume-1.5.0-bin/logs

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

    1. # Bind the source and sink to the channel
  207. a1.sources.r1.channels = c1
  208. a1.sinks.k1.channel = c1
    复制代码

          b)启动flume agent a1

  209. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/file_roll.conf -n a1 -Dflume.root.logger=INFO,console

  210. 复制代码

          c)测试产生log

  211. root@m1:/home/hadoop# echo “hello idoall.org syslog” | nc localhost 5555

  212. root@m1:/home/hadoop# echo “hello idoall.org syslog 2” | nc localhost 5555
    复制代码

          d)查看/home/hadoop/flume-1.5.0-bin/logs下是否生成文件,默认每30秒生成一个新文件

  213. root@m1:/home/hadoop# ll /home/hadoop/flume-1.5.0-bin/logs

  214. 总用量 272
  215. drwxr-xr-x 3 root root 4096 Aug 10 12:50 ./
  216. drwxr-xr-x 9 root root 4096 Aug 10 10:59 ../
  217. -rw-r–r– 1 root root 50 Aug 10 12:49 1407646164782-1
  218. -rw-r–r– 1 root root 0 Aug 10 12:49 1407646164782-2
  219. -rw-r–r– 1 root root 0 Aug 10 12:50 1407646164782-3
  220. root@m1:/home/hadoop# cat /home/hadoop/flume-1.5.0-bin/logs/1407646164782-1 /home/hadoop/flume-1.5.0-bin/logs/1407646164782-2
  221. hello idoall.org syslog
  222. hello idoall.org syslog 2
    复制代码

        8)案例8:Replicating Channel Selector
        Flume支持Fan out流从一个源到多个通道。有两种模式的Fan out,分别是复制和复用。在复制的情况下,流的事件被发送到所有的配置通道。在复用的情况下,事件被发送到可用的渠道中的一个子集。Fan out流需要指定源和Fan out通道的规则。

        这次我们需要用到m1,m2两台机器

          a)在m1创建replicating_Channel_Selector配置文件

  223. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector.conf

    1. a1.sources = r1
  224. a1.sinks = k1 k2
  225. a1.channels = c1 c2

    1. # Describe/configure the source
  226. a1.sources.r1.type = syslogtcp
  227. a1.sources.r1.port = 5140
  228. a1.sources.r1.host = localhost
  229. a1.sources.r1.channels = c1 c2
  230. a1.sources.r1.selector.type = replicating

    1. # Describe the sink
  231. a1.sinks.k1.type = avro
  232. a1.sinks.k1.channel = c1
  233. a1.sinks.k1.hostname = m1
  234. a1.sinks.k1.port = 5555

    1. a1.sinks.k2.type = avro
  235. a1.sinks.k2.channel = c2
  236. a1.sinks.k2.hostname = m2
  237. a1.sinks.k2.port = 5555

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

    1. a1.channels.c2.type = memory
  241. a1.channels.c2.capacity = 1000
  242. a1.channels.c2.transactionCapacity = 100
    复制代码

          b)在m1创建replicating_Channel_Selector_avro配置文件

  243. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector_avro.conf

    1. a1.sources = r1
  244. a1.sinks = k1
  245. a1.channels = c1

    1. # Describe/configure the source
  246. a1.sources.r1.type = avro
  247. a1.sources.r1.channels = c1
  248. a1.sources.r1.bind = 0.0.0.0
  249. a1.sources.r1.port = 5555

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

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

    1. # Bind the source and sink to the channel
  254. a1.sources.r1.channels = c1
  255. a1.sinks.k1.channel = c1
    复制代码

          c)在m1上将2个配置文件复制到m2上一份

  256. root@m1:/home/hadoop/flume-1.5.0-bin# scp -r /home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector.conf root@m2:/home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector.conf
  257. root@m1:/home/hadoop/flume-1.5.0-bin# scp -r /home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector_avro.conf root@m2:/home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector_avro.conf

  258. 复制代码
          d)打开4个窗口,在m1和m2上同时启动两个flume agent
  259. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector_avro.conf -n a1 -Dflume.root.logger=INFO,console
  260. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/replicating_Channel_Selector.conf -n a1 -Dflume.root.logger=INFO,console
  261. 复制代码

          e)然后在m1或m2的任意一台机器上,测试产生syslog

  262. root@m1:/home/hadoop# echo “hello idoall.org syslog” | nc localhost 5140
  263. 复制代码

          f)在m1和m2的sink窗口,分别可以看到以下信息,这说明信息得到了同步:

  264. 14/08/10 14:08:18 INFO ipc.NettyServer: Connection to /192.168.1.51:46844 disconnected.
  265. 14/08/10 14:08:52 INFO ipc.NettyServer: [id: 0x90f8fe1f, /192.168.1.50:35873 => /192.168.1.50:5555] OPEN
  266. 14/08/10 14:08:52 INFO ipc.NettyServer: [id: 0x90f8fe1f, /192.168.1.50:35873 => /192.168.1.50:5555] BOUND: /192.168.1.50:5555
  267. 14/08/10 14:08:52 INFO ipc.NettyServer: [id: 0x90f8fe1f, /192.168.1.50:35873 => /192.168.1.50:5555] CONNECTED: /192.168.1.50:35873
  268. 14/08/10 14:08:59 INFO ipc.NettyServer: [id: 0xd6318635, /192.168.1.51:46858 => /192.168.1.50:5555] OPEN
  269. 14/08/10 14:08:59 INFO ipc.NettyServer: [id: 0xd6318635, /192.168.1.51:46858 => /192.168.1.50:5555] BOUND: /192.168.1.50:5555
  270. 14/08/10 14:08:59 INFO ipc.NettyServer: [id: 0xd6318635, /192.168.1.51:46858 => /192.168.1.50:5555] CONNECTED: /192.168.1.51:46858
  271. 14/08/10 14:09:20 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 68 65 6C 6C 6F 20 69 64 6F 61 6C 6C 2E 6F 72 67 hello idoall.org }
  272. 复制代码

        9)案例9:Multiplexing Channel Selector
          a)在m1创建Multiplexing_Channel_Selector配置文件

  273. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector.conf


    1. a1.sources = r1
  274. a1.sinks = k1 k2
  275. a1.channels = c1 c2

    1. # Describe/configure the source
  276. a1.sources.r1.type = org.apache.flume.source.http.HTTPSource
  277. a1.sources.r1.port = 5140
  278. a1.sources.r1.channels = c1 c2
  279. a1.sources.r1.selector.type = multiplexing

    1. a1.sources.r1.selector.header = type
  280. #映射允许每个值通道可以重叠。默认值可以包含任意数量的通道。
  281. a1.sources.r1.selector.mapping.baidu = c1
  282. a1.sources.r1.selector.mapping.ali = c2
  283. a1.sources.r1.selector.default = c1

    1. # Describe the sink
  284. a1.sinks.k1.type = avro
  285. a1.sinks.k1.channel = c1
  286. a1.sinks.k1.hostname = m1
  287. a1.sinks.k1.port = 5555

    1. a1.sinks.k2.type = avro
  288. a1.sinks.k2.channel = c2
  289. a1.sinks.k2.hostname = m2
  290. a1.sinks.k2.port = 5555

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

    1. a1.channels.c2.type = memory
  294. a1.channels.c2.capacity = 1000
  295. a1.channels.c2.transactionCapacity = 100
    复制代码

          b)在m1创建Multiplexing_Channel_Selector_avro配置文件

  296. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector_avro.conf

    1. a1.sources = r1
  297. a1.sinks = k1
  298. a1.channels = c1

    1. # Describe/configure the source
  299. a1.sources.r1.type = avro
  300. a1.sources.r1.channels = c1
  301. a1.sources.r1.bind = 0.0.0.0
  302. a1.sources.r1.port = 5555

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

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

    1. # Bind the source and sink to the channel
  307. a1.sources.r1.channels = c1
  308. a1.sinks.k1.channel = c1
    复制代码

          c)将2个配置文件复制到m2上一份

  309. root@m1:/home/hadoop/flume-1.5.0-bin# scp -r /home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector.conf root@m2:/home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector.conf
  310. root@m1:/home/hadoop/flume-1.5.0-bin# scp -r /home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector_avro.conf root@m2:/home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector_avro.conf
  311. 复制代码

          d)打开4个窗口,在m1和m2上同时启动两个flume agent

  312. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector_avro.conf -n a1 -Dflume.root.logger=INFO,console
  313. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Multiplexing_Channel_Selector.conf -n a1 -Dflume.root.logger=INFO,console
  314. 复制代码

          e)然后在m1或m2的任意一台机器上,测试产生syslog

  315. root@m1:/home/hadoop# curl -X POST -d ‘[{ “headers” :{“type” : “baidu”},”body” : “idoall_TEST1”}]’ http://localhost:5140 && curl -X POST -d ‘[{ “headers” :{“type” : “ali”},”body” : “idoall_TEST2”}]’ http://localhost:5140 && curl -X POST -d ‘[{ “headers” :{“type” : “qq”},”body” : “idoall_TEST3”}]’ http://localhost:5140
  316. 复制代码

         f)在m1的sink窗口,可以看到以下信息:

  317. 14/08/10 14:32:21 INFO node.Application: Starting Sink k1
  318. 14/08/10 14:32:21 INFO node.Application: Starting Source r1
  319. 14/08/10 14:32:21 INFO source.AvroSource: Starting Avro source r1: { bindAddress: 0.0.0.0, port: 5555 }…
  320. 14/08/10 14:32:21 INFO instrumentation.MonitoredCounterGroup: Monitored counter group for type: SOURCE, name: r1: Successfully registered new MBean.
  321. 14/08/10 14:32:21 INFO instrumentation.MonitoredCounterGroup: Component type: SOURCE, name: r1 started
  322. 14/08/10 14:32:21 INFO source.AvroSource: Avro source r1 started.
  323. 14/08/10 14:32:36 INFO ipc.NettyServer: [id: 0xcf00eea6, /192.168.1.50:35916 => /192.168.1.50:5555] OPEN
  324. 14/08/10 14:32:36 INFO ipc.NettyServer: [id: 0xcf00eea6, /192.168.1.50:35916 => /192.168.1.50:5555] BOUND: /192.168.1.50:5555
  325. 14/08/10 14:32:36 INFO ipc.NettyServer: [id: 0xcf00eea6, /192.168.1.50:35916 => /192.168.1.50:5555] CONNECTED: /192.168.1.50:35916
  326. 14/08/10 14:32:44 INFO ipc.NettyServer: [id: 0x432f5468, /192.168.1.51:46945 => /192.168.1.50:5555] OPEN
  327. 14/08/10 14:32:44 INFO ipc.NettyServer: [id: 0x432f5468, /192.168.1.51:46945 => /192.168.1.50:5555] BOUND: /192.168.1.50:5555
  328. 14/08/10 14:32:44 INFO ipc.NettyServer: [id: 0x432f5468, /192.168.1.51:46945 => /192.168.1.50:5555] CONNECTED: /192.168.1.51:46945
  329. 14/08/10 14:34:11 INFO sink.LoggerSink: Event: { headers:{type=baidu} body: 69 64 6F 61 6C 6C 5F 54 45 53 54 31 idoall_TEST1 }
  330. 14/08/10 14:34:57 INFO sink.LoggerSink: Event: { headers:{type=qq} body: 69 64 6F 61 6C 6C 5F 54 45 53 54 33 idoall_TEST3 }
  331. 复制代码

         g)在m2的sink窗口,可以看到以下信息:

  332. 14/08/10 14:32:27 INFO node.Application: Starting Sink k1

  333. 14/08/10 14:32:27 INFO node.Application: Starting Source r1
  334. 14/08/10 14:32:27 INFO source.AvroSource: Starting Avro source r1: { bindAddress: 0.0.0.0, port: 5555 }…
  335. 14/08/10 14:32:27 INFO instrumentation.MonitoredCounterGroup: Monitored counter group for type: SOURCE, name: r1: Successfully registered new MBean.
  336. 14/08/10 14:32:27 INFO instrumentation.MonitoredCounterGroup: Component type: SOURCE, name: r1 started
  337. 14/08/10 14:32:27 INFO source.AvroSource: Avro source r1 started.
  338. 14/08/10 14:32:36 INFO ipc.NettyServer: [id: 0x7c2f0aec, /192.168.1.50:38104 => /192.168.1.51:5555] OPEN
  339. 14/08/10 14:32:36 INFO ipc.NettyServer: [id: 0x7c2f0aec, /192.168.1.50:38104 => /192.168.1.51:5555] BOUND: /192.168.1.51:5555
  340. 14/08/10 14:32:36 INFO ipc.NettyServer: [id: 0x7c2f0aec, /192.168.1.50:38104 => /192.168.1.51:5555] CONNECTED: /192.168.1.50:38104
  341. 14/08/10 14:32:44 INFO ipc.NettyServer: [id: 0x3d36f553, /192.168.1.51:48599 => /192.168.1.51:5555] OPEN
  342. 14/08/10 14:32:44 INFO ipc.NettyServer: [id: 0x3d36f553, /192.168.1.51:48599 => /192.168.1.51:5555] BOUND: /192.168.1.51:5555
  343. 14/08/10 14:32:44 INFO ipc.NettyServer: [id: 0x3d36f553, /192.168.1.51:48599 => /192.168.1.51:5555] CONNECTED: /192.168.1.51:48599
  344. 14/08/10 14:34:33 INFO sink.LoggerSink: Event: { headers:{type=ali} body: 69 64 6F 61 6C 6C 5F 54 45 53 54 32 idoall_TEST2 }
  345. 复制代码

        可以看到,根据header中不同的条件分布到不同的channel上

        10)案例10:Flume Sink Processors
        failover的机器是一直发送给其中一个sink,当这个sink不可用的时候,自动发送到下一个sink。

          a)在m1创建Flume_Sink_Processors配置文件

  346. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors.conf


    1. a1.sources = r1
  347. a1.sinks = k1 k2
  348. a1.channels = c1 c2

    1. #这个是配置failover的关键,需要有一个sink group
  349. a1.sinkgroups = g1
  350. a1.sinkgroups.g1.sinks = k1 k2
  351. #处理的类型是failover
  352. a1.sinkgroups.g1.processor.type = failover
  353. #优先级,数字越大优先级越高,每个sink的优先级必须不相同
  354. a1.sinkgroups.g1.processor.priority.k1 = 5
  355. a1.sinkgroups.g1.processor.priority.k2 = 10
  356. #设置为10秒,当然可以根据你的实际状况更改成更快或者很慢
  357. a1.sinkgroups.g1.processor.maxpenalty = 10000

    1. # Describe/configure the source
  358. a1.sources.r1.type = syslogtcp
  359. a1.sources.r1.port = 5140
  360. a1.sources.r1.channels = c1 c2
  361. a1.sources.r1.selector.type = replicating

    1. 24.
  362. # Describe the sink
  363. a1.sinks.k1.type = avro
  364. a1.sinks.k1.channel = c1
  365. a1.sinks.k1.hostname = m1
  366. a1.sinks.k1.port = 5555

    1. a1.sinks.k2.type = avro
  367. a1.sinks.k2.channel = c2
  368. a1.sinks.k2.hostname = m2
  369. a1.sinks.k2.port = 5555

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

    1. a1.channels.c2.type = memory
  373. a1.channels.c2.capacity = 1000
  374. a1.channels.c2.transactionCapacity = 100
    复制代码

          b)在m1创建Flume_Sink_Processors_avro配置文件

  375. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors_avro.conf

    1. a1.sources = r1
  376. a1.sinks = k1
  377. a1.channels = c1

    1. # Describe/configure the source
  378. a1.sources.r1.type = avro
  379. a1.sources.r1.channels = c1
  380. a1.sources.r1.bind = 0.0.0.0
  381. a1.sources.r1.port = 5555

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

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

    1. # Bind the source and sink to the channel
  386. a1.sources.r1.channels = c1
  387. a1.sinks.k1.channel = c1
    复制代码

          c)将2个配置文件复制到m2上一份

  388. root@m1:/home/hadoop/flume-1.5.0-bin# scp -r /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors.conf root@m2:/home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors.conf
  389. root@m1:/home/hadoop/flume-1.5.0-bin# scp -r /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors_avro.conf root@m2:/home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors_avro.conf
  390. 复制代码

          d)打开4个窗口,在m1和m2上同时启动两个flume agent

  391. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors_avro.conf -n a1 -Dflume.root.logger=INFO,console
  392. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors.conf -n a1 -Dflume.root.logger=INFO,console
  393. 复制代码

          e)然后在m1或m2的任意一台机器上,测试产生log

  394. root@m1:/home/hadoop# echo “idoall.org test1 failover” | nc localhost 5140
    复制代码

          f)因为m2的优先级高,所以在m2的sink窗口,可以看到以下信息,而m1没有:

  395. 14/08/10 15:02:46 INFO ipc.NettyServer: Connection to /192.168.1.51:48692 disconnected.
  396. 14/08/10 15:03:12 INFO ipc.NettyServer: [id: 0x09a14036, /192.168.1.51:48704 => /192.168.1.51:5555] OPEN
  397. 14/08/10 15:03:12 INFO ipc.NettyServer: [id: 0x09a14036, /192.168.1.51:48704 => /192.168.1.51:5555] BOUND: /192.168.1.51:5555
  398. 14/08/10 15:03:12 INFO ipc.NettyServer: [id: 0x09a14036, /192.168.1.51:48704 => /192.168.1.51:5555] CONNECTED: /192.168.1.51:48704
  399. 14/08/10 15:03:26 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 31 idoall.org test1 }
  400. 复制代码

          g)这时我们停止掉m2机器上的sink(ctrl+c),再次输出测试数据:

  401. root@m1:/home/hadoop# echo “idoall.org test2 failover” | nc localhost 5140
    复制代码

          h)可以在m1的sink窗口,看到读取到了刚才发送的两条测试数据:

  402. 14/08/10 15:02:46 INFO ipc.NettyServer: Connection to /192.168.1.51:47036 disconnected.
  403. 14/08/10 15:03:12 INFO ipc.NettyServer: [id: 0xbcf79851, /192.168.1.51:47048 => /192.168.1.50:5555] OPEN
  404. 14/08/10 15:03:12 INFO ipc.NettyServer: [id: 0xbcf79851, /192.168.1.51:47048 => /192.168.1.50:5555] BOUND: /192.168.1.50:5555
  405. 14/08/10 15:03:12 INFO ipc.NettyServer: [id: 0xbcf79851, /192.168.1.51:47048 => /192.168.1.50:5555] CONNECTED: /192.168.1.51:47048
  406. 14/08/10 15:07:56 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 31 idoall.org test1 }
  407. 14/08/10 15:07:56 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 32 idoall.org test2 }
  408. 复制代码

          i)我们再在m2的sink窗口中,启动sink:

  409. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Flume_Sink_Processors_avro.conf -n a1 -Dflume.root.logger=INFO,console
  410. 复制代码

          j)输入两批测试数据:

  411. root@m1:/home/hadoop# echo “idoall.org test3 failover” | nc localhost 5140 && echo “idoall.org test4 failover” | nc localhost 5140
  412. 复制代码

         k)在m2的sink窗口,我们可以看到以下信息,因为优先级的关系,log消息会再次落到m2上:

  413. 14/08/10 15:09:47 INFO node.Application: Starting Sink k1
  414. 14/08/10 15:09:47 INFO node.Application: Starting Source r1
  415. 14/08/10 15:09:47 INFO source.AvroSource: Starting Avro source r1: { bindAddress: 0.0.0.0, port: 5555 }…
  416. 14/08/10 15:09:47 INFO instrumentation.MonitoredCounterGroup: Monitored counter group for type: SOURCE, name: r1: Successfully registered new MBean.
  417. 14/08/10 15:09:47 INFO instrumentation.MonitoredCounterGroup: Component type: SOURCE, name: r1 started
  418. 14/08/10 15:09:47 INFO source.AvroSource: Avro source r1 started.
  419. 14/08/10 15:09:54 INFO ipc.NettyServer: [id: 0x96615732, /192.168.1.51:48741 => /192.168.1.51:5555] OPEN
  420. 14/08/10 15:09:54 INFO ipc.NettyServer: [id: 0x96615732, /192.168.1.51:48741 => /192.168.1.51:5555] BOUND: /192.168.1.51:5555
  421. 14/08/10 15:09:54 INFO ipc.NettyServer: [id: 0x96615732, /192.168.1.51:48741 => /192.168.1.51:5555] CONNECTED: /192.168.1.51:48741
  422. 14/08/10 15:09:57 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 32 idoall.org test2 }
  423. 14/08/10 15:10:43 INFO ipc.NettyServer: [id: 0x12621f9a, /192.168.1.50:38166 => /192.168.1.51:5555] OPEN
  424. 14/08/10 15:10:43 INFO ipc.NettyServer: [id: 0x12621f9a, /192.168.1.50:38166 => /192.168.1.51:5555] BOUND: /192.168.1.51:5555
  425. 14/08/10 15:10:43 INFO ipc.NettyServer: [id: 0x12621f9a, /192.168.1.50:38166 => /192.168.1.51:5555] CONNECTED: /192.168.1.50:38166
  426. 14/08/10 15:10:43 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 33 idoall.org test3 }
  427. 14/08/10 15:10:43 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 34 idoall.org test4 }
  428. 复制代码

        11)案例11:Load balancing Sink Processor
        load balance type和failover不同的地方是,load balance有两个配置,一个是轮询,一个是随机。两种情况下如果被选择的sink不可用,就会自动尝试发送到下一个可用的sink上面。

          a)在m1创建Load_balancing_Sink_Processors配置文件

  429. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors.conf


    1. a1.sources = r1
  430. a1.sinks = k1 k2
  431. a1.channels = c1

    1. #这个是配置Load balancing的关键,需要有一个sink group
  432. a1.sinkgroups = g1
  433. a1.sinkgroups.g1.sinks = k1 k2
  434. a1.sinkgroups.g1.processor.type = load_balance
  435. a1.sinkgroups.g1.processor.backoff = true
  436. a1.sinkgroups.g1.processor.selector = round_robin

    1. # Describe/configure the source
  437. a1.sources.r1.type = syslogtcp
  438. a1.sources.r1.port = 5140
  439. a1.sources.r1.channels = c1

    1. 19.
  440. # Describe the sink
  441. a1.sinks.k1.type = avro
  442. a1.sinks.k1.channel = c1
  443. a1.sinks.k1.hostname = m1
  444. a1.sinks.k1.port = 5555

    1. a1.sinks.k2.type = avro
  445. a1.sinks.k2.channel = c1
  446. a1.sinks.k2.hostname = m2
  447. a1.sinks.k2.port = 5555

    1. # Use a channel which buffers events in memory
  448. a1.channels.c1.type = memory
  449. a1.channels.c1.capacity = 1000
  450. a1.channels.c1.transactionCapacity = 100
    复制代码

          b)在m1创建Load_balancing_Sink_Processors_avro配置文件

  451. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors_avro.conf

    1. a1.sources = r1
  452. a1.sinks = k1
  453. a1.channels = c1

    1. # Describe/configure the source
  454. a1.sources.r1.type = avro
  455. a1.sources.r1.channels = c1
  456. a1.sources.r1.bind = 0.0.0.0
  457. a1.sources.r1.port = 5555

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

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

    1. # Bind the source and sink to the channel
  462. a1.sources.r1.channels = c1
  463. a1.sinks.k1.channel = c1
    复制代码

          c)将2个配置文件复制到m2上一份

  464. root@m1:/home/hadoop/flume-1.5.0-bin# scp -r /home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors.conf root@m2:/home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors.conf
  465. root@m1:/home/hadoop/flume-1.5.0-bin# scp -r /home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors_avro.conf root@m2:/home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors_avro.conf
  466. 复制代码

          d)打开4个窗口,在m1和m2上同时启动两个flume agent

  467. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors_avro.conf -n a1 -Dflume.root.logger=INFO,console

  468. root@m1:/home/hadoop# /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/Load_balancing_Sink_Processors.conf -n a1 -Dflume.root.logger=INFO,console
  469. 复制代码

          e)然后在m1或m2的任意一台机器上,测试产生log,一行一行输入,输入太快,容易落到一台机器上

  470. root@m1:/home/hadoop# echo “idoall.org test1” | nc localhost 5140
  471. root@m1:/home/hadoop# echo “idoall.org test2” | nc localhost 5140
  472. root@m1:/home/hadoop# echo “idoall.org test3” | nc localhost 5140
  473. root@m1:/home/hadoop# echo “idoall.org test4” | nc localhost 5140
    复制代码

          f)在m1的sink窗口,可以看到以下信息:

  474. 14/08/10 15:35:29 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 32 idoall.org test2 }
  475. 14/08/10 15:35:33 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 34 idoall.org test4 }
  476. 复制代码

          g)在m2的sink窗口,可以看到以下信息:

  477. 14/08/10 15:35:27 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 31 idoall.org test1 }
  478. 14/08/10 15:35:29 INFO sink.LoggerSink: Event: { headers:{Severity=0, flume.syslog.status=Invalid, Facility=0} body: 69 64 6F 61 6C 6C 2E 6F 72 67 20 74 65 73 74 33 idoall.org test3 }
  479. 复制代码

        说明轮询模式起到了作用。

        12)案例12:Hbase sink

          a)在测试之前,请先参考《ubuntu12.04+hadoop2.2.0+zookeeper3.4.5+hbase0.96.2+hive0.13.1分布式环境部署》将hbase启动

          b)然后将以下文件复制到flume中:

  480. cp /home/hadoop/hbase-0.96.2-hadoop2/lib/protobuf-java-2.5.0.jar /home/hadoop/flume-1.5.0-bin/lib

  481. cp /home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-client-0.96.2-hadoop2.jar /home/hadoop/flume-1.5.0-bin/lib
  482. cp /home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-common-0.96.2-hadoop2.jar /home/hadoop/flume-1.5.0-bin/lib
  483. cp /home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-protocol-0.96.2-hadoop2.jar /home/hadoop/flume-1.5.0-bin/lib
  484. cp /home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-server-0.96.2-hadoop2.jar /home/hadoop/flume-1.5.0-bin/lib
  485. cp /home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-hadoop2-compat-0.96.2-hadoop2.jar /home/hadoop/flume-1.5.0-bin/lib
  486. cp /home/hadoop/hbase-0.96.2-hadoop2/lib/hbase-hadoop-compat-0.96.2-hadoop2.jar /home/hadoop/flume-1.5.0-bin/lib@@@
  487. cp /home/hadoop/hbase-0.96.2-hadoop2/lib/htrace-core-2.04.jar /home/hadoop/flume-1.5.0-bin/lib
  488. 复制代码

          c)确保test_idoall_org表在hbase中已经存在

          d)在m1创建hbase_simple配置文件

  489. root@m1:/home/hadoop# vi /home/hadoop/flume-1.5.0-bin/conf/hbase_simple.conf

    1. a1.sources = r1
  490. a1.sinks = k1
  491. a1.channels = c1

    1. # Describe/configure the source
  492. a1.sources.r1.type = syslogtcp
  493. a1.sources.r1.port = 5140
  494. a1.sources.r1.host = localhost
  495. a1.sources.r1.channels = c1

    1. # Describe the sink
  496. a1.sinks.k1.type = logger
  497. a1.sinks.k1.type = hbase
  498. a1.sinks.k1.table = test_idoall_org
  499. a1.sinks.k1.columnFamily = name
  500. a1.sinks.k1.column = idoall
  501. a1.sinks.k1.serializer = org.apache.flume.sink.hbase.RegexHbaseEventSerializer
  502. a1.sinks.k1.channel = memoryChannel

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

    1. # Bind the source and sink to the channel
  506. a1.sources.r1.channels = c1
  507. a1.sinks.k1.channel = c1
    复制代码

          e)启动flume agent

  508. /home/hadoop/flume-1.5.0-bin/bin/flume-ng agent -c . -f /home/hadoop/flume-1.5.0-bin/conf/hbase_simple.conf -n a1 -Dflume.root.logger=INFO,console
    复制代码

          f)测试产生syslog

  509. root@m1:/home/hadoop# echo “hello idoall.org from flume” | nc localhost 5140
    复制代码

          g)这时登录到hbase中,可以发现新数据已经插入

  510. root@m1:/home/hadoop# /home/hadoop/hbase-0.96.2-hadoop2/bin/hbase shell
  511. 2014-08-10 16:09:48,984 INFO [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available
  512. HBase Shell; enter ‘help’ for list of supported commands.
  513. Type “exit” to leave the HBase Shell
  514. Version 0.96.2-hadoop2, r1581096, Mon Mar 24 16:03:18 PDT 2014

    1. hbase(main):001:0> list
  515. TABLE
  516. SLF4J: Class path contains multiple SLF4J bindings.
  517. SLF4J: Found binding in [jar:file:/home/hadoop/hbase-0.96.2-hadoop2/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
  518. SLF4J: Found binding in [jar:file:/home/hadoop/hadoop-2.2.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
  519. SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
  520. hbase2hive_idoall
  521. hive2hbase_idoall
  522. test_idoall_org
  523. 3 row(s) in 2.6880 seconds

    1. => [“hbase2hive_idoall”, “hive2hbase_idoall”, “test_idoall_org”]
  524. hbase(main):002:0> scan “test_idoall_org”
  525. ROW COLUMN+CELL
  526. 10086 column=name:idoall, timestamp=1406424831473, value=idoallvalue
  527. 1 row(s) in 0.0550 seconds

    1. hbase(main):003:0> scan “test_idoall_org”
  528. ROW COLUMN+CELL
  529. 10086 column=name:idoall, timestamp=1406424831473, value=idoallvalue
  530. 1407658495588-XbQCOZrKK8-0 column=name:payload, timestamp=1407658498203, value=hello idoall.org from flume
  531. 2 row(s) in 0.0200 seconds

    1. hbase(main):004:0> quit
      复制代码

经过这么多flume的例子测试,如果你全部做完后,会发现flume的功能真的很强大,可以进行各种搭配来完成你想要的工作,俗话说师傅领进门,修行在个人,如何能够结合你的产品业务,将flume更好的应用起来,快去动手实践吧。