{{announcement.body}}
{{announcement.title}}

API Streaming Response with Oracle and Java

DZone 's Guide to

API Streaming Response with Oracle and Java

Using Java to get the data in a streamed manner from an Oracle database. This helps in early first record processing when the dataset is huge.

· Java Zone ·
Free Resource

Data Stream is a fascinating thing to work with. We see this in many places nowadays. What we do not see often is Java application getting streamed data out of Oracle. Getting data out of Oracle in streamed fashion is extremely useful when the expected data set is large. I will share the details around the use-case, problem, solution, implementation, and advantages for the same.

Use Case

A few months ago, I ran into a situation where it was needed to fetch close to million records from the Oracle DB. Now Oracle may not be the best database to handle such a request, but if you depend on Oracle database, you need to make it work.

Consider a scenario where one needs to get all productId that satisfies the given criteria (criteria can change for each and every request). Once we get the productIds, for each we need to take some action, say fetch quantity.

Problem

Our traditional Java processing would go something like this:

  • Make a database call. Get the List of productId
  • For each productId get quantity

Seems straight forward, right? But the issue comes when we do it for million records, and that too with parallel request and shared DB connections. Our object will be huge leading to memory issues. Initial caller, who wanted to know the product and its quantity will have to wait till we finish processing all records. Not a great user experience.

Solution

The first thing that comes to our mind is pagination. Pagination is a way to break this into chunks and feed small data at a time. This may not be feasible always. And with microservices, we may not want someone calling a thousand times to get one requirement. 

So the next thing that comes to our mind is what if we want the quantity of a product as soon as we know the productId. Hence streaming!

Now it will not be beneficial if we do partial stream. We need our source (Oracle database here) to give us data in a streamed manner. If our API is capable to give out a response stream rather than one bulky response, that would be even more awesome. This will enable API caller to process data as soon as it arrives.

Implementation 

Here we have a rest endpoint (JAX-RS).

Note: There are no changes needed on Oracle side. This is a Java implementation which is using Oracle as a database.

Java
 




xxxxxxxxxx
1


 
1
@Produces("application/octet-stream")
2
@GET
3
@Path("/product/ids")
4
public Object getProductIds(@QueryParam("c1") String condition1, @QueryParam("c2") String condition2, ...);



Oracle JDBC call:

Java
 




xxxxxxxxxx
1
14


 
1
public StreamingOutput getproductIdStream(Map<String, String> conditions) {
2
        StreamingOutput stream = null;
3
        try {
4
            stream = new StreamingOutput() {
5
                @Override
6
                public void write(OutputStream os) throws IOException, WebApplicationException {
7
                    namedParameterJdbcTemplate.query(sql, conditions, new productResultSetExtractor(os));
8
                }
9
            };
10
        } catch (Exception e) {
11
             e.printStackTrace();
12
        }
13
        return stream;
14
    }



ProductResultSetExtractor class:

Java
 




xxxxxxxxxx
1
36


 
1
public class ProductResultSetExtractor implements ResultSetExtractor<Void> {
2
 
3
    private final OutputStream data;
4
 
5
    public ProductResultSetExtractor(final OutputStream data) {
6
        this.data = data;
7
    }
8
 
9
    @Override
10
    public Void extractData(final ResultSet resultSet) {
11
        final ObjectMapper objectMapper = new ObjectMapper();
12
        try (JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(data, JsonEncoding.UTF8)) {
13
            prepareJson(resultSet, jsonGenerator);
14
            jsonGenerator.flush();
15
 
16
        } catch (IOException | SQLException e) {
17
            throw new RuntimeException(e);
18
        }
19
        return null;
20
    }
21
 
22
    private static void prepareJson(final ResultSet resultSet, final JsonGenerator )
23
            throws SQLException, IOException {
24
        final ResultSetMetaData metaData = resultSet.getMetaData();
25
        final int columnCount = metaData.getColumnCount();
26
        JsonGenerator.writeStartArray();
27
        while (resultSet.next()) {
28
            JsonGenerator.writeStartObject();
29
            for (int i = 1; i <= columnCount; i++) {
30
                JsonGenerator.writeObjectField(metaData.getColumnName(i), resultSet.getObject(i));
31
            }
32
            JsonGenerator.writeEndObject();
33
        }
34
        JsonGenerator.writeEndArray();
35
    }
36
}



This will give out the following stream:

Java
 




xxxxxxxxxx
1


1
[
2
  { "productId": "p1"},
3
  { "productId": "p2"},
4
  { "productId": "p3"},
5
  ...
6
  { "productId": "pN"}
7
]


Here caller can process as soon as { "productId": "p1"} is received.  

Advantages

Adapting to streaming data from Oracle has few advantages when returning dataset is large.

  • Low memory consumption
  • Early first record. This is extremely useful. The caller does not need to wait until the complete object is prepared.
  • API performance does not decrease much with an increase in response dataset. (Assuming caller is processing as soon as the first record is received) 

Disadvantage

If stream breaks, it becomes difficult to find out how much data is processed or what was the last successful record received on the caller side. This may lead to the whole resend of the dataset.

Conclusion

Streaming may not be needed in all the places, but when such a use case comes, it becomes extremely helpful. The early first record is very helpful when you do not worry about how much total time will it take as you can work on the next set of instructions in parallel. 

Topics:
database, java, oracle, streaming, webservice

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}