如何设置HTTP源以测试Flume设置?

时间:2013-09-06 12:09:47

标签: java hadoop flume

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

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

任何建议?

先谢谢/

3 个答案:

答案 0 :(得分:13)

希望这有助于您入门。我在我的机器上测试这个问题时遇到了一些问题,现在没有时间对它进行彻底的故障排除,但我会接受...

假设您现在已启动并运行Flume,这应该是您的flume.conf文件需要使用HTTP POST源和本地文件接收器(注意:这将转到本地文件,而不是HDFS)< / p>

########## 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。根据您的需要调整它(特别是port,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都应该有效,只要它的格式有效。

答案 1 :(得分:1)

从问题措辞的方式确切地说出你想要的内容有点难,但我假设您想使用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配置示例

  3. # 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 Gson用于解析JSONEvents的JSONHandler库将抛出异常,因为它无法解析JSON - 期待一个字符串。

      这是您需要的JSON格式:

    2. [
        {
          "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或HTTPSource将抛出您将能够在Flume Logs中看到的异常(可能在/var/log/flume-ng/中)。要解决此问题,请增加tier1.channels.channel1.capacity
      • 如果您看到Flume日志中的异常指示Flume因权限无法写入HDFS,或者因为找不到目标目录,请检查以确保您在HDFS中创建了目标目录并打开了其权限详见上文第1步。

答案 2 :(得分:0)

试试这个:

curl -X POST -H'Content-Type:application / json; charset = UTF-8'-d'[{“username”:“xrqwrqwryzas”,“password”:“12124sfsfsfas123”}]'http://yourdomain.com:81/