Java- read data from voltdb with native procedure

839 Views Asked by At

I'm trying to read data from a VoltDB database with Java. Now, it can be done using result sets from SQL statements, but there should (I'm told) be another way of doing it, native to VoltDB, similarly to how data is written to a VoltDB database (with client.callProcedure). I can't figure out how to do that; it seems like it should be a pretty simple thing to do, but I don't see any simple way to do it in client.

2

There are 2 best solutions below

2
On

Yes, if you are using client.callProcedure for your writes, you can certainly use the same API for your reads. Here is a simple example:

ClientResponse cr = client.callProcedure(procname,parameters);
VoltTable[] tables = cr.getResults();
VoltTable table_a = tables[0];
while (table_a.advanceRow()) {
    System.out.println("Column 0 is " + table_a.getString(0));
}

Here is a shortened example:

VoltTable table_a = client.callProcedure(procname,parameters).getResults()[0];
while (table_a.advanceRow()) {
    System.out.println("Column 0 is " + table_a.getString(0));
}

Rather than procname and parameters, you could also call AdHoc SQL like this:

VoltTable table_a = client.callProcedure("@AdHoc","SELECT * FROM helloworld;").getResults()[0];

These examples above are synchronous or blocking calls. If you want your application to use asynchronous calls, you can also use a Callback object with the call, so the client would continue executing subsequent code. When the response is received by the client thread that handles callbacks, the result could be passed off to another thread in our application to read the results.

You can read more about the API in the Java Client API Javadoc.

0
On

If you want to use client.callProcedure function. You have to make that procedure in VoltDB's user interface . For example,

CREATE PROCEDURE insertNumber AS
INSERT INTO NUMBERS (number1) values (1)

this will create a procedure. When you call it with client.callProcedure(insertNumber), that will do the work.