1

This is the code I have used for writing my program but there are errors - please give us some suggestions with the corrected code.

session.execute("INSERT INTO users JSON '{'id':'user123' , 'age':21 ,'state':'TX'}';");

The errors are directed to this one statement so I thought that its not necessary to present the whole code here.TABLE users has already been created in the cassandra database with the columns id, age and state. I could not find any proper answers for this problem anywhere, I hope my problem is solved here.

0

1 Answer 1

2

Here is the working query and below java code where I insert it and the results

"INSERT INTO users JSON '{\"id\":888 , \"age\":21 ,\"state\":\"TX\"}'";


import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class CasandarConnect {

public static void main(String[] args) {
    String serverIP = "127.0.0.1";
    String keyspace = "mykeyspace";

    Cluster cluster = Cluster.builder()
      .addContactPoints(serverIP)
      .build();

    Session session = cluster.connect(keyspace);

    String cqlStatement = "INSERT INTO users JSON '{\"id\":888 , \"age\":21 ,\"state\":\"TX\"}'";
    session.execute(cqlStatement);
   }

}

Result

cqlsh:mykeyspace> select * from users;

 id   | age | state
------+-----+-------
 1745 |  12 | smith
  123 |  21 |    TX
  888 |  21 |    TX
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.