从Quarkus应用程序执行插入时,Cassandra数据库expion

发布于 2025-01-30 03:54:12 字数 1349 浏览 3 评论 0原文

在我们的项目中,我们使用Quarkus版本2.8.0. -final 以及DataStax版本1.1.1访问Cassandra数据库版本3.11

WA时,当我们通过Java执行插入物时,将获得以下异常。

com.datastax.oss.oss.driver.api.core.servererrors.invalidqueryException:主键列“ col_4”不能限制为前面的列“ col_3”,不限制

我们不明白为什么会发生这种情况,因为以下语句从vscode中执行了以下语句,成功没有任何问题

INSERT INTO my_table  (
    col_1,
    col_2,
    col_3,
    col_4,
    col_5,
    col_6,
    col_7
  )
VALUES (
    'Some Value',
    '5ea20642-e9e7-44bd-b4f0-97b84ae97f2c',
    '1ed91e32-a7ba-4b5f-b4dd-9cfe3e522914',
    'Another Value',
    '2010-01-01',
    '24afc036-21e8-41c6-9f7a-1994fba71223',
    'Just one mor value'
  );

我们的桌子如下

CREATE TABLE IF NOT EXISTS my_table (
    col_1 text,
    col_2 text,
    col_3 text,
    col_4 text,
    col_5 text,
    col_6 text,
    col_7 date,
    PRIMARY KEY(col_1, col_2, col_3, col_4)
);

所示

@Entity
@CqlName("my_entity")
public class MyEntity {

  @PartitionKey
  private String col_1;
  @ClusterinColumn
  private String col_2;
  @ClusterinColumn(1)
  private String col_3;
  @ClusterinColumn(2)
  private String col_4;
  
  private String col_5;
  private String col_6;
  private LocalDate col_1;

}

@Dao
public interface MyEntityDao {

  @Insert
  void insert (MyEntity myEntity)

}

In our Project we are using Quarkus Version 2.8.0.FINAL
and Datastax Version 1.1.1 accessing cassandra database Version 3.11

Wa are getting the following Exception when we perform an insert via Java.

com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: PRIMARY KEY column "col_4" cannot be restricted as preceding column "col_3" is not restricted

We do not understand why that happens since the following statement executed via SQLTools from VSCode succeeds without any problem

INSERT INTO my_table  (
    col_1,
    col_2,
    col_3,
    col_4,
    col_5,
    col_6,
    col_7
  )
VALUES (
    'Some Value',
    '5ea20642-e9e7-44bd-b4f0-97b84ae97f2c',
    '1ed91e32-a7ba-4b5f-b4dd-9cfe3e522914',
    'Another Value',
    '2010-01-01',
    '24afc036-21e8-41c6-9f7a-1994fba71223',
    'Just one mor value'
  );

Our Table is as follows

CREATE TABLE IF NOT EXISTS my_table (
    col_1 text,
    col_2 text,
    col_3 text,
    col_4 text,
    col_5 text,
    col_6 text,
    col_7 date,
    PRIMARY KEY(col_1, col_2, col_3, col_4)
);

The Entity

@Entity
@CqlName("my_entity")
public class MyEntity {

  @PartitionKey
  private String col_1;
  @ClusterinColumn
  private String col_2;
  @ClusterinColumn(1)
  private String col_3;
  @ClusterinColumn(2)
  private String col_4;
  
  private String col_5;
  private String col_6;
  private LocalDate col_1;

}

The Dao

@Dao
public interface MyEntityDao {

