Use the Cassandra CQL Java driver to connect to LindormTable and perform DDL and DML operations.
Prerequisites
-
Java Development Kit (JDK) 1.8 or later is installed.
-
The Cassandra CQL Java driver is installed. Install the Cassandra CQL Driver.
-
Your client IP address is added to the Lindorm instance whitelist. Configure a whitelist.
-
You have obtained the CQL Connection endpoint. In the Lindorm console, go to instance details > Endpoints > LindormTable tab > Cassandra-compatible address to get the private or public address (default username and password:
root). View endpoints.
Procedure
-
Configure the connection parameters.
String[] contactPoints = new String[]{ "<host>" }; ClusterNote-
host: The CQL Connection address from the Lindorm console. Example:
ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com. -
username: The Lindorm instance account name. Default: root.
-
password: The Lindorm instance account password. If forgotten, reset it in the Cluster Management System of LindormTable. Change user password.
-
-
Use the Cassandra CQL Java API to access Lindorm wide tables.
-
DDL operations
// Create a keyspace and specify its strategy and replication factor. session.execute( "CREATE KEYSPACE IF NOT EXISTS testKeyspace WITH replication " + "= {'class':'SimpleStrategy', 'replication_factor':1};"); // Create a table and specify its primary key, cluster key, and regular key. session.execute( "CREATE TABLE IF NOT EXISTS testKeyspace.testTable (" + "id int PRIMARY KEY," + "name text," + "age int," + "address text" + ");"); // Truncate the table. session.execute("TRUNCATE TABLE testKeyspace.testTable;"); // Drop the table. session.execute("DROP TABLE testKeyspace.testTable "); -
DML operations
// Perform an insert operation. session.execute( "INSERT INTO testKeyspace.testTable (id, name, age, address) " + "VALUES (" + "1," + "'testname'," + "11," + "'hangzhou');"); // Perform a select operation. `select *` retrieves all columns. To retrieve data from specific columns, specify their names. ResultSet res = session.execute( "SELECT * FROM testKeyspace.testTable ;"); // To retrieve data from each column: for (Row row : res) { int id = row.getInt("id"); String name = row.getString("name"); int age = row.getInt("age"); String address = row.getString("address"); } // Close the session. session.close(); // Close the cluster. cluster.close();
-