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

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

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

任何建议?

提前致谢/

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

假设您现在已启动并运行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。 根据您的需要调整它(特别是port,sink.directory和rollInterval)。 这是一个非常简单的配置文件,有更多选项可用,请查看Flume用户指南。 现在,就此而言,代理人开始并为我运行良好….

这是我没有时间测试的。 默认情况下,HTTP代理接受JSON格式的数据。 您应该能够通过发送带有以下forms的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 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接收器中的exception,因为该接收器期望事件头中的主机和时间戳。 还要确保公开Flume HTTPSource正在侦听的服务器上的端口。

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

 # 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": key必须具有String的值。 "body":不能是JSON对象 – 如果是,则Flume JSONHandler用于解析JSONEvents的Gson库将抛出exception,因为它无法解析JSON – 它期待一个String。

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

尝试这个 :

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

Interesting Posts