datastax querybuilder.update在cosmos db上失败,但在cassandra上失败

rmbxnbpk  于 2021-06-13  发布在  Cassandra
关注(0)|答案(0)|浏览(195)

我使用querybuilder和spring数据cassandra 3.x构建了一个更新查询,在cassandra上是成功的,但在cosmos db上不是。
我使用“simplestatement&positional arguments”重新编写了查询,它通过cosmos db没有任何问题。
在我看来,这两个查询是相同的,但不知何故与querybuilder失败,我不知道如何解决它。两个调试记录器都在打印:更新图书集publisher=?,title=?其中id=?两个值都设置了。
错误为:“org.springframework.data.cassandra.cassandrainvalidqueryexception:查询;cql[com.datastax.oss.driver.internal.core.cql。defaultsimplestatement@c1ff87cc]; 参数名publisher无效;嵌套异常为com.datastax.oss.driver.api.core.servererrors.invalidqueryexception:参数名publisher无效“

private boolean udateNotWorkingForCosmosDB(BookEntity myBook) {
        CqlOperations cqlOperations = cassandraOperations.getCqlOperations();

        UpdateStart updateStartQuery = QueryBuilder.update(BookEntity.BOOK);
        Map<String, Object> mapValues = new HashMap<>();
        List<Assignment> assignments = new ArrayList<Assignment>();
        List<String> fields = new ArrayList<>();
        fields.add(BookEntity.COLUMN_PUBLISHER);
        fields.add(BookEntity.COLUMN_TITLE);

        for (String field : fields) {
            assignments.add(Assignment.setColumn(field, QueryBuilder.bindMarker()));
            switch (field) {
            case BookEntity.COLUMN_PUBLISHER:
                mapValues.put(field, "PublsiherB");
                break;
            case BookEntity.COLUMN_TITLE:
                mapValues.put(field,"TitleB");
                break;
            }
        }
        mapValues.put(BookEntity.COLUMN_ID, myBook.getId());

        SimpleStatement updateStatement = updateStartQuery .set(assignments).whereColumn(BookEntity.COLUMN_ID).isEqualTo(QueryBuilder.bindMarker()).build(mapValues);

        LOGGER.debug("Executing entity query: {}", updateStatement.getQuery());

        try {
            return cqlOperations.execute(updateStatement);
        } catch (Exception ex) {
            LOGGER.error("Update query fails on CosmosDB", ex);
            return false;
        }
    }
private boolean updateWorkingForCosmosDB(BookEntity myBook) {
        CqlOperations cqlOperations = cassandraOperations.getCqlOperations();
        SimpleStatementBuilder simpleStatementBuilder= SimpleStatement.builder("");

        String updateStatementStr = "UPDATE " + BookEntity.BOOK;

        List<String> fields = new ArrayList<>();
        fields.add(BookEntity.COLUMN_PUBLISHER);
        fields.add(BookEntity.COLUMN_TITLE);

        for (String field : fields) {
            switch (field) {
            case BookEntity.COLUMN_PUBLISHER:
                if (updateStatementStr.contains("SET")) {
                    updateStatementStr = updateStatementStr.concat(", " + field + "=?");
                } else {
                    updateStatementStr = updateStatementStr.concat(" SET " + field + "=?");
                }
                simpleStatementBuilder.addPositionalValue(myBook.getPublisher());
                break;
            case BookEntity.COLUMN_TITLE:
                if (updateStatementStr.contains("SET")) {
                    updateStatementStr = updateStatementStr.concat(", " + field + "=?");
                } else {
                    updateStatementStr = updateStatementStr.concat(" SET " + field + "=?");
                }
                simpleStatementBuilder.addPositionalValue(myBook.getTitle());
                break;
            }
        }

        updateStatementStr = updateStatementStr.concat(" WHERE id=?");
        simpleStatementBuilder.addPositionalValue(myBook.getId());

        SimpleStatement updateStatement = simpleStatementBuilder.setQuery(updateStatementStr).build();

        LOGGER.debug("Executing MediaSegmentEntity query: {}", updateStatement.getQuery());

        try {
            return cqlOperations.execute(updateStatement);
        } catch (Exception ex) {
            LOGGER.error("Failed to perform update on cosmosdb", ex);
            return false;
        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题