1

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 (i.e, 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"}
iammrmehul
  • 730
  • 1
  • 14
  • 35

2 Answers2

0

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);
}
earandap
  • 1,446
  • 1
  • 13
  • 22
  • I do know about converting the `InputStream` to a String. My goal here actually is to convert it into `List` where each element is a line in the S3 object. – iammrmehul May 03 '19 at 13:46
  • Do you know which is the line separator of your string? – earandap May 03 '19 at 13:52
  • I have added sample data. I was wondering if creating a `BufferedStreamReader` using the `InputStream` and then using `readLine()` would accomplish this. – iammrmehul May 03 '19 at 14:01
  • I added an example for the line separator "\n". If your string is a compact JSON the example will not works – earandap May 03 '19 at 15:05
0

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.

artemisian
  • 2,976
  • 1
  • 22
  • 23
  • This is what I am going to do for each line since each line is a json object. I am not sure how this would work with the `InputStream` instance itself. – iammrmehul May 03 '19 at 14:00