批处理执行为Aysnc不更新任何行

时间:2017-04-13 06:19:30

标签: java cassandra datastax

我正在尝试使用dsc cassandra 3的异步更新,

Integer count = 0;
    String query = "select status, guid from catalog_new where affiliate_id = ? AND store_id =?";
    String approveStoreQuery = "UPDATE catalog_new SET status = ? WHERE affiliate_id = ? AND store_id = ? AND guid = ?";
    PreparedStatement selectStmt = session.prepare(query);
    BoundStatement selectBoundStatement = new BoundStatement(selectStmt);
    ResultSet selectSet = session.execute(selectBoundStatement.bind(new Object[]{affiliateId, storeId}));
    BatchStatement  batchStatement = new BatchStatement(BatchStatement.Type.UNLOGGED);
    Iterator<Row> rowItr = selectSet.iterator();
    while (!selectSet.isFullyFetched()) {
        selectSet.fetchMoreResults();
        Row row = rowItr.next();
        if(row.getInt("status") == statusFrom){
            String guid = row.getString("guid");
            PreparedStatement preparedStatement  = session.prepare(approveStoreQuery);
            BoundStatement boundStatement = new BoundStatement(preparedStatement);
            batchStatement.add(boundStatement.bind(new Object[]{statusTo, affiliateId, storeId, guid}));
            count++;
        }
    }
    session.executeAsync(batchStatement);
    return count;

{此处statusFrom为-2且statusto为-2,ID为3和9} 这不会更新任何行,我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我找到了它:

  

只需将while构造替换为do while,您就可以了!

我还针对具有多个页面的select fetch语句测试了此代码,因此它应该按预期方式工作:

import com.datastax.driver.core.*;

import java.util.Iterator;

public class Hello {
    public static void main(String[] args) {

    Cluster cluster = Cluster.builder()
            .addContactPoint("127.0.0.2")
            .build();

    //I tried to reverse engineer this from your code:
    //I think it's relatively close to what you got

    /*
        CREATE TABLE catalog_new (
            affiliate_id text,
            store_id text,
            guid text,
            status int,
            PRIMARY KEY(affiliate_id, store_id, guid)
        );

        -- Just some test data
        INSERT INTO catalog_new(affiliate_id, store_id, guid, status) VALUES ('af1', 'st1', 'guid1', 0);
        INSERT INTO catalog_new(affiliate_id, store_id, guid, status) VALUES ('af1', 'st1', 'guid2', 0);
        INSERT INTO catalog_new(affiliate_id, store_id, guid, status) VALUES ('af1', 'st1', 'guid3', 0);
    */

    Session session = cluster.connect();

    String affiliateId = "af1";
    String storeId = "st1";

    Integer statusFrom = 0;
    Integer statusTo = 1;

    Integer count = 0;

    String query = "select status, guid from test.catalog_new where affiliate_id = ? AND store_id =?";
    String approveStoreQuery = "UPDATE test.catalog_new SET status = ? WHERE affiliate_id = ? AND store_id = ? AND guid = ?";

    PreparedStatement selectStmt = session.prepare(query);

    BoundStatement selectBoundStatement = new BoundStatement(selectStmt);

    ResultSet selectSet = session.execute(selectBoundStatement.bind(new Object[]{affiliateId, storeId}));

    Iterator<Row> rowItr = selectSet.iterator();

    BatchStatement  batchStatement = new BatchStatement(BatchStatement.Type.UNLOGGED);

    // the way you wrote it is
    // while (!selectSet.isFullyFetched()) {
    // basically you never even go into a loop

    // you might try a do while! - that's all there is to it

    do  {
        selectSet.fetchMoreResults();

        Row row = rowItr.next();

        if (row.getInt("status") == statusFrom) {
            String guid = row.getString("guid");
            PreparedStatement preparedStatement  = session.prepare(approveStoreQuery);
            BoundStatement boundStatement = new BoundStatement(preparedStatement);

            batchStatement.add(boundStatement.bind(statusTo, affiliateId, storeId, guid));

            count++;
        }

    } while (!selectSet.isFullyFetched());

    session.executeAsync(batchStatement);

    // I just made simple print without returning anything just to make sure this works, I tried your example locally and everything runs fine
    System.out.println(count);
   }
  }