【问题标题】:Spring Data Cassandra - using CqlOperations to run arbitrary CQLSpring Data Cassandra - 使用 CqlOperations 运行任意 CQL
【发布时间】:2018-03-17 23:53:57
【问题描述】:

不确定2.0 版本是否有任何变化,但是当使用 Spring Data Cassandra 2.0.5 时,此代码将不再在 Spring Boot 测试中工作:

@Autowired
CqlTemplate cqlTemplate;

这是在教程中介绍的,如何让CqlOperations(接口CqlTemplate 实现)读取JavadocReference doc 并不是很简单。

SpringBootTest 中使用该注释会导致:

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

【问题讨论】:

    标签: java spring spring-boot cassandra integration-testing


    【解决方案1】:

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

    您可以自动装配一个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);
    

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

    【讨论】:

      猜你喜欢
      • 2020-03-08
      • 2019-10-29
      • 2015-11-09
      • 2014-12-06
      • 1970-01-01
      • 2019-10-12
      • 2018-02-06
      • 1970-01-01
      • 2018-02-21
      相关资源
      最近更新 更多