很好的解釋如何在Cassandra中存儲具有複合鍵的行是here。
在Astyanax和Hector中,我注意到有趣的事情 - 當試圖連接時 - 它使用CQL2。當我用Cassandra API(來自示例波紋管的代碼)通過CQL3連接到Cassandra時,某處存儲了此設置,此後Astyanax和Hector使用cql3而不是CQL2。連接是作爲單獨的執行,因此它不能存儲在客戶端...有人對此有任何想法?
可以使用set_cql_version方法在org.apache.cassandra.thrift.Cassandra.Client上設置CQL版本。
如果有人有興趣使用純卡桑德拉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();
}
}
}
}
示例有問題的模式。
來源
2012-06-26 16:24:05
trj
我不知道如何以及如果我能夠使用散裝加載程序可寫的組合鍵爲hadoop提供! – jonbros
我查過了,就像你說的。另外,可以使用Astyanax或Hector,但您必須使用cql3來執行源代碼。 – trj