2

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.

Lateral
  • 41
  • 6

2 Answers2

1

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.

BenjaminBallard
  • 1,482
  • 12
  • 11
  • Yes, I was looking for "@AdHoc." Thanks. However, the type I'm actually trying to get is VoltTable[], not VoltTable- it should be able to handle inputs of multiple VoltTables. (It also needs a callback, but I don't think that affects this.) Either way, it's giving me an error. 'Cannot resolve method getResults.' Is there an import I'm missing? – Lateral Jul 31 '14 at 15:29
  • Sorry for not seeing this sooner. The getResults() is in the ClientResponse class. You need to "import org.voltdb.client.ClientResponse;". The first example above shows getting a VoltTable[], the second and third examples just skip getting the array as an object and instead get the first element of the array. This is just a shortcut for the common case where there is only going to be one VoltTable in the array. – BenjaminBallard Aug 13 '14 at 03:10
0

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.