很好的解释如何在 Cassandra 中存储带有复合键的行在这里。
在 Astyanax 和 Hector 中,我注意到有趣的事情 - 当一个尝试连接时 - 它使用了 CQL2。当我使用 cassandra api(下面的示例代码)通过 CQL3 连接到 Cassandra 时,此设置存储在某处,之后 Astyanax 和 Hector 使用 cql3 而不是 CQL2。连接是作为单独的执行进行的,所以它不能存储在客户端......有人对此有任何想法吗?
可以使用 set_cql_version 方法在 org.apache.cassandra.thrift.Cassandra.Client 上设置 CQL 版本。
如果有人对使用纯 Cassandra api 的工作示例感兴趣:
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.List;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.Compression;
import org.apache.cassandra.thrift.CqlResult;
import org.apache.cassandra.thrift.CqlRow;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.SchemaDisagreementException;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class KKVVGetter {
private static Cassandra.Client client;
private static TTransport transport;
public static void main(String[] args) throws UnsupportedEncodingException, InvalidRequestException,
UnavailableException, TimedOutException, SchemaDisagreementException, TException {
transport = new TFramedTransport(new TSocket("localhost", 9160));
TProtocol protocol = new TBinaryProtocol(transport);
client = new Cassandra.Client(protocol);
transport.open();
client.set_cql_version("3.0.0");
executeQuery("USE ks_test3");
show("select x,y,val1,val2 from kkvv where x > 1 and x < 11 and y < 100 and y > 2");
System.out.println("\n\n*****************************\n\n");
show("select x,y,val1,val2 from kkvv");
transport.close();
}
private static int toInt(byte[] bytes) {
int result = 0;
for (int i = 0; i < 4; i++) {
result = (result << 4) + (int) bytes[i];
}
return result;
}
private static CqlResult executeQuery(String query) throws UnsupportedEncodingException, InvalidRequestException,
UnavailableException, TimedOutException, SchemaDisagreementException, TException {
return client.execute_cql_query(ByteBuffer.wrap(query.getBytes("UTF-8")), Compression.NONE);
}
private static void show(String query) throws UnsupportedEncodingException, InvalidRequestException,
UnavailableException, TimedOutException, SchemaDisagreementException, TException {
CqlResult result = executeQuery(query);
List<CqlRow> rows = result.getRows();
System.out.println("rows: " + rows.size());
for (CqlRow row : rows) {
System.out.println("columns: " + row.getColumnsSize());
for (Column c : row.getColumns()) {
System.out.print(" " + new String(c.getName()));
switch (new String(c.getName())) {
case "x":
case "y":
System.out.print(" " + toInt(c.getValue()));
break;
case "val1":
case "val2":
System.out.print(" " + new String(c.getValue()));
break;
default:
break;
}
System.out.println();
}
}
}
}
有问题的架构示例。