我正在 Cassandra 表中存储包含 Maps 的 Map:
CREATE TABLE reportsdb.report (
report_id TEXT,
entity_type TEXT,
entity_id TEXT,
parameters MAP<TEXT, FROZEN<MAP<TEXT, INT>>>,
PRIMARY KEY ((report_id), entity_type, entity_id)
);
存储数据工作正常,没有任何问题。 当检索数据(使用 Java 和 datastax 驱动程序)时,我想做这样的事情:
resultSet.forEach(row -> {
Map<String, Map> parametersMap = row.getMap("parameters", String.class, Map.class);
// Do something with the parametersMap here...
}
由于我无法获取我需要定义的泛型类型的类
Map<String, Map>
当我真的想定义时,并转换它
Map<String, Map<String, Integer>>
但是,当运行上面的代码时,出现以下异常:
Caused by: com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [map<varchar, int> <-> java.util.Map]
wildfly | at com.datastax.driver.core.CodecRegistry.notFound(CodecRegistry.java:741)
wildfly | at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:594)
wildfly | at com.datastax.driver.core.CodecRegistry.findCodec(CodecRegistry.java:558)
wildfly | at com.datastax.driver.core.CodecRegistry.maybeCreateCodec(CodecRegistry.java:643)
wildfly | at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:586)
wildfly | at com.datastax.driver.core.CodecRegistry.access$500(CodecRegistry.java:137)
wildfly | at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:246)
wildfly | at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:232)
wildfly | at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3542)
wildfly | at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2323)
wildfly | at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2286)
wildfly | at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201)
wildfly | at com.google.common.cache.LocalCache.get(LocalCache.java:3953)
wildfly | at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3957)
wildfly | at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4875)
wildfly | at com.datastax.driver.core.CodecRegistry.lookupCodec(CodecRegistry.java:522)
wildfly | at com.datastax.driver.core.CodecRegistry.codecFor(CodecRegistry.java:485)
wildfly | at com.datastax.driver.core.AbstractGettableByIndexData.codecFor(AbstractGettableByIndexData.java:73)
wildfly | at com.datastax.driver.core.AbstractGettableByIndexData.getMap(AbstractGettableByIndexData.java:338)
wildfly | at com.datastax.driver.core.AbstractGettableData.getMap(AbstractGettableData.java:26)
wildfly | at com.datastax.driver.core.AbstractGettableByIndexData.getMap(AbstractGettableByIndexData.java:327)
wildfly | at com.datastax.driver.core.AbstractGettableData.getMap(AbstractGettableData.java:26)
wildfly | at com.datastax.driver.core.AbstractGettableData.getMap(AbstractGettableData.java:231)
wildfly | at com.ericsson.sm.reports.slaReport.SlaReportDao.lambda$getReportsFromResultSet$2(SlaReportDao.java:111)
wildfly | at java.lang.Iterable.forEach(Iterable.java:75)
wildfly | at com.ericsson.sm.reports.slaReport.SlaReportDao.getReportsFromResultSet(SlaReportDao.java:105)
wildfly | at com.ericsson.sm.reports.common.AbstractDao.getReports(AbstractDao.java:38)
wildfly | at com.ericsson.sm.reports.common.AbstractService.getReports(AbstractService.java:65)
wildfly | at com.ericsson.sm.reports.slaReport.SlaReportRestService.getDeviceSlaReports(SlaReportRestService.java:49)
wildfly | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
wildfly | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
wildfly | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
wildfly | at java.lang.reflect.Method.invoke(Method.java:498)
wildfly | at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140)
wildfly | at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
wildfly | at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
wildfly | at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
wildfly | at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:406)
wildfly | ... 56 more
由于上述原因,必须实现自定义编解码器似乎有点奇怪。
我在这里遗漏了一些明显的东西吗?最干净的解决方案是什么?
谢谢
最佳答案
通过使用类型标记修复了该问题:
Map<String, Map<String, Integer>> parametersMap = row
.getMap("parameters", TypeToken.of(String.class), TypeTokens.mapOf(String.class, Integer.class));
关于java - Cassandra 中的 map ,不使用自定义编解码器。可能的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50064970/