datastax / cassandra-quarkus

An Apache Cassandra(R) extension for Quarkus
Apache License 2.0
39 stars 28 forks source link

Question : How to use UDT column with cassandra-quarkus driver? #201

Closed usuket closed 2 years ago

usuket commented 2 years ago

Hello,

I am building new API application with cassandra-quarkus driver with Cassandra 3.11.6

But I stacked CQL conversion due to missing codec in the codec registry. I am pretty sure the issue comes from the UDT, Codec, and Registry but I couldn't figure out a way to register the codec.

I built the basic function based on the quickstart project, and it worked without any trouble except UDT.

I tried annotation with com.datastax.driver.mapping.annotations.UDT and field:Column with Codec. I believe UDT is a common use case, can you help me or can you add additional information to the document or sample code?

These are my kotlin codes and DDL, I will put them as a reference. (But I modified the field name and package name so it might not be accurate)

Thanks in advance.

@UDT(name = "payment") // Tried, but not work?
class Payment(
    @field:Field
    val payment_type: String? = "tbd",
    @field:Field
    val amount: Double? = null,
)
@Entity
@PropertyStrategy(mutable = false)
class SampleData(
    @field:PartitionKey(0) var id: String,
      @field:Frozen("<list<payment>>")
//    @field:Column(codec = SampleCodec::class) // Tried, but not work?
    var payments: List<Payment>?,
)
create table sample_data
(
    id          text,
    payments                frozen<list<payment>>,
    primary key (id)
);
CREATE TYPE IF NOT EXISTS
    payment(
      payment_type text,
      amount double,
    );

Track trace is.

stack: 'org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalArgumentException: The CQL ks.table: sample.sample_data defined in the entity class: com.example.dao.cassandra.SampleData declares type mappings that are not supported by the codec registry:\n' + 'Field: payments, Entity Type: com.sample.dao.cassandra.Payment, CQL type: UDT(sample.payment)\n' + '\tat org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:105)\n' + '\tat org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:359)\n' + '\tat org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218)\n' + '\tat org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519)\n' + '\tat org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:261)\n' + '\tat org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:161)\n' + '\tat org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:364)\n' + '\tat org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:164)\n' + '\tat org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:247)\n' + '\tat io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)\n' + '\tat io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:135)\n' + '\tat io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.run(VertxRequestHandler.java:90)\n' + '\tat io.quarkus.vertx.core.runtime.VertxCoreRecorder$13.runWith(VertxCoreRecorder.java:543)\n' + '\tat org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)\n' + '\tat org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)\n' + '\tat org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)\n' + '\tat org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)\n' + '\tat io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)\n' + '\tat java.base/java.lang.Thread.run(Thread.java:829)\n' + 'Caused by: java.lang.IllegalArgumentException: The CQL ks.table:

┆Issue is synchronized with this Jira Task by Unito

adutra commented 2 years ago

Hi @usuket, thanks for reporting this.

The Cassandra Quarkus extension does support UDTs, but it is true that we currently do not test this in our integration tests. I added a few tests in a new PR, see #202.

As you can see there, UDT classes must be annotated with @Entity. However your class is annotated with @UDT(name = "payment") – but @UDT is not a supported annotation.

My guess is that you are mixing jars from driver 4.x and 3.x in your application, because @UDT used to be a valid annotation in driver 3.x, but not in driver 4.x.

Please remove all dependencies to driver 3.x from your code as this driver version is NOT compatible with the Cassandra Quarkus extension.

Read more about mapping features in driver 4.x here.

Let me know if that helps.

adutra commented 2 years ago

I'm going to close this to avoid stale issues. Feel free to reopen if you have further questions. Thanks!

usuket commented 2 years ago

@adutra I apologize for the late reply. I confirmed that it works well with @Entity annotation with the latest version of the Cassandra Driver.

Thank you for your support.