我正在尝试从 kotlin 中的 spring 代码访问 C*。它失败并出现
Primitive type 'int' used as type parameter
错误。我怀疑这是由于 kotlin 对 JVM 中原始类型的神奇拆箱所致。如何解决这个问题? row.getList("c1", Int::class.java)
是罪魁祸首,我这里绝对需要盒装类型。
这是我的代码片段。不,我不能使用 CassandraTemplate,在我的场景中,模式非常流畅,可以在运行时更改。
@Component
class Tst(
private val cql: ReactiveCqlTemplate,
) {
val ks = "test1"
val table = "some_table"
init {
runBlocking {
cql.execute("create keyspace if not exists $ks with replication = {'class': 'SimpleStrategy', 'replication_factor': 1};")
.awaitSingle()
cql.execute("drop table if exists $ks.$table;").awaitSingle()
cql.execute(
"""create table if not exists $ks.$table
(
id bigint,
c1 list<int>,
primary key (id)
);"""
).awaitSingle()
val someList = listOf(1, 2, 4, 67, 8, 5, 3, 3, 79, 7453457)
cql.execute(
"insert into $ks.$table (id, c1) values (?,?)",
1L, someList
).awaitSingle()
val result = cql.query("select c1 from $ks.$table where id = ${1L}") { row, _ ->
val result = row.getList("c1", Int::class.java)
result
}.next().awaitSingle()
println(result)
}
}
}
完整的异常堆栈:
Caused by: java.lang.IllegalArgumentException: Primitive type 'int' used as type parameter
at com.datastax.oss.driver.shaded.guava.common.base.Preconditions.checkArgument(Preconditions.java:440)
at com.datastax.oss.driver.shaded.guava.common.reflect.Types.disallowPrimitiveType(Types.java:525)
at com.datastax.oss.driver.shaded.guava.common.reflect.Types.access$200(Types.java:54)
at com.datastax.oss.driver.shaded.guava.common.reflect.Types$ParameterizedTypeImpl.<init>(Types.java:267)
at com.datastax.oss.driver.shaded.guava.common.reflect.Types.newParameterizedType(Types.java:102)
at com.datastax.oss.driver.shaded.guava.common.reflect.Types.newParameterizedTypeWithOwner(Types.java:91)
at com.datastax.oss.driver.shaded.guava.common.reflect.TypeResolver.resolveParameterizedType(TypeResolver.java:265)
at com.datastax.oss.driver.shaded.guava.common.reflect.TypeResolver.resolveType(TypeResolver.java:220)
at com.datastax.oss.driver.shaded.guava.common.reflect.TypeToken.where(TypeToken.java:231)
at com.datastax.oss.driver.api.core.type.reflect.GenericType.listOf(GenericType.java:126)
at com.datastax.oss.driver.api.core.data.GettableByIndex.getList(GettableByIndex.java:499)
at com.datastax.oss.driver.api.core.data.GettableByName.getList(GettableByName.java:580)
at com.example.Tst$1.invokeSuspend$lambda$0(Tst.kt:49)
at org.springframework.data.cassandra.core.cql.ReactiveRowMapperResultSetExtractor.lambda$extractData$0(ReactiveRowMapperResultSetExtractor.java:61)
at reactor.core.publisher.FluxHandleFuseable$HandleFuseableSubscriber.onNext(FluxHandleFuseable.java:179)
at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.drainAsync(FluxFlattenIterable.java:453)
at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.drain(FluxFlattenIterable.java:724)
at reactor.core.publisher.FluxFlattenIterable$FlattenIterableSubscriber.onNext(FluxFlattenIterable.java:256)
at reactor.core.publisher.FluxExpand$ExpandBreathSubscriber.onNext(FluxExpand.java:118)
at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2571)
at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.set(Operators.java:2367)
at reactor.core.publisher.FluxExpand$ExpandBreathSubscriber.onSubscribe(FluxExpand.java:112)
at reactor.core.publisher.MonoJust.subscribe(MonoJust.java:55)
at reactor.core.publisher.Mono.subscribe(Mono.java:4563)
at reactor.core.publisher.FluxExpand$ExpandBreathSubscriber.drainQueue(FluxExpand.java:179)
at reactor.core.publisher.MonoExpand.subscribeOrReturn(MonoExpand.java:54)
at reactor.core.publisher.Flux.subscribe(Flux.java:8821)
at reactor.core.publisher.MonoFlatMapMany$FlatMapManyMain.onNext(MonoFlatMapMany.java:196)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:122)
at reactor.core.publisher.MonoCompletionStage$MonoCompletionStageSubscription.apply(MonoCompletionStage.java:121)
at reactor.core.publisher.MonoCompletionStage$MonoCompletionStageSubscription.apply(MonoCompletionStage.java:67)
at java.base/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934)
at java.base/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911)
at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147)
at com.datastax.oss.driver.internal.core.cql.CqlRequestHandler.setFinalResult(CqlRequestHandler.java:324)
at com.datastax.oss.driver.internal.core.cql.CqlRequestHandler.access$1500(CqlRequestHandler.java:95)
at com.datastax.oss.driver.internal.core.cql.CqlRequestHandler$NodeResponseCallback.onResponse(CqlRequestHandler.java:655)
at com.datastax.oss.driver.internal.core.channel.InFlightHandler.channelRead(InFlightHandler.java:257)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:289)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:346)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:318)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:840)
虽然 Kotlin 编译器会抱怨使用 Integer 而不是 Int,但似乎要走的路是
row.getList("c1", Integer::class.java)