简体   繁体   English

使用Java在flink中进行JSON聚合

[英]aggregation on JSON in flink using Java

I am reading JSON data from a file. 我正在从文件读取JSON数据。

Sample Data 样本数据

{"name":"user1","myparam0":false,"myparam1":"44750004-23df-4960-88be-ba0884291597","myparam2":"36A3BF29-23df-EE2A-76B9-19BC1C854BA7","myparam3":"http://www.seloger.com/","myparam4":"http://www.seloger.com/erreur-temporaire/husk-pie","ver":"4.0.0"}
{"name":"user1","myparam0":true,"myparam1":"44750004-8bff-4960-88be-ba0884291597","myparam2":"36A3BF29-88be-EE2A-76B9-19BC1C854BA7","myparam3":"","myparam4":"http://www.seloger.com/erreur-temporaire/binde","ver":"4.0.0"}

I have written a sample code to read from the file and converted data to JSON like this 我已经编写了一个示例代码来读取文件,并将数据转换为JSON,如下所示

DataStream<Object> input = env.readTextFile("file:///home/ravisankar/workspace/temporary/input.file")
                .map((line) -> {
                    return JSON.parseFull(line);
                });

Now I need to calculate how many myparam3 are empty in 15 seconds based on the name. 现在,我需要根据名称计算在15秒内有多少myparam3为空。 and group by myparam4 并按myparam4

Ex: {
  "user1": {
    "myparams3": 1,
    "myparam4": {
      "http://www.seloger.com/erreur-temporaire/binde": 1,
      "http://www.seloger.com/erreur-temporaire/husk-pie": 1
    }
  }
}

Is it possible to extract such data like this from Flink?? 是否可以从Flink中提取这样的数据? I don't see any examples working on JSON using Java. 我看不到使用Java在JSON上运行的任何示例。 Thanks for your time 谢谢你的时间

您可以将json字符串解析为对象,即通过jackson库,并照常操作Java对象流

you can use jackson to parse the json to an object then make a loop to count your elements 您可以使用杰克逊将json解析为对象,然后进行循环以计算元素

private ObjectMapper objectMapper = new ObjectMapper() ;
...
Object element = objectMapper.readValue( jsonString , Object.class );

or you can use a regex that matches "myparam3":"" and calculate tha matches 或者您可以使用匹配“ myparam3”的正则表达式:“”并计算tha匹配项

public static void main( String[] args ) throws IOException
{
    String str = "{\"name\":\"user1\",\"myparam0\":false,\"myparam1\":\"44750004-23df-4960-88be-ba0884291597\",\"myparam2\":\"36A3BF29-23df-EE2A-76B9-19BC1C854BA7\",\"myparam3\":\"http://www.seloger.com/\",\"myparam4\":\"http://www.seloger.com/erreur-temporaire/husk-pie\",\"ver\":\"4.0.0\"}\r\n" + 
            "{\"name\":\"user1\",\"myparam0\":true,\"myparam1\":\"44750004-8bff-4960-88be-ba0884291597\",\"myparam2\":\"36A3BF29-88be-EE2A-76B9-19BC1C854BA7\",\"myparam3\":\"\",\"myparam3\":\"\",\"myparam3\":\"\"\"myparam4\":\"http://www.seloger.com/erreur-temporaire/binde\",\"ver\":\"4.0.0\"}";

    Pattern pattern = Pattern.compile("\"myparam3\":\"\"");


    Matcher matcher = pattern.matcher(str);

    int count = 0;
    while (matcher.find()) {
        count++;
    }
    System.out.println("Matches found : " + count );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM