Reputation: 11
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],...)">
Upvotes: 1
Views: 4492
Reputation: 4536
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);
Upvotes: 2
Reputation: 815
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']);
Upvotes: 0