All Products
Search
Document Center

Lindorm:Use a Cassandra client driver for Java to develop applications

Last Updated:Aug 21, 2023

This topic describes how to use a Cassandra client driver for Java to connect to and use LindormTable over Cassandra Query Language (CQL).

Prerequisites

  • Java Development Kit (JDK) V1.8 or later is installed.

  • A Cassandra client driver for Java is installed over CQL. For more information, see Install a Cassandra client driver.

  • The IP address of your client is added to the whitelist of the Lindorm instance. For more information, see Configure whitelists.

  • The LindormTable endpoint for Cassandra SQL is obtained. For more information, see View endpoints.获取连接地址

Procedure

  1. Configure connection parameters.

     String[] contactPoints = new String[]{
          "<host>"
     };
      
     Cluster cluster = Cluster.builder()
          .addContactPoints(contactPoints)      
          .withAuthProvider(new PlainTextAuthProvider(username, password))
          .build();
     cluster.init();
     Session session = cluster.connect();
    Note
    • Replace <host> with the LindormTable endpoint for Cassandra SQL that is obtained on the Lindorm console. Example: ld-bp17j28j2y7pm****-proxy-lindorm.lindorm.rds.aliyuncs.com.

    • Replace username with the username that can be used to connect to the Lindorm instance. The default username is root.

    • Replace password with the password that can be used to connect to the Lindorm instance. If you forget the password, you can go to the cluster management system of LindormTable to modify the password. For more information, see Change the password of a user.

  2. Use the API operations of the Cassandra client driver for Java to access a Lindorm wide table over CQL. The following code snippets provide examples on how to access a Lindorm wide table:

    • DDL operations

      // Create a keyspace. Configure a replication strategy and a replication factor for the keyspace. 
          session.execute(
                      "CREATE KEYSPACE IF NOT EXISTS testKeyspace WITH replication "
                              + "= {'class':'SimpleStrategy', 'replication_factor':1};");
      
       // Create a table and specify a primary key, a cluster key, and a regular key for the table.
          session.execute(
                      "CREATE TABLE IF NOT EXISTS testKeyspace.testTable ("
                              + "id int PRIMARY KEY,"
                              + "name text,"
                              + "age int,"
                              + "address text"
                              + ");");    
      
       // Clear the table.
          session.execute("TRUNCATE TABLE testKeyspace.testTable;");
       // Delete the table.
          session.execute("DROP TABLE testKeyspace.testTable ");
    • DML operations

      // Execute the INSERT statement.
          session.execute(
                      "INSERT INTO testKeyspace.testTable (id, name, age, address) "
                              + "VALUES ("
                              + "1,"
                              + "'testname',"
                              + "11,"
                              + "'hangzhou');");
          // Execute the SELECT statement. If you want to query all columns in a table, use SELECT *. You can also specify the columns that you want to query.
          ResultSet res = session.execute(
                      "SELECT * FROM testKeyspace.testTable ;");
      
          // If you want to query data of each column, execute the following statements:
          for (Row row : results)
          {
              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();
          // Shut down the cluster.
          cluster.close();