【问题标题】:Batch queries into Cassandra批量查询到 Cassandra
【发布时间】:2014-07-25 22:00:35
【问题描述】:

我正在尝试将一批对象插入 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);
    }

}

在这里,我向这个方法传递了一个要插入 Cassandra 的对象的 ArrayList。, 但是有什么方法可以像批量插入一样对这些对象运行单个查询?

我已经查看了 datastax,但找不到任何东西,我们将不胜感激。

提前致谢。

【问题讨论】:

    标签: java cassandra batch-processing datastax


    【解决方案1】:

    不同分区的批处理会增加协调器的大量开销,因此不建议这样做,除非您想确保即使协调器和应用程序崩溃也能成功执行语句。

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

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

    记录的批次:http://christopher-batey.blogspot.co.uk/2015/03/cassandra-anti-pattern-cassandra-logged.html

    未记录的批次:http://christopher-batey.blogspot.co.uk/2015/02/cassandra-anti-pattern-misuse-of.html

    【讨论】:

      【解决方案2】:

      根据您运行的 Cassandra 版本,您可以将绑定语句添加到批处理 (C* 2.0) 或准备批处理语句 (C* 1.2)。这篇博文介绍了这两个选项:http://www.datastax.com/dev/blog/client-side-improvements-in-cassandra-2-0

      基本上使用 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);
      

      【讨论】:

      • 这会降低性能,因为默认类型是 LOGGED 批处理,我认为它们是在想要插入更简单/更高性能之后。
      猜你喜欢
      • 2018-01-13
      • 2019-03-25
      • 2021-11-29
      • 2017-08-13
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多