API Libraries > Java

Java

This document describes how to use java client for Rockset. This client allows you to:

  • securely connect to Rockset service
  • create, manage and administer collections
  • issue queries against collections

The Java client library code is available here.

Refer here for the JavaDocs.

Installation

Add this Maven dependency to your project’s POM:

<dependency>
    <groupId>com.rockset</groupId>
    <artifactId>rockset-java</artifactId>
    <version>0.5.11</version>
</dependency>

or create a jar file rockset-java-${version}.jar

mvn package

Connect

Client objects allow you to connect securely to Rockset service. All other API calls require a valid client object.

In order to create a client object, you will need a valid Rockset API key. If you have access to the Rockset Console, then you can use the console to create an API key. If not, please contact the Rockset team at support@rockset.com

import com.rockset.client.RocksetClient;

RocksetClient client = new RocksetClient("<apiKey>", "<apiServer>");

Use https://api.rs2.usw2.rockset.com for the apiServer.

Usage

Create a collection

Create a collection using the client object as follows:

CreateCollectionRequest request = new CreateCollectionRequest()
                                    .name("my-first-collection");
CreateCollectionResponse response = client.createCollection("commons", request);

If your document source is S3, follow the steps below otherwise refer to Rock CLI to add documents to the created collection.

Create an Integration

If you have an Amazon S3 bucket that you want to ingest data from, create a Rockset Integration to store credentials required to access the bucket. Create an integration object using the client object as follows:

CreateIntegrationRequest request = new CreateIntegrationRequest()
                                    .name("my-first-integration")
                                    .aws(new AwsKeyIntegration()
                                        .awsAccessKeyId(".....")
                                        .awsSecretAccessKey("....."));
CreateIntegrationResponse response = client.createIntegration(request);

Create a collection from Amazon S3

Prior to creating a collection using Amazon S3 as source, create a Rockset Integration first as described above.

SourceS3 sourceS3 = new SourceS3();
sourceS3.setBucket("<bucket>");
List<Source> sources = new ArrayList<Source>();
sources.add(new Source().s3(sourceS3).integrationName("<integration>"));
CreateCollectionRequest request = new CreateCollectionRequest()
                                    .name("my-first-s3-collection")
                                    .sources(sources);
CreateCollectionResponse response = client.createCollection("commons", request);

Add Documents

Add documents to an existing collection using the client object.

// load json from a file or create a list of objects to be inserted
LinkedList<Object> list = new LinkedList<>();
Map<String, Object> json = new LinkedHashMap<>();
json.put("name", "foo");
json.put("address", "bar");
list.add(json);

AddDocumentsRequest documentsRequest =
     new AddDocumentsRequest().data(list);
AddDocumentsResponse documentsResponse =
     client.addDocuments("commons", "my-first-collection", documentsRequest);

Query

Make queries to Rockset using the client object.

QueryRequest request = new QueryRequest()
                            .sql(new QueryRequestSql()
                            .query("select * from \"_events\""));
QueryResponse response = rs.query(request);

You can find more examples here.