从Quarkus应用程序执行插入时,Cassandra数据库expion
在我们的项目中,我们使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以,我发现了。
DataStax确实很难
在这种情况下,
Quarkus或
So, I figured it out.
Quarkus or DataStax in this case is really ugly
I had a second Method in my Dao
I temporarily added allowFiltering=true to the Annotation and now it worked
您获得的错误与Cassandra驱动程序对象映射有关,在引擎盖下创建一个CQL语句,违反了表的某些约束。可能没有提供强制性属性。
由于设计薄弱,并且可能误解了Cassandra数据建模方法,因此发生了这种情况。让我们改进这个! (最后在github上的工作项目)。
关于表的评论:
您提供的创建表语句等同于
col_1
称为您的分区密钥。它不能确保记录的统一性,并且在每个条款针对表的每个请求中绝对是强制性的。当省略双括号时,第一列被视为分区键。col_2
tocol_4
的列称为聚类列,用于完成确保在该条款中允许的单调和标记列的主要键。该顺序在声明中很重要。例如。如果要过滤col_4,则需要提供col_1
(分区密钥),但还需要col_2
etcol_3
才能了解有关方法的更多信息 https://academy.datastax.com
Strong>
这是一个更新的版本,您会遇到一些错误:
表名是
my_table
您提供了
col_1
两次您忘记了第一个
clusteringColumn(0)
中的偏移
对DAO的评论:
我注意到您使用的是相当新的Quarkus版本,但是Cassandra的旧版本。如果您开始一个项目跳到4.0,则会拥有。免费的完美提升。
在这里,我为您提供了带有数据库架构的工作Quarkus应用程序,并且您的实体=> 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
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
tocol_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 providecol_1
(the partition key) but alsocol_2
etcol_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
twiceYou forgot an offset in the first
ClusteringColumn(0)
Comments about the Dao:
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.