【问题标题】:Spring-Data-Cassandra SchemaAction not workingSpring-Data-Cassandra SchemaAction 不起作用
【发布时间】:2021-02-07 01:53:12
【问题描述】:

我正在尝试通过扩展 CassandraAutoConfiguration 在我的应用程序中配置 Cassandra

我正在使用 spring CassandraRepository 进行 DB 访问,并使用带有 o.s.d.cassandra.core.mapping.Table 注释的类来定义我的表。

我还配置了以下属性,以及集群所需的其他属性

spring: 
  data:
   cassandra:
     schema-action: CREATE_IF_NOT_EXISTS

但在应用程序启动时,Cassandra 中没有创建表。 schemaAction 中的 CassandraProperties 不起作用。

如果我在启动时使用cassandraTemplate.getCqlOperations().execute(...)ApplicationRunner 中以编程方式创建表,那么一切正常。

在这种情况下,我可以使用我的存储库。 find() 和 save() 方法。 只是为了证明我的@table 类是正确编写的

【问题讨论】:

    标签: spring spring-boot cassandra datastax spring-data-cassandra


    【解决方案1】:

    这是我注意到的行为。这不仅适用于application.yaml中的这个特定键

    • 当您不创建任何扩展 AbstractCassandraConfiguration 的 bean 时,spring-data 将读取 application.yaml 中与 spring.data.* 匹配的每个键,包括您提供的模式操作。 (按照惯例)。我没有看到您提供的文件有任何问题,事实上我有一个工作示例here

    • 当您创建扩展 AbstractCassandraConfiguration 的 bean 时,现在您的工作就是明确实现您想要的值,因此请在您的类中添加。您还需要 ro 提供明确的注释 @EnableCassandraRepositories

    @Value("${spring.data.cassandra.schema-action}")
    private String schemaAction;
        
    @Override
    public SchemaAction getSchemaAction() {
      return SchemaAction.valueOf(schemaAction);
    }
    

    除此之外,我想建议不要使用它。 Spring Data 就像一个魅力,但这是我的担忧:

    • 创建表不仅仅是匹配数据模型的问题。确实,基于您的用例或 TTL 或任何元数据的压缩策略怎么样。

    • 我们假设您知道如何使用 Partitions 键和 Clustering 列正确构建主键,但是如果您需要在 2 个表中存储完全相同的对象,因为您有 2 个不同的查询。 (请记住:我需要在应用程序的任何位置进行 ALLOW FILTERING=> 您的数据模型可能是错误的。

    【讨论】:

    • 1- 我在 yml 中有 spring.data.(contact-points/username/password/keyspace-name)。架构操作不起作用.....
    • 2- DID-NOT not create bean by extends AbstractCassandraConfiguration.. created @Configuration class by extension o.s.boot.autoconfigure.cassandra.CassandraAutoConfiguration to create cluster @Bean....
    • 3- 是的,我创建了一个主 @Table 和 3 个查找 @Table..四个 o.s.data.cassandra.repo.CassandraRepository.. 主表类有 @PrimaryKey 作为 UUID (PartitionKey) & 3 个查找类的 Primarykey 为 Java 类 o.s.d.cassandra.core.mapping.PrimaryKeyClass.. 所有 3 个 Java 类都有一个字段为 PartitionKey 并保持为 Clusteringkey
    • CassandraAutoConfiguration 不包含对 SCHEMA 的任何引用,您应该扩展 AbstractCassandraConfiguration 以进行自定义。在此处查看 7.3.1 docs.spring.io/spring-data/cassandra/docs/current-SNAPSHOT/…。您还谈到了Cluster bean,但这个不再存在,被CqlSession
    • 谢谢@clunven,你是对的CassandraPropertiesCassandraAutoConfiguration 中使用,但类没有在代码中使用SchemaAction 字段。因此它被忽略了......但是正如你在你的关注下提到的,我意识到,我不应该在应用程序启动时创建模式 @table 定义,因为元数据在各种环境中会有所不同,'例如'replication_factor &其他领域。所以I'll not use SchemaAction=CreateIfNotExists 它对 POC 有好处,但不适用于在生产中运行应用程序......感谢您的建议。对于未来的 POC,我将扩展 AbstractCassandraConfiguration
    猜你喜欢
    • 2015-01-31
    • 2017-10-25
    • 2020-03-15
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2020-10-31
    相关资源
    最近更新 更多