  @Insert
  void insert (MyEntity myEntity)

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最佳男配角 2025-02-06 03:54:13

所以,我发现了。
DataStax确实很难

在这种情况下,

@Select (custimWhereClause = "col2 = :col_2_value and col4 = :col_4_value")
PagingIterable<MyEntity> findByCriteria(String col_2_value, String col_4_value)

Quarkus或

So, I figured it out.
Quarkus or DataStax in this case is really ugly

I had a second Method in my Dao

@Select (custimWhereClause = "col2 = :col_2_value and col4 = :col_4_value")
PagingIterable<MyEntity> findByCriteria(String col_2_value, String col_4_value)

I temporarily added allowFiltering=true to the Annotation and now it worked

琉璃梦幻 2025-02-06 03:54:12

您获得的错误与Cassandra驱动程序对象映射有关,在引擎盖下创建一个CQL语句,违反了表的某些约束。可能没有提供强制性属性。

由于设计薄弱,并且可能误解了Cassandra数据建模方法,因此发生了这种情况。让我们改进这个! (最后在github上的工作项目)。

关于表的评论:

您提供的创建表语句等同于

CREATE TABLE IF NOT EXISTS my_table (
   col_1 text,
   col_2 text,
   col_3 text,
   col_4 text,
   col_5 text,
   col_6 text,
   col_7 date,
PRIMARY KEY((col_1), col_2, col_3, col_4)
WITH CLUSTERING ORDER BY (col_2 DESC, col_3 DESC, col_4 DESC);
  • col_1称为您的分区密钥。它不能确保记录的统一性,并且在每个条款针对表的每个请求中绝对是强制性的。当省略双括号时,第一列被视为分区键。

  • col_2 to col_4的列称为聚类列,用于完成确保在该条款中允许的单调和标记列的主要键。该顺序在声明中很重要。例如。如果要过滤col_4,则需要提供col_1(分区密钥),但还需要col_2 et col_3

才能了解有关方法的更多信息 https://academy.datastax.com

Strong>

这是一个更新的版本,您会遇到一些错误:

  • 表名是my_table

  • 您提供了col_1两次

  • 您忘记了第一个clusteringColumn(0)

    中的偏移

     @entity
    @cqlname(“ my_table”)
    公共班级MENTITY {
    
      @PartitionKey
      @cqlname(“ col_1”)
      私人字符串col1;
    
      @ClusteringColumn(0)
      @cqlname(“ col_2”)
      私人字符串COL2;
    
      @ClusteringColumn(1)
      @cqlname(“ col_3”)
      私人字符串Col3;
    
      @ClusteringColumn(2)
      @cqlname(“ col_4”)
      私人字符串col4;
    
      @cqlname(“ col_5”)
      私人字符串col5;
    
      @cqlname(“ col_6”)
      私人字符串Col6;
    
      @cqlname(“ col_7”)
      私人localdate col7;
    
      //构造函数
      //获取器和固定器
    }
     

对DAO的评论:

  @Dao
  @DefaultNullSavingStrategy(NullSavingStrategy.SET_TO_NULL)
  public interface MyEntityDao {

    @Delete(entityClass = MyEntity.class)
    void deleteByCol1(String col1);

    @Select
    PagingIterable<MyEntity> findByPkCol1(String col1);

    @Select
    Optional<MyEntity> findById(String col1, String col2, String col3, String col4);

    @Insert
    void save(MyEntity product);    

  }

我注意到您使用的是相当新的Quarkus版本,但是Cassandra的旧版本。如果您开始一个项目跳到4.0,则会拥有。免费的完美提升。

在这里,我为您提供了带有数据库架构的工作Quarkus应用程序,并且您的实体=&gt; https://github.com/clun/qlun/quarkus_cassandra

现在,请永远不要使用允许过滤。它将对您的群集进行全面扫描,并且仍然很慢,然后将返回超时(在DEV工作时)。如果您需要在相同数据上进行多个查询...请在多个表中复制数据。

The error you got is related to the Cassandra Driver object mapping, creating under the hood a CQL Statement that violates some constraints of your table. A mandatory attribute was probably not provided.

It happens because of a weak design and probably some misunderstanding of the Cassandra data modeling methodology. Let's improve this ! (working project on github at the end).

Comments about the table:

The creation table statement your provided is equivalent to

CREATE TABLE IF NOT EXISTS my_table (
   col_1 text,
   col_2 text,
   col_3 text,
   col_4 text,
   col_5 text,
   col_6 text,
   col_7 date,
PRIMARY KEY((col_1), col_2, col_3, col_4)
WITH CLUSTERING ORDER BY (col_2 DESC, col_3 DESC, col_4 DESC);
  • col_1 is called your partition key. It does not ensure the unicity of the record and is absolutely mandatory in every request WHERE CLAUSE against the table. When double parenthesis are omited the first column is considered the partition key.

  • Columns from col_2 to col_4 are called clustering columns and are used to complete the primary key to ensuring unicity and mark columns allowed in the clause WHERE. The order is important in the declaration. eg. If you want to filter on col_4 you need to provide col_1 (the partition key) but also col_2 et col_3

To know more about the methodology take the free course DS220 on https://academy.datastax.com

Comments about the entity:

Here is an updated version you got a few mistakes:

  • The table name is my_table

  • You provided the col_1 twice

  • You forgot an offset in the first ClusteringColumn(0)

    @Entity
    @CqlName("my_table")
    public class MyEntity {
    
      @PartitionKey
      @CqlName("col_1")
      private String col1;
    
      @ClusteringColumn(0)
      @CqlName("col_2")
      private String col2;
    
      @ClusteringColumn(1)
      @CqlName("col_3")
      private String col3;
    
      @ClusteringColumn(2)
      @CqlName("col_4")
      private String col4;
    
      @CqlName("col_5")
      private String col5;
    
      @CqlName("col_6")
      private String col6;
    
      @CqlName("col_7")
      private LocalDate col7;
    
      // Constructors
      // Getters and Setters
    }
    

Comments about the Dao:

  @Dao
  @DefaultNullSavingStrategy(NullSavingStrategy.SET_TO_NULL)
  public interface MyEntityDao {

    @Delete(entityClass = MyEntity.class)
    void deleteByCol1(String col1);

    @Select
    PagingIterable<MyEntity> findByPkCol1(String col1);

    @Select
    Optional<MyEntity> findById(String col1, String col2, String col3, String col4);

    @Insert
    void save(MyEntity product);    

  }

I noticed you are using a fairly new Quarkus version but an old version of Cassandra. If you are starting a project jump to the 4.0 you will have. free perfromance boost.

Here I provide you the working Quarkus Application with your database schema and your entity => https://github.com/clun/quarkus_cassandra

Now, please NEVER, EVER use ALLOW FILTERING. It will do a full scan of your cluster and still be slow and then will return timeout (while working in dev). If you need multiple queries on the same data...duplicate the data in multiple tables, please.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文