【发布时间】:2014-12-03 14:30:50
【问题描述】:
我需要使用 Datastax Java 驱动程序将批处理写入 Cassandra,这是我第一次尝试将批处理与 datastax Java 驱动程序一起使用,所以我有些困惑 -
下面是我的代码,我在其中尝试创建一个 Statement 对象并将其添加到 Batch 并将 ConsistencyLevel 设置为 QUORUM。
Session session = null;
Cluster cluster = null;
// we build cluster and session object here and we use DowngradingConsistencyRetryPolicy as well
// cluster = builder.withSocketOptions(socketOpts).withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
public void insertMetadata(List<AddressMetadata> listAddress) {
// what is the purpose of unloggedBatch here?
Batch batch = QueryBuilder.unloggedBatch();
try {
for (AddressMetadata data : listAddress) {
Statement insert = insertInto("test_table").values(
new String[] { "address", "name", "last_modified_date", "client_id" },
new Object[] { data.getAddress(), data.getName(), data.getLastModifiedDate(), 1 });
// is this the right way to set consistency level for Batch?
insert.setConsistencyLevel(ConsistencyLevel.QUORUM);
batch.add(insert);
}
// now execute the batch
session.execute(batch);
} catch (NoHostAvailableException e) {
// log an exception
} catch (QueryExecutionException e) {
// log an exception
} catch (QueryValidationException e) {
// log an exception
} catch (IllegalStateException e) {
// log an exception
} catch (Exception e) {
// log an exception
}
}
下面是我的AddressMetadata类-
public class AddressMetadata {
private String name;
private String address;
private Date lastModifiedDate;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getLastModifiedDate() {
return lastModifiedDate;
}
public void setLastModifiedDate(Date lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
}
现在我的问题是 - 我使用 Batch 通过 Datastax Java 驱动程序插入 cassandra 的方式是否正确?那么重试策略呢,也就是说如果批处理语句执行失败了,那会发生什么,会重试吗?
还有没有更好的方法来使用 java 驱动程序对 cassandra 进行批量写入?
【问题讨论】:
标签: java cassandra datastax-java-driver