DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Mastering Backpressure in Java: Concepts, Real-World Examples, and Implementation
  • Creating Scalable OpenAI GPT Applications in Java
  • How To Best Use Java Records as DTOs in Spring Boot 3

Trending

  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • AI’s Role in Everyday Development
  • Performance Optimization Techniques for Snowflake on AWS
  • Concourse CI/CD Pipeline: Webhook Triggers
  1. DZone
  2. Data Engineering
  3. Databases
  4. API Streaming Response with Oracle and Java

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.

By 
Setu Patani user avatar
Setu Patani
·
Jul. 21, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
7.8K Views

Join the DZone community and get the full member experience.

Join For Free

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. 

API Java (programming language) Data stream Database Use case

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together
  • Mastering Backpressure in Java: Concepts, Real-World Examples, and Implementation
  • Creating Scalable OpenAI GPT Applications in Java
  • How To Best Use Java Records as DTOs in Spring Boot 3

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!