java - Spring Data Cassandra - 使用 CqlOperations 运行任意 CQL

标签 java spring spring-boot cassandra integration-testing

不确定 2.0 版本是否有任何变化,但是当使用 Spring Data Cassandra 2.0.5 时,这段代码将不再适用于 Spring Boot 测试:

@Autowired
CqlTemplate cqlTemplate;

这是在教程中介绍的,如何获得读取 JavadocCqlOperations(接口(interface) CqlTemplate 实现)并不是很简单或 Reference doc .

SpringBootTest 中使用该注释将导致:

UnsatisfiedDependencyException: Error creating bean with name
'com.alertavert.api.TriggerRestControllerTest': Unsatisfied 
dependency expressed through field 'cqlTemplate';

最佳答案

解决方案非常简单,但我将其发布在这里是因为我浪费了一些时间在谷歌上搜索它,但没有产生任何有值(value)的东西(而且很多东西只会让事情变得更糟)。

您可以 Autowiring CassandraTemplate 的 bean,然后使用它来获取 CassandraOperation 对象以执行 CQL 查询:

@Autowired
private CassandraTemplate cassandraTemplate;

在你的测试中:

    UUID id = UUID.randomUUID();
    Insert insertBuilder = QueryBuilder.insertInto("triggers")
      .value("trigger_id", id)
      .value("serializedpb", ByteBuffer.wrap("Definitely not a Protobuf".getBytes()));

    cassandraTemplate.getCqlOperations().execute(insertBuilder);

希望这可以节省一些人的时间!

关于java - Spring Data Cassandra - 使用 CqlOperations 运行任意 CQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49343123/

相关文章:

java - setOnClickListener 在简单的 Android 中不起作用

JAVA 文件和图形流数据?

java - DecimalFormat 在舍入时是否不考虑小数值?

Java 类似于 "C++: const return type"- 或者如何允许读取和防止写入非原始成员

JAVA:如何根据属性值合并 arrayList 中的对象?

angular - 在Spring Boot中使用Angular启用了cors,仍然出现cors错误

java - 如何在 Spring 中使用注释定义 PasswordEncoder?

Spring OAuth2 服务器无法使用资源所有者凭据(密码)授权流程刷新 token

spring - 如何在docker容器中配置spring cloud oauth2

java - Spring Boot 在环境中设置 Activity 配置文件