VoltDB stored procedures are written in Java and the primary client interface also uses Java. However, that is not the only programming language you can use with VoltDB.
It is possible to have client interfaces written in almost any language. These client interfaces allow programs written in different programming languages to interact with a VoltDB database using native functions of the language. The client interface then takes responsibility for translating those requests into a standard communication protocol with the database server as described in the VoltDB wire protocol.
Some client interfaces are developed and packaged as part of the standard VoltDB distribution kit while others are compiled and distributed as separate client kits. As of this writing, the following client interfaces are available for VoltDB:
C#
C++
Erlang
Go
Java (packaged with VoltDB)
JDBC (packaged with VoltDB)
JSON (packaged with VoltDB)
Node.js
PHP
Python
The JSON client interface may be of particular interest if your favorite programming language is not listed above. JSON is a data format, rather than a programming interface, and the JSON interface provides a way for applications written in any programming language to interact with VoltDB via JSON messages sent across a standard HTTP protocol.
The following sections explain how to use the C++, JSON, and JDBC client interfaces.
VoltDB provides a client interface for programs written in C++. The C++ client interface is available pre-compiled as a
separate kit from the VoltDB web site, or in source format from the VoltDB github
repository (http://github.com/VoltDB/voltdb-client-cpp
). The following
sections describe how to write VoltDB client applications in C++.
When using the VoltDB client library, as with any C++ library, it is important to include all of the necessary definitions at the beginning of your source code. For VoltDB client applications, this includes definitions for the VoltDB methods, structures, and datatypes as well as the libraries that VoltDB depends on (specifically, boost shared pointers). For example:
#define __STDC_CONSTANT_MACROS #define __STDC_LIMIT_MACROS #include <vector> #include <boost/shared_ptr.hpp> #include "Client.h" #include "Table.h" #include "TableIterator.h" #include "Row.hpp" #include "WireType.h" #include "Parameter.hpp" #include "ParameterSet.hpp" #include "ProcedureCallback.hpp"
Once you have included all of the necessary declarations, there are three steps to using the interface to interact with VoltDB:
Create and open a client connection
Invoke stored procedures
Interpret the results
The following sections explain how to perform each of these functions.
Before you can call VoltDB stored procedures, you must create a client instance and connect to the database cluster. For example:
voltdb::ClientConfig config("myusername", "mypassword"); voltdb::Client client = voltdb::Client::create(config); client.createConnection("myserver");
As with the Java client interface, you can create connections to multiple nodes in the cluster by making multiple calls to the createConnection method specifying a different IP address for each connection.
The C++ client library provides both a synchronous and asynchronous interface. To make a synchronous stored procedure call, you must declare objects for the parameter types, the procedure call itself, the parameters, and the response. Note that the datatypes, the procedure, and the parameters need to be declared in a specific order. For example:
/* Declare the number and type of parameters */ std::vector<voltdb::Parameter> parameterTypes(3); parameterTypes[0] = voltdb::Parameter(voltdb::WIRE_TYPE_BIGINT); parameterTypes[1] = voltdb::Parameter(voltdb::WIRE_TYPE_STRING); parameterTypes[2] = voltdb::Parameter(voltdb::WIRE_TYPE_STRING); /* Declare the procedure and parameter structures */ voltdb::Procedure procedure("AddCustomer", parameterTypes); voltdb::ParameterSet* params = procedure.params(); /* Declare a client response to receive the status and return values */ voltdb::InvocationResponse response;
Once you instantiate these objects, you can reuse them for multiple calls to the stored procedure, inserting different values into params each time. For example:
params->addInt64(13505).addString("William").addString("Smith"); response = client.invoke(procedure); params->addInt64(13506).addString("Mary").addString("Williams"); response = client.invoke(procedure); params->addInt64(13507).addString("Bill").addString("Smythe"); response = client.invoke(procedure);
To make asynchronous procedure calls, you must also declare a callback structure and method that will be used when the procedure call completes.
class AsyncCallback : public voltdb::ProcedureCallback { public: bool callback (voltdb::InvocationResponse response) throw (voltdb::Exception) { /* * The work of your callback goes here... */ } };
Then, when you go to make the actual stored procedure invocation, you declare an callback instance and invoke the procedure, using both the procedure structure and the callback instance:
boost::shared_ptr<AsyncCallback> callback(new AsyncCallback()); client.invoke(procedure, callback);
Note that the C++ interface is single-threaded. The interface is not thread-safe and you should not use instances of the client, client response, or other client interface structures from within multiple concurrent threads. Also, the application must release control occasionally to give the client interface an opportunity to issue network requests and retrieve responses. You can do this by calling either the run() or runOnce() methods.
The run() method waits for and processes network requests, responses, and callbacks until told not to. (That is, until a callback returns a value of false.)
The runOnce() method processes any outstanding work and then returns control to the client application.
In most applications, you will want to create a loop that makes asynchronous requests and then calls runOnce(). This allows the application to queue stored procedure requests as quickly as possible while also processing any incoming responses in a timely manner.
Another important difference when making stored procedure calls asynchronously is that you must make sure all of the procedure calls complete before the client connection is closed. The client objects destructor automatically closes the connection when your application leaves the context or scope within which the client is defined. Therefore, to make sure all asynchronous calls have completed, be sure to call the drain method until it returns true before leaving your client context:
while (!client.drain()) {}
Both the synchronous and asynchronous invocations return a client response object that contains both the status of the call and the return values. You can use the status information to report problems encountered while running the stored procedure. For example:
if (response.failure()) { std::cout << "Stored procedure failed. " << response.toString(); exit(-1); }
If the stored procedure is successful, you can use the client response to retrieve the results. The results are returned as an array of VoltTable structures. Within each VoltTable object you can use an iterator to walk through the rows. There are also methods for retrieving each datatype from the row. For example, the following example displays the results of a single VoltTable containing two strings in each row:
/* Retrieve the results and an iterator for the first volttable */ std::vector<voltdb::Table> results = response.results(); voltdb::TableIterator iterator = results[0].iterator(); /* Iterate through the rows */ while (iterator.hasNext()) { voltdb::Row row = iterator.next(); std::cout << row.getString(0) << ", " << row.getString(1) << std::endl; }