flume httpsource由于格式错误而拒绝json主体

gtlvzcf8  于 2021-06-04  发布在  Flume
关注(0)|答案(1)|浏览(435)

我有一个flume代理配置,其中我使用httpsource从特定服务接收事件数据。出于测试目的,我在python中将静态json结构创建为一个名为data的string对象(参见下面的代码段1),并将该对象发送到具有适当头的flume,但是flume始终返回一个400错误请求错误(参见下面的代码段2)。下面的代码段3提供了相关的flume执行异常消息。
问:有人能告诉我,我的静态json请求导致flume httpsource拒绝它,这有什么问题吗?是否有其他问题我可能会遗漏,这是无关的json数据?谢谢。
代码段1(生成包含静态json数据的伪http请求的python脚本)

import urllib2, json

 serviceName = "serviceA"
 timestamp = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')

 body = "{ \"service\":\"" + serviceName + "\" }"
 print("BODY: " +  body)

 //My JSON data
 data = "[{ \"headers\" : { \"timestamp\" : \"" + timestamp + "\" }, \"body\" : " + body + " }]"
 print("DATA: "  + data)

 req = urllib2.Request("http://10.1.0.100:5140")
 req.add_header('Content-Type', 'application/json')
 response = urllib2.urlopen(req, data)

代码段2—python脚本的执行输出

BODY: { "service":"serviceA" }
  DATA: [{ "headers" : { "timestamp" : "2016-01-13 12:26:48" }, "body" : { "service":"serviceA" } }]
  Traceback (most recent call last):
  File "./event_data_gen.py", line 57, in <module>
  response = urllib2.urlopen(req, data)  
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
  return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
  response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
  'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
  return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
  result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
  raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
  urllib2.HTTPError: HTTP Error 400: Bad request from client. 
  Request has invalid JSON Syntax.

代码段3-flume执行输出中的异常消息

2016-01-13 12:26:48,653 (34313572@qtp-604003190-13)
  [WARN -     org.apache.flume.source.http.HTTPSource$FlumeHTTPServlet.doPost(HTTPSource.java:      242)] Received bad request from client. 
  org.apache.flume.source.http.HTTPBadRequestException: Request has invalid JSON Syntax.
  at org.apache.flume.source.http.JSONHandler.getEvents(JSONHandler.java:119)
  at  org.apache.flume.source.http.HTTPSource$FlumeHTTPServlet.doPost(HTTPSource.java:240)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:814)
  at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:401)
  at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
  at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
  at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
  at org.mortbay.jetty.Server.handle(Server.java:326)
  at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
  at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
  at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
  at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
  at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
  at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
  at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
  Caused by: com.google.gson.JsonSyntaxException:  java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line  1 column 67
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
  at com.google.gson.Gson.fromJson(Gson.java:795)
  at com.google.gson.Gson.fromJson(Gson.java:761)
  at org.apache.flume.source.http.JSONHandler.getEvents(JSONHandler.java:117)
... 17 more
  Caused by: java.lang.IllegalStateException: Expected a string but was  BEGIN_OBJECT at line 1 column 67
  at com.google.gson.stream.JsonReader.nextString(JsonReader.java:464)
  at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:349)
  at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:337)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
xqk2d5yq

xqk2d5yq1#

正如@martyn w在他的评论中指出的那样,jsonhandler期望并验证json具有特定的结构。
这在jsonhandler的javadocs中有描述:
httpsource的jsonhandler,它接受一个事件数组。如果反序列化由于格式错误或任何其他原因而失败,此处理程序将引发异常。每个事件都必须编码为带有两个键值对的Map。
headers—这个键值对的键是“headers”。这个键的值是另一个Map,它表示事件头。这些标头按原样插入到flume事件中。
body—body是表示事件主体的字符串。这个键值对的键是“body”。所有的键值对都被认为是头。举个例子:
[{“headers”:{“a”:“b”,“c”:“d”},“body”:“random\u body”},{“headers”:{“e”:“f”},“body”:“random\u body 2”}]

相关问题