2017-04-13 13 views
0

我想使用DSC卡桑德拉3 aync更新,批量EXECUTE AS Aysnc不更新任何行

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; 

{这里是状态从-2和statusto是-2,IDS是3和9} 这不会更新任何行,我在这里做错了什么?

+0

请提供表/ s的说明和报表。 – nevsv

+0

'approveStoreQuery'的外观如何? –

+0

你有没有得到任何错误? –

回答

1

我发现:

只需更换while构建一个do while,你会被罚款!

我还测试了这款代码对选择获取具有多个页面的语句,因此如预期的你应该工作:

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); 
    } 
    }