|
| 1 | +## Connecting to ScyllaDB Cloud Serverless |
| 2 | + |
| 3 | +With ScyllaDB Cloud, you can deploy [serverless databases](https://cloud.docs.scylladb.com/stable/serverless/index.html). |
| 4 | +The Java driver allows you to connect to a serverless database by utilizing the connection bundle you can download via the **Connect>Java** tab in the Cloud application. |
| 5 | +The connection bundle is a YAML file with connection and credential information for your cluster. |
| 6 | + |
| 7 | +Connecting to a ScyllaDB Cloud serverless database is very similar to a standard connection to a ScyllaDB database. |
| 8 | + |
| 9 | +Here’s a short program that connects to a ScyllaDB Cloud serverless database and executes a query: |
| 10 | + |
| 11 | +```java |
| 12 | +Cluster cluster = null; |
| 13 | +try { |
| 14 | + File bundleFile = new File("/file/downloaded/from/cloud/connect-bundle.yaml"); |
| 15 | + |
| 16 | + cluster = Cluster.builder() // (1) |
| 17 | + .withScyllaCloudConnectionConfig(bundleFile) // (2) |
| 18 | + .build(); |
| 19 | + Session session = cluster.connect(); // (3) |
| 20 | + |
| 21 | + ResultSet rs = session.execute("select release_version from system.local"); // (4) |
| 22 | + Row row = rs.one(); |
| 23 | + System.out.println(row.getString("release_version")); // (5) |
| 24 | +} finally { |
| 25 | + if (cluster != null) cluster.close(); // (6) |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +1. The [Cluster] object is the main entry point of the driver. It holds the known state of the actual ScyllaDB cluster |
| 30 | + (notably the [Metadata](metadata/)). This class is thread-safe, you should create a single instance (per target |
| 31 | + ScyllaDB cluster), and share it throughout your application; |
| 32 | +2. [withScyllaCloudConnectionConfig] is a method that configures the cluster endpoints and credentials |
| 33 | + to your ScyllaDB Cloud serverless cluster based on the YAML connection bundle you downloaded from ScyllaDB Cloud; |
| 34 | +3. The [Session] is what you use to execute queries. Likewise, it is thread-safe and should be reused; |
| 35 | +4. We use `execute` to send a query to Cassandra. This returns a [ResultSet], which is essentially a collection of [Row] |
| 36 | + objects. On the next line, we extract the first row (which is the only one in this case); |
| 37 | +5. We extract the value of the first (and only) column from the row; |
| 38 | +6. Finally, we close the cluster after we're done with it. This will also close any session that was created from this |
| 39 | + cluster. This step is important because it frees underlying resources (TCP connections, thread pools...). In a real |
| 40 | + application, you would typically do this at shutdown (for example, when undeploying your webapp). |
| 41 | + |
| 42 | +[withScyllaCloudConnectionConfig]: https://java-driver.docs.scylladb.com/scylla-3.11.2.x/api/com/datastax/driver/core/Cluster.Builder.html#withScyllaCloudConnectionConfig-java.io.File- |
| 43 | +[Cluster]: https://docs.datastax.com/en/drivers/java/3.11/com/datastax/driver/core/Cluster.html |
| 44 | +[Session]: https://docs.datastax.com/en/drivers/java/3.11/com/datastax/driver/core/Session.html |
| 45 | +[ResultSet]: https://docs.datastax.com/en/drivers/java/3.11/com/datastax/driver/core/ResultSet.html |
| 46 | +[Row]: https://docs.datastax.com/en/drivers/java/3.11/com/datastax/driver/core/Row.html |
0 commit comments