我打算使用Datastax Java驅動程序寫入Cassandra ..我主要感興趣的是Batch Writes
和Asycnhronous
Datastax java驅動程序的功能,但我無法獲得任何教程可以解釋我如何將在使用Datastax Java驅動程序我下面的代碼,這些功能..如何使用Datastax Java驅動程序的異步/批量寫入功能
/**
* Performs an upsert of the specified attributes for the specified id.
*/
public void upsertAttributes(final String userId, final Map<String, String> attributes, final String columnFamily) {
try {
// make a sql here using the above input parameters.
String sql = sqlPart1.toString()+sqlPart2.toString();
DatastaxConnection.getInstance();
PreparedStatement prepStatement = DatastaxConnection.getSession().prepare(sql);
prepStatement.setConsistencyLevel(ConsistencyLevel.ONE);
BoundStatement query = prepStatement.bind(userId, attributes.values().toArray(new Object[attributes.size()]));
DatastaxConnection.getSession().execute(query);
} catch (InvalidQueryException e) {
LOG.error("Invalid Query Exception in DatastaxClient::upsertAttributes "+e);
} catch (Exception e) {
LOG.error("Exception in DatastaxClient::upsertAttributes "+e);
}
}
在下面的代碼中,我創建一個連接到卡桑德拉使用Datastax Java驅動程序節點。
/**
* Creating Cassandra connection using Datastax Java driver
*
*/
private DatastaxConnection() {
try{
builder = Cluster.builder();
builder.addContactPoint("some_nodes");
builder.poolingOptions().setCoreConnectionsPerHost(
HostDistance.LOCAL,
builder.poolingOptions().getMaxConnectionsPerHost(HostDistance.LOCAL));
cluster = builder
.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
.withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
.build();
StringBuilder s = new StringBuilder();
Set<Host> allHosts = cluster.getMetadata().getAllHosts();
for (Host h : allHosts) {
s.append("[");
s.append(h.getDatacenter());
s.append(h.getRack());
s.append(h.getAddress());
s.append("]");
}
System.out.println("Cassandra Cluster: " + s.toString());
session = cluster.connect("testdatastaxks");
} catch (NoHostAvailableException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (Exception e) {
}
}
任何人可以幫助我如何批量寫入或異步功能添加到我上面的代碼..感謝您的幫助..
我正在卡桑德拉1.2.9
是否有辦法用命名參數執行此操作? – Highstead
@Highstead什麼編程語言?上面是java所以([排序](http://java.dzone.com/articles/named-parameters-java)) –
我專注於python,但我認爲是否有辦法在一個另一方面會有辦法做到這一點。舊的cql驅動程序支持它,但已被棄用。所以我正在尋找替代功能。 – Highstead