Java 对Cassandra的批处理查询

Java 对Cassandra的批处理查询,java,cassandra,batch-processing,datastax,Java,Cassandra,Batch Processing,Datastax,我正在尝试将一批对象插入Cassandra,如下所示: public void insertIntoCassandra(ArrayList<FileLoaderVo> LoadVo) throws NitroLoaderException { int temp = LoadVo.size(); try { Session session = cassandraDAO.getSession(); if (session

我正在尝试将一批对象插入Cassandra,如下所示:

public void insertIntoCassandra(ArrayList<FileLoaderVo> LoadVo)
        throws NitroLoaderException {

    int temp = LoadVo.size();

    try {
        Session session = cassandraDAO.getSession();
        if (session == null) {
            String msg = "CassandraDAO.getSession() returned null";
            logger.error(msg);
            throw new FileLoaderException(msg);
        }

        BoundStatement bStmtHistTable = null;

        if (pStmtHistTable == null) {
            pStmtHistTable = session.prepare(insertToCassandra);
            pStmtHistTable.setConsistencyLevel(ConsistencyLevel.ONE);
        }

        for (FileLoaderVo fileLoaderVo : LoadVo) {              

            bStmtHistTable = pStmtHistTable.bind(fileLoaderVo.getSecurityCode(),
                    fileLoaderVo.getType(), fileLoaderVo.getAreaCode(),
                    fileLoaderVo.getEmpName(), fileLoaderVo.getCityType(),
                    fileLoaderVo.getHomeFIPS(), fileLoaderVo.getLastName(),
                    fileLoaderVo.getDst(), fileLoaderVo.getCssCode(),
                    fileLoaderVo.getAbbr(), fileLoaderVo.getOfficeFIPS(),
                    fileLoaderVo.getMiddleName(), fileLoaderVo.getZone(),
                    fileLoaderVo.getUtc());

            session.execute(bStmtHistTable);
            logger.info("LoadVo.size() is :"+temp);
            temp--;
        }
    } catch (Exception e) {
        System.out.println(e);
    }

}
public void insertIntoCassandra(ArrayList LoadVo)
加载异常{
int temp=LoadVo.size();
试一试{
Session Session=cassandrado.getSession();
if(会话==null){
String msg=“cassandrado.getSession()返回null”;
记录器错误(msg);
抛出新的FileLoaderException(msg);
}
BoundStatement BSTMTHISTABLE=null;
if(pStmtHistTable==null){
pStmtHistTable=会话.prepare(insertToCassandra);
PSTMHistTable.SetConsistenceLevel(ConsistenceLevel.ONE);
}
对于(FileLoaderVo FileLoaderVo:LoadVo){
bStmtHistTable=pStmtHistTable.bind(fileLoaderVo.getSecurityCode(),
fileLoaderVo.getType(),fileLoaderVo.getAreaCode(),
fileLoaderVo.getEmpName(),fileLoaderVo.getCityType(),
fileLoaderVo.getHomeFIPS(),fileLoaderVo.getLastName(),
fileLoaderVo.getDst(),fileLoaderVo.getCssCode(),
fileLoaderVo.getAbbr(),fileLoaderVo.GetOfficePips(),
fileLoaderVo.getMiddleName(),fileLoaderVo.getZone(),
fileLoaderVo.getUtc());
执行(BSTMHistTable);
logger.info(“LoadVo.size()为:“+temp”);
温度--;
}
}捕获(例外e){
系统输出打印ln(e);
}
}
在这里,我将向这个方法传递一个要插入Cassandra的对象的ArrayList。, 但是有没有办法像批插入一样对这些对象运行单个查询

我已经查过数据税了,但什么也找不到,如果您能提供意见,我将不胜感激


提前谢谢

根据您运行的Cassandra版本,您可以将绑定语句添加到批处理(C*2.0)或准备批处理语句(C*1.2)。这篇博文介绍了这两个选项:

基本上,使用C*2.0,您可以:

if (pStmtHistTable == null) {
    pStmtHistTable = session.prepare(insertToCassandra);
    pStmtHistTable.setConsistencyLevel(ConsistencyLevel.ONE);
}
// create a batch statement
BatchStatement batch = new BatchStatement();

for (FileLoaderVo fileLoaderVo : LoadVo) {
    // add bound statements to the batch
    batch.add(pStmtHistTable.bind(...));
}
// execute all
session.execute(batch);

不同分区的批处理会在协调器上增加大量开销,因此不建议使用,除非您希望确保即使协调器和应用程序崩溃,语句也能成功

通过多次异步调用,然后收集结果并重试任何失败的调用,您可能会看到最佳性能

有关包括常见反模式在内的完整详细信息,请参阅:

记录的批次:


未记录的批处理:

这将降低性能,因为默认类型是记录的批处理,我认为它们是在更简单/更高性能的批处理之后插入的。