如何使用java代码在cassandra中插入java列表值?

时间:2016-09-12 06:41:14

标签: java cassandra cqlsh

我的cassandra数据库结构:

unknown STANDARD CHARTERED B
nicholas@123.com DBS BANK LIMITED HON
WHEEL@123.com DBS BANK (HONG KONG)

我的代码:

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';

我收到错误,所以从print语句中获取查询:

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("fake1@fake1.com");
        list.add("fake2@fake2.com");

        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);
    }

然后我手动运行此查询到cql,我收到此错误:

  

SyntaxException:&lt; ErrorMessage code = 2000 [CQL查询中的语法错误] message =&#34;第1:70行在输入中没有可行的选择&#39;,&#39; (......价值观(&#39; 11101&#39;,&#39; john&#39;,[[AUE],...)&#34;&gt;

2 个答案:

答案 0 :(得分:2)

使用绑定标记:

List<String> list = new ArrayList<String>(); 
list.add("fake1@fake1.com"); 
list.add("fake2@fake2.com"); 
Statement st = new SimpleStatement("INSERT INTO USERS (user_id, name, rlist) VALUES ('11101', 'john', ?);", list); 
ResultSet rs = session.execute(st);

答案 1 :(得分:0)

由于您的列表由字符串组成,因此应该可以解决这个问题:

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