The answer to your question involves several steps for both server side and client side. Note: all of the code examples below are in scala rather than java for terseness. On the server side you need to implement:
1. Streaming the Data from a DB
There is another stack question/answer that explains how to get rows out of a database using streaming methods. The end result is that you have a source of rows:
type DataObject = ???
val source : Source[DataObject,_] = ??? // see linked answer
2. Converting DataObject to Bytes
This part can only be answered knowing the specifics of your data and it sounds like you already have this part implemented:
val converter : DataObject => ByteString
3. Attaching to a REST Endpoint
You can use akka-http
to setup a REST endpoint that clients can query to get the data. The Source
of your ByteString
values will be the Source feeding the stream entity for your HttpResponse
Setup a Route
that responds to GET
requests on a particular path:
val route : Route =
get {
path("/queryDB") {
complete(HttpResponse(entity = HttpEntity(ContentTypes.`application/octet-stream`, source.map(converter))))
}
}
And use the Route to launch your service:
Http().bindAndHandle(route, "localhost", 8080)
On the client side there are similar mechanisms for reading data from an HttpResponse.