在java中操纵json数据... java newbie

时间:2017-10-10 12:54:56

标签: java json

我现在正在学习java并在Kafka Streams中构建一个小应用程序来提取数据并基本上创建一个新流。

我有一个这样的主要类:

public class Main {

    public static void main(final String[] args) throws Exception {

        final Properties streamsConfiguration = new Properties();

        streamsConfiguration.put("application.id", "streams-consumer-logs");
        streamsConfiguration.put("client.id", "consumer-client");
        streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
        streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
        streamsConfiguration.put(StreamsConfig.DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG, WallclockTimestampExtractor.class);
        streamsConfiguration.put("servers");


        final Serde<String> stringSerde = Serdes.String();
        final Serde<Long> longSerde = Serdes.Long();

        System.out.println(streamsConfiguration);


        final KStreamBuilder builder = new KStreamBuilder();
        final KStream<String, String> textLines = builder.stream( stringSerde, stringSerde,"sdp_logs");


        //textLines.print();
        System.out.println("test");



        KStream<String, String> wordCounts = textLines
                .map((key, value) ->  {
                    //how do I manipulate this value which is a json object?
                    Gson gson = new Gson();
                    String json = gson.toJson(value);

                    FilterLogs filterlogs = new FilterLogs(json);

                    String filterlogs = filterlogs.execute();

                    return KeyValue.pair(key, json);
                });

        //KStream<String, Long> streamWrite = wordCounts.toStream();

        wordCounts.print();


        final KafkaStreams streams = new KafkaStreams(builder, streamsConfiguration);

        streams.cleanUp();
        streams.start();


        Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
    }

}

然后我有一个FilterLogs类看起来像这样:

public class FilterLogs {

    private type? jsoonObj;

    public FilterLogs (type? jsonObj) {
        this.jsonObj = jsonObj;
    }


    public type? getResult() { return result; }


    public void execute () {

        //manipulation of jsonObj goes in here?

        }
    }

}

我的json对象看起来像value返回KStream<String, String> wordCounts = textLines.map((key, value) -> {}

{
  "tags": [
    "dgtl"
  ], 
  "beat": {
    "hostname": "host1", 
    "name": "host`", 
    "version": "5.4.1"
  }, 
  "input_type": "log", 
  "@timestamp": "2017-09-27T20:01:52.282Z", 
  "source": "/log/state-change.log", 
  "offset": 1725163, 
  "message": "message of the log is here"
  "type": "log"
}

从我对Java的理解(因为我来自函数式编程范例)是我必须采用value,在我的情况下将它传递给类FilterLogs,然后该类应该返回新的value ...对吗?

我困惑的地方是什么类型用于课程,我如何实际解析json并用它做什么?

通常情况下,在javascript中,我可以通过一堆循环来解析它并按照我想要的方式进行解析,但我不太了解如何用Java实现这一点。

如果我希望我的json对象像这样返回怎么办?

{
  "tags": [
    "dgtl"
  ], 
  "beat": {
    "hostname": "host1"
  }, 
  "offset": 1725163, 
  "message": "message of the log is here"
}

1 个答案:

答案 0 :(得分:0)

你应该结帐新的

  1. JSON-P(用于JSON处理和解析) - http://json-b.net/
  2. 最近发布的JSON-B(用于JSON绑定)apis - https://github.com/javaee/jsonp
  3. 它们是专门针对您的所有用例而制作的