Java connecting
Authentication
This example assumes that you’re running Stargate locally with the default credentials
of cassandra/cassandra
.
For more information regarding authentication please see the
Stargate authentication and authorization docs.
The following code snippet can be used to generate the table-based auth token in the client code:
// Stargate OSS configuration for locally hosted docker image
private static final String STARGATE_USERNAME = "cassandra";
private static final String STARGATE_PASSWORD = "cassandra";
private static final String STARGATE_HOST = "localhost";
private static final int STARGATE_GRPC_PORT = 8090;
private static final String STARGATE_AUTH_ENDPOINT = "http://" + STARGATE_HOST+ ":8081/v1/auth";
// Authenticate to get a token (http client jdk11)
String token = getTokenFromAuthEndpoint(STARGATE_USERNAME, STARGATE_PASSWORD);
private static String getTokenFromAuthEndpoint(String username, String password) {
try {
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(STARGATE_AUTH_ENDPOINT))
.setHeader("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{"
+ " \"username\": \"" + STARGATE_USERNAME+ "\",\n"
+ " \"password\": \"" + STARGATE_PASSWORD + "\"\n"
+ "}'"))
.build();
HttpResponse<String> response = HttpClient.newBuilder().build()
.send(request, HttpResponse.BodyHandlers.ofString());
return response.body().split(":")[1].replaceAll("\"", "").replaceAll("}", "");
} catch(Exception e) {
throw new IllegalArgumentException(e);
}
}
Creating a client
To connect to your Stargate instance, create the client. For a local Stargate instance, for instance, the following client code will fetch an auth token with a REST call:
// Stargate OSS configuration for locally hosted docker image
private static final String STARGATE_USERNAME = "cassandra";
private static final String STARGATE_PASSWORD = "cassandra";
private static final String STARGATE_HOST = "localhost";
private static final int STARGATE_GRPC_PORT = 8090;
private static final String STARGATE_AUTH_ENDPOINT = "http://" + STARGATE_HOST+ ":8081/v1/auth";
public static void main(String[] args)
throws Exception {
// Create Grpc Channel
ManagedChannel channel = ManagedChannelBuilder
.forAddress(STARGATE_HOST, STARGATE_GRPC_PORT).usePlaintext().build();
// blocking stub version
StargateGrpc.StargateBlockingStub blockingStub =
StargateGrpc.newBlockingStub(channel)
.withDeadlineAfter(10, TimeUnit.SECONDS)
.withCallCredentials(new StargateBearerToken(token));