For a more detailed documentation how to set up and use the Crate client please refer to the client documentation on `https://crate.io/docs/`_.
The client module exposes a very simple interface to query Crate using SQL.
The easiest way to use the crate client is to include it as a dependency using Maven:
<dependency>
<groupId>io.crate</groupId>
<artifactId>crate-client</artifactId>
<version>...</version>
</dependency>You can find the latest version of the client on Bintray.
See :ref:`Development <client_develop>` for further details.
A minimal example is just a few lines of code:
import io.crate.client.CrateClient;
CrateClient client = new CrateClient("server1.crate.org:4300", "server2.crate.org:4300");
SQLResponse r = client.sql("select firstName, lastName from users").actionGet();
System.out.println(Arrays.toString(r.cols()));
// outputs ["firstName", "lastName"]
for (Object[] row: r.rows()){
System.out.println(Arrays.toString(row));
}
// outputs the users. For example:
// ["Arthur", "Dent"]
// ["Ford", "Perfect"]The CrateClient takes multiple servers as arguments. They are used in a round-robin fashion to distribute the load. In case a server is unavailable it will be skipped.
By default, the column data types are not serialized. In order to get these, one must defined it at the SQLRequest object:
import io.crate.client.CrateClient;
CrateClient client = new CrateClient("server1.crate.org:4300", "server2.crate.org:4300");
SQLRequest request = new SQLRequest("select firstName, lastName from users");
request.includeTypesOnResponse(true);
SQLResponse r = client.sql().actionGet();
// Get the data type of the first column
DataType dataType = r.columnTypes()[0]
System.out.print(dataType.getName())
// outputs: "string"Note
Queries are executed asynchronous. client.sql("") will return a Future<SQLResponse> and code execution is only blocked if .actionGet() is called on it.