这是一个使用简单表格和文本值的示例。使用 Centos 6.5 和 DSE4.5.1
我的表:
DESCRIBE TABLE deleteme1 ;
CREATE TABLE deleteme1 (
col1 text,
col2 text,
PRIMARY KEY ((col1))
) WITH
bloom_filter_fp_chance=0.010000 AND
caching='KEYS_ONLY' AND
comment='' AND
dclocal_read_repair_chance=0.100000 AND
gc_grace_seconds=10000 AND
index_interval=128 AND
read_repair_chance=0.000000 AND
replicate_on_write='true' AND
populate_io_cache_on_flush='false' AND
default_time_to_live=0 AND
speculative_retry='99.0PERCENTILE' AND
memtable_flush_period_in_ms=0 AND
compaction={'class': 'SizeTieredCompactionStrategy'} AND
compression={'sstable_compression': 'LZ4Compressor'};
我的数据:
cqlsh:results> select * from deleteme1 ;
col1 | col2
-------+-------
three | three
one | one
two | two
four | four
(4 rows)
我的示例测试类
import java.util.ArrayList;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
public class PrepStatement {
Cluster cluster;
Session session;
public static void main(String args []){
PrepStatement myTest = new PrepStatement();
String node = "192.168.56.22";
myTest.runTest(node);
myTest.close();
}
private void runTest(String node){
ArrayList<String> vals = new ArrayList<String>(2);
vals.add("one");
vals.add("three");
try {
cluster = Cluster.builder()
.addContactPoint(node)
//.withCredentials("cassandra", "cassandra") // only if you need a login
.build();
session = cluster.connect();
PreparedStatement preparedStatement = session.prepare("SELECT * FROM results.deleteme1 WHERE col1 IN ?");
BoundStatement boundStatement = new BoundStatement(preparedStatement);
ResultSet results = session.execute(boundStatement.bind(vals));
for (Object result : results){
System.out.println(result.toString());
}
}
catch (Exception e){
e.printStackTrace();
}
}
/**
* cleans up the session
*/
private void close() {
session.close();
cluster.close();
}
}
最后..这是输出:
Row[one, one]
Row[three, three]
请注意,您只能在 CQL3.0 文档中概述的主键列上使用 IN 查询:http ://www.datastax.com/documentation/cql/3.0/cql/cql_reference/select_r.html