|
| 1 | +# Integrating cqlsh with Spanner Cassandra Java Client |
| 2 | + |
| 3 | +This guide provides detailed instructions on setting up `cqlsh` to connect to the Spanner Cassandra Java Client. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- **cqlsh Version:** Ensure you have `cqlsh` versions from the branches `cassandra-4.0.13` or `cassandra-4.1.5`. |
| 8 | +- **Spanner Cassandra Java Client Setup:** Ensure the Spanner Cassandra Java Client is set up and running. |
| 9 | + |
| 10 | +## Setup Instructions |
| 11 | + |
| 12 | +>> NOTE: You may skip these instructions, if you already have `cqlsh` interface installed. |
| 13 | +
|
| 14 | +### Option 1: Install cqlsh Directly |
| 15 | + |
| 16 | +#### Step 1: Download cqlsh |
| 17 | + |
| 18 | +Download the appropriate version of `cqlsh` by cloning the repository and following the instructions: |
| 19 | + |
| 20 | +- **cqlsh 4.0.13:** |
| 21 | + ```sh |
| 22 | + git clone https://github.com/apache/cassandra.git -b cassandra-4.0.13 |
| 23 | + cd cassandra/bin |
| 24 | + ``` |
| 25 | + |
| 26 | +- **cqlsh 4.1.5:** |
| 27 | + ```sh |
| 28 | + git clone https://github.com/apache/cassandra.git -b cassandra-4.1.5 |
| 29 | + cd cassandra/bin |
| 30 | + ``` |
| 31 | + |
| 32 | +#### Step 2: Configure cqlsh to Connect to the Spanner Cassandra Java Client (Optional) |
| 33 | + |
| 34 | +Edit the `cqlsh` configuration to point to the Spanner Cassandra Java Client: |
| 35 | + |
| 36 | +1. Open the `cqlshrc` configuration file. If it does not exist, create one in your home directory: |
| 37 | + ```sh |
| 38 | + nano ~/.cassandra/cqlshrc |
| 39 | + ``` |
| 40 | + |
| 41 | +2. Add the following configuration: |
| 42 | + ```ini |
| 43 | + [connection] |
| 44 | + hostname = <java_client_hostname> |
| 45 | + port = <java_client_port> |
| 46 | + ``` |
| 47 | + |
| 48 | + Replace `<java_client_hostname>` and `<java_client_port>` with the appropriate values for your java client setup. |
| 49 | + |
| 50 | +#### Step 3: Launch cqlsh |
| 51 | + |
| 52 | +Launch `cqlsh` with the configured/default settings: |
| 53 | +```sh |
| 54 | +./cqlsh --protocol-version 4 |
| 55 | +``` |
| 56 | + |
| 57 | +Launch `cqlsh` with the custom hostname and port: |
| 58 | +```sh |
| 59 | +./cqlsh <java_client_hostname> <java_client_port> --protocol-version 4 |
| 60 | +``` |
| 61 | + |
| 62 | +Replace `<java_client_hostname>` and `<java_client_port>` with the appropriate values. |
| 63 | + |
| 64 | +### Option 2: Use Dockerized cqlsh |
| 65 | + |
| 66 | +#### Step 1: Install Docker |
| 67 | + |
| 68 | +Ensure Docker is installed on your machine. Follow the instructions on the [Docker website](https://docs.docker.com/get-docker/) to install Docker for your operating system. |
| 69 | + |
| 70 | +#### Step 2: Download and Run the Dockerized cqlsh |
| 71 | + |
| 72 | +Download the relevant Docker image and open a bash shell: |
| 73 | +```sh |
| 74 | +docker run -it nuvo/docker-cqlsh bash |
| 75 | +``` |
| 76 | + |
| 77 | +#### Step 3: Find Your Machine’s IP Address |
| 78 | + |
| 79 | +Find the local IP address of the machine if the java client is running locally. For macOS, you can get the local machine IP address using: |
| 80 | +```sh |
| 81 | +ifconfig | grep "inet " | grep -v 127.0.0.1 |
| 82 | +``` |
| 83 | + |
| 84 | +#### Step 4: Connect to the Spanner Cassandra Java Client |
| 85 | + |
| 86 | +Open a bash shell in the Docker image: |
| 87 | +```sh |
| 88 | +docker run -it nuvo/docker-cqlsh bash |
| 89 | +``` |
| 90 | + |
| 91 | +Connect to the Spanner Cassandra Java Client using your IP address and port: |
| 92 | +```sh |
| 93 | +cqlsh --protocol-version 4 '<your_ip_address>' <port> |
| 94 | +``` |
| 95 | + |
| 96 | +Replace `<your_ip_address>` with the local IP address and `<port>` obtained in Step 3. |
| 97 | + |
| 98 | +### Option 3: Use pip |
| 99 | + |
| 100 | +To download and install `cqlsh` using `pip`, the Python package installer, one can use the following command: |
| 101 | + |
| 102 | +```sh |
| 103 | +pip install cqlsh |
| 104 | +``` |
| 105 | + |
| 106 | +## Basic CRUD Operations |
| 107 | + |
| 108 | +**Insert:** |
| 109 | +```sql |
| 110 | +INSERT INTO keyspace_name.table_name (col1, col2, time, count) VALUES ('1234', 'check', '2024-06-13T05:19:16.882Z', 10); |
| 111 | +``` |
| 112 | + |
| 113 | +**Select:** |
| 114 | +```sql |
| 115 | +SELECT * FROM keyspace_name.table_name WHERE col1 = '1234'; |
| 116 | +``` |
| 117 | + |
| 118 | +**Update:** |
| 119 | +```sql |
| 120 | +UPDATE keyspace_name.table_name SET count = 15 WHERE col1 = '1234' AND col2 = 'check' AND time = '2024-06-13T05:19:16.882Z'; |
| 121 | +``` |
| 122 | + |
| 123 | +**Delete:** |
| 124 | +```sql |
| 125 | +DELETE FROM keyspace_name.table_name WHERE col1 = '1234' AND col2 = 'check'; |
| 126 | +``` |
| 127 | + |
| 128 | +## Unsupported Queries |
| 129 | + |
| 130 | +DDL queries are not supported by the Spanner Cassandra Java Client when using `cqlsh`: |
| 131 | + |
| 132 | +- **Create Table:** |
| 133 | + ```sql |
| 134 | + CREATE TABLE keyspace_name.table_name (id UUID PRIMARY KEY, name text); |
| 135 | + ``` |
| 136 | + |
| 137 | +- **Drop Table:** |
| 138 | + ```sql |
| 139 | + DROP TABLE keyspace_name.table_name; |
| 140 | + ``` |
| 141 | + |
| 142 | +- **Describe Table:** |
| 143 | + ```sql |
| 144 | + DESCRIBE TABLE keyspace_name.table_name; |
| 145 | + ``` |
| 146 | + |
0 commit comments