简体   繁体   中英

Java - Read (JSON) data from S3 object line by line

I have an S3 object that contains JsonL lines. I want to read this object line by line in Java so that I can recursively keep parsing each line (ie, each json) into a POJO and eventually convert the object contents into a list of POJO.

As I understand, AmazonS3.getObject().getObjectContent() returns a stream to the object's content. My next step is to read a line and convert to string which I can then parse into a POJO. I am not sure how to operate on this InputStream to keep obtaining the next line in String format.

Sample data :

{"key1":"val", "key2":"val1", "key3":"val2"}
{"key1":"val3", "key2":"val4", "key3":"val5"}
{"key1":"val6", "key2":"val7", "key3":"val8"}

You have a lot of options for converting the InputStream to String. Take a look here .

 String json = "{\"key1\":\"val\", \"key2\":\"val1\", \"key3\":\"val2\"}\n" +
        "{\"key1\":\"val3\", \"key2\":\"val4\", \"key3\":\"val5\"}\n" +
        "{\"key1\":\"val6\", \"key2\":\"val7\", \"key3\":\"val8\"}";

InputStream in = new ByteArrayInputStream(json.getBytes());

try (BufferedReader buffer = new BufferedReader(new InputStreamReader(in))) {
    buffer.lines().forEach(System.out::println);
}

If you can afford it you can use the jackson libraries and simplify your implementation:

Maven dependency

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

Code

ObjectMapper mapper = new ObjectMapper();
MyObject obj = mapper.readValue(inputStream, MyObject.class);

For applying this logic to each line then loop over each line as explained here and apply the same readValue method which is overloaded for String as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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