AWS Lambda解析CloudWatch日志流

时间:2016-11-09 17:35:44

标签: java json eclipse amazon-web-services aws-lambda

我正在编写一个AWS Lambda,它将处理来自CloudWatch的日志流。我正在 Java 中实现它。我正在努力解决几个问题:

  1. 如何解码和解压缩收到的日志。 在Python下面的代码工作:
  2. import json
    import base64
    import zlib
    
    def stream_gzip_decompress(stream):
        dec = zlib.decompressobj(32 + zlib.MAX_WBITS)  # offset 32 to skip the header
        foo=''
        for chunk in stream:
            rv = dec.decompress(chunk)
            if rv:
                foo += rv
        return foo
    
    def lambda_handler(event, context):
        # Decode and decompress the AWS Log stream to extract json object
        stream=json.dumps(event['awslogs']['data'])
        payload=json.loads(stream_gzip_decompress(stream.decode('base64')))
        print(payload)
    

    如何在Java中实现类似的功能?

    1. 当我尝试添加外部jar org.json来解析输入JSON数据时。虽然我尝试了stackoverflow上给出的大部分内容,但我得到了以下错误。
    2. 错误:

        

      {" errorMessage":"加载课程时出错   UpdateCurrentNotificationStatus:org / json / JSONException",
        " errorType":" class java.lang.NoClassDefFoundError" }

      以下是我写的示例代码:

      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
      
      import org.json.JSONException;
      import org.json.JSONObject;
      
      import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
      import com.amazonaws.services.lambda.runtime.Context; 
      
      public class UpdateCurrentNotificationStatus implements RequestStreamHandler {
          public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
                  throws IOException {
              int letter;
              while((letter = inputStream.read()) != -1)
              {
                  //outputStream.write(Character.toUpperCase(letter));
                  JSONObject json = new JSONObject(letter);
                  try {
                    String stream = json.getString("awslogs");
                    System.out.println(stream);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              }
          }
      }
      

      有人可以帮我吗?

      谢谢,

      普拉萨德

1 个答案:

答案 0 :(得分:1)

您需要做的是接受CloudWatchLogsEvent作为输入参数。

    public class Testing implements RequestHandler<CloudWatchLogsEvent, String> {


        public String handleRequest(CloudWatchLogsEvent request, Context context){
            context.getLogger().log(request.toString());
            String timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime());
            context.getLogger().log("Invocation started: " + timeStamp);

            context.getLogger().log(request.toString());

            timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime());
            context.getLogger().log("Invocation completed: " + timeStamp);
            return "completed";
        }
}

请记住,这将打印编码的基本64字符串,也是gzip。你必须解码和解压缩。网上有关于如何做到这一点的例子,所以我不会深入研究。一个例如在这里Decode Base64 encoded ZIP archive (GZIP)