gpt4 book ai didi

java - 作为异步批量执行不更新任何行

转载 作者:行者123 更新时间:2023-12-02 13:25:48 26 4
gpt4 key购买 nike

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

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 ,ids 是 3 和 9}这不会更新任何行,我在这里做错了什么?

最佳答案

我找到了:

Just replace the while construct with a do while and you will be fine!

我还针对具有多个页面的 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);
}
}

关于java - 作为异步批量执行不更新任何行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43384866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com