1

my cassandra DB structure:

CREATE TABLE devjavasource.userorders (
user_id text PRIMARY KEY,
name text,
rlist list<text>
) WITH bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';

my code:

import com.datastax.driver.core.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class cassandraList
{

    public static void main(String[] args) {
        Cluster cluster = null;
        Session session = null;
        cluster = cluster.builder().addContactPoint("192.168.1.23").build();
        List<String> list= new ArrayList<String>();
        list.add("[email protected]");
        list.add("[email protected]");

        session = cluster.connect("devjavasource");

        System.out.println("test :" +list);

        Statement st = new SimpleStatement("INSERT INTO USERS (user_id, name, Rlist) VALUES ('11101', 'john', "+list+");");

        System.out.println("query :" +"INSERT INTO USERS (user_id, name, Rlist) VALUES ('11101', 'john', "+routing_points+");");
        ResultSet rs = session.execute(st);
    }

I got a error, so get query from print statement like this:

  INSERT INTO USERS (user_id, name, Rlist) VALUES ('11101', 'john', [AUE, SAN, UK, LON]);

Then I run this query to cql manually, I got this error:

SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:70 no viable alternative at input ',' (...VALUES ('11101', 'john', [[AUE],...)">

2 Answers 2

2

Use bind markers:

List<String> list = new ArrayList<String>(); 
list.add("[email protected]"); 
list.add("[email protected]"); 
Statement st = new SimpleStatement("INSERT INTO USERS (user_id, name, rlist) VALUES ('11101', 'john', ?);", list); 
ResultSet rs = session.execute(st);
Sign up to request clarification or add additional context in comments.

Comments

0

Since your list is comprised of strings, this should do the trick:

INSERT INTO USERS (user_id, name, Rlist) VALUES ('11101', 'john', ['AUE', 'SAN', 'UK', 'LON']);

6 Comments

In java how to create the list of string that have single quotes dynamically
I got java List with out single quotes ,how convert this?
I would need more information than that. Are you preparing you query? If you could post the code of how you are trying to insert, I could take a look.
List<String> list= new ArrayList<String>(); list.add("[email protected]"); list.add("[email protected]"); Statement st = new SimpleStatement("INSERT INTO USERS (user_id, name, Rlist) VALUES ('11101', 'john', "+list+");"); ResultSet rs = session.execute(st);
Try using prepared statements instead. They might handle this for you automatically.
|

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.