【问题标题】:How to setup a HTTP Source for testing Flume setup?如何设置 HTTP 源以测试 Flume 设置?
【发布时间】:2017-06-24 00:25:58
【问题描述】:

我是 Flume 和 Hadoop 的新手。我们正在开发一个 BI 模块,我们可以在其中将来自不同服务器的所有日志存储在 HDFS 中。

为此,我正在使用 Flume。我刚开始尝试。成功创建了一个节点,但现在我愿意设置一个 HTTP 源和一个接收器,它将通过 HTTP 将传入请求写入本地文件。

有什么建议吗?

提前致谢/

【问题讨论】:

    标签: java hadoop flume


    【解决方案1】:

    希望这可以帮助您入门。我在我的机器上测试它时遇到了一些问题,现在没有时间进行全面的故障排除,但我会解决的......

    假设你现在已经启动并运行了 Flume,这应该是你的 flume.conf 文件需要使用 HTTP POST 源和本地文件接收器的样子(注意:这是一个本地文件,而不是 HDFS)

    ########## NEW AGENT ########## 
    # flume-ng agent -f /etc/flume/conf/flume.httptest.conf -n httpagent
    # 
    
    # slagent = SysLogAgent
    ###############################
    httpagent.sources = http-source
    httpagent.sinks = local-file-sink
    httpagent.channels = ch3
    
    # Define / Configure Source (multiport seems to support newer "stuff")
    ###############################
    httpagent.sources.http-source.type = org.apache.flume.source.http.HTTPSource
    httpagent.sources.http-source.channels = ch3
    httpagent.sources.http-source.port = 81
    
    
    # Local File Sink
    ###############################
    httpagent.sinks.local-file-sink.type = file_roll
    httpagent.sinks.local-file-sink.channel = ch3
    httpagent.sinks.local-file-sink.sink.directory = /root/Desktop/http_test
    httpagent.sinks.local-file-sink.rollInterval = 5
    
    # Channels
    ###############################
    httpagent.channels.ch3.type = memory
    httpagent.channels.ch3.capacity = 1000
    

    使用第二行的命令启动 Flume。根据您的需要调整它(尤其是端口、sink.directory 和 rollInterval)。这是一个非常简单的最低配置文件,还有更多可用选项,请查看 Flume 用户指南。现在,就目前而言,代理启动并运行良好......

    这是我没有时间测试的。默认情况下,HTTP 代理接受 JSON 格式的数据。您 - 应该 - 可以通过发送带有如下形式的 cURL 请求来测试此代理:

    curl -X POST -H 'Content-Type: application/json; charset=UTF-8' -d '{"username":"xyz","password":"123"}' http://yourdomain.com:81/
    

    -X 将请求设置为 POST,-H 发送标头,-d 发送数据(有效的 json),然后是 host:port。对我来说问题是我得到一个错误:

    WARN http.HTTPSource: Received bad request from client. org.apache.flume.source.http.HTTPBadRequestException: Request has invalid JSON Syntax.
    

    在我的 Flume 客户端中,JSON 无效?所以有些东西是错误的。弹出错误的事实表明 Flume 源正在接收数据。只要格式有效,您所拥有的任何 POST 都应该可以正常工作。

    【讨论】:

    • 嘿。非常感谢您的回复...我尝试设置 http 代理。
    • Flume 处理程序接受一组事件(即使只有一个事件,也必须以数组的形式发送该事件)。所以尝试发送 JSONArray 而不是 JSONObject...对我来说效果很好...这是您可以尝试的命令 - curl -X POST -H 'Content-Type: application/json; charset=UTF-8' -d '[{"username":"xyz","password":"123"}]' yourdomain.com:81
    • 我遇到了同样的问题,但它只有在我以以下格式发送数据时才有效 [{ "headers" : { "timestamp" : "434324343", "host" : "random_host.example .com”},“body”:“random_body”},{“headers”:{“namenode”:“namenode.example.com”,“datanode”:“random_datanode.example.com”},“body”:“ real_random_body" }] 以及如何以这种格式创建 JSON?在发送到 Flume 之前将我的 json 转换为这种 json 格式是开销。有什么线索吗?
    • 我实际上想出了如何让它工作,但我无法编辑这个答案(甚至认为这是这样做的方法)。最好的方法是在 Flume 配置中定义时间戳拦截器和主机拦截器,并让它们期待您在 JSON 标头对象中传递的参数。您可以配置 HDFS 接收器和 HTTPSource,就像在他们网站上的官方 Flume 文档中一样。最大的注意 - JSON 中的 "body": 键必须有一个 String 作为值 - 它不能是 JSON 对象,因为 Gson 期望 JSONHandler 中的字符串。
    【解决方案2】:

    从问题的措辞中很难准确说出您想要什么,但我假设您希望使用 HTTP POST 请求将 JSON 发送到 Flume,然后让 Flume 将这些 JSON 事件转储到HDFS(不是本地文件系统)。如果这就是你想要做的,这就是你需要做的。

    1. 确保首先在 HDFS 中为 Flume 创建一个目录以将事件发送到。例如,如果您想在 HDFS 中向/user/flume/events 发送事件,您可能必须运行以下命令:

      $ su - hdfs
      $ hdfs dfs -mkdir /user/flume
      $ hdfs dfs -mkdir /user/flume/events
      $ hdfs dfs -chmod -R 777 /user/flume
      $ hdfs dfs -chown -R flume /user/flume
      
    2. 将 Flume 配置为使用 HTTP Source 和 HDFS Sink。您需要确保添加主机和时间戳的拦截器,否则您的事件将导致 HDFS 接收器中的异常,因为该接收器在事件标头中需要主机和时间戳。还要确保暴露 Flume HTTPSource 正在侦听的服务器上的端口。

      这是适用于 CDH-5.7.0 的 Cloudera Quickstart Docker 容器的 Flume 配置示例

    # Please paste flume.conf here. Example:
    
    # Sources, channels, and sinks are defined per # agent name, in this case 'tier1'.
    tier1.sources  = source1
    tier1.channels = channel1
    tier1.sinks    = sink1
    tier1.sources.source1.interceptors = i1 i2 
    tier1.sources.source1.interceptors.i1.type = host
    tier1.sources.source1.interceptors.i1.preserveExisting = false
    tier1.sources.source1.interceptors.i1.hostHeader = host
    tier1.sources.source1.interceptors.i2.type = timestamp
    
    # For each source, channel, and sink, set # standard properties.
    tier1.sources.source1.type     = http
    tier1.sources.source1.bind     = 0.0.0.0
    tier1.sources.source1.port     = 5140
    # JSONHandler is the default for the httpsource # 
    tier1.sources.source1.handler = org.apache.flume.source.http.JSONHandler
    tier1.sources.source1.channels = channel1
    tier1.channels.channel1.type   = memory
    tier1.sinks.sink1.type         = hdfs
    tier1.sinks.sink1.hdfs.path = /user/flume/events/%y-%m-%d/%H%M/%S
    tier1.sinks.sink1.hdfs.filePrefix = event-file-prefix-
    tier1.sinks.sink1.hdfs.round = false
    tier1.sinks.sink1.channel      = channel1
    
    # Other properties are specific to each type of # source, channel, or sink. In this case, we # specify the capacity of the memory channel.
    tier1.channels.channel1.capacity = 1000
    
    1. 有必要创建一个 Flume 客户端,它可以将 JSON 事件以它期望的格式发送到 Flume HTTP(这个客户端可以像 curl 请求一样简单)。关于格式最重要的一点是 JSON "body": 键必须有一个值即字符串"body": 不能是 JSON 对象 - 如果是,Flume JSONHandler 用来解析 JSONEvents 的 Gson 库将抛出异常,因为它无法解析 JSON - 它需要一个字符串.

      这是您需要的 JSON 格式:

    [
      {
        "headers": {
          "timestamp": "434324343",
          "host": "localhost",
        },
        "body": "No matter what, this must be a String, not a list or a JSON object",
      },
      { ... following events take the same format as the one above ...}
    ]
    

    疑难解答

    • 如果 Flume 正在向您的客户端(例如 Curl)发送 200 OK Success 消息,但您在 HDFS 上看不到任何文件,请检查 Flume 日志。我早期遇到的一个问题是我的 Flume Channel 没有足够的容量,因此无法接收任何事件。如果发生这种情况,Channel 或 HTTPSource 将抛出异常,您将能够在 Flume 日志中看到(可能在 /var/log/flume-ng/ 中)。要解决此问题,请增加tier1.channels.channel1.capacity
    • 如果您在 Flume 日志中看到异常,表明 Flume 由于权限而无法写入 HDFS,或者因为找不到目标目录,请检查以确保您在 HDFS 中创建了目标目录并打开了其权限如上文第 1 步所述。

    【讨论】:

    【解决方案3】:

    试试这个:

    curl -X POST -H '内容类型:应用程序/json; charset=UTF-8' -d '[{"username":"xrqwrqwryzas","password":"12124sfsfsfas123"}]' http://yourdomain.com:81/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 2011-03-11
      • 2017-01-31
      相关资源
      最近更新 更多