我在Cassandra表格中存储了一张包含地图的地图:
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
由于上述原因,必须实现自定义编解码器似乎有点奇怪。
我错过了一些明显的东西吗?什么是最干净的解决方案?
由于
答案 0 :(得分:2)
通过使用类型令牌来修复此问题:
__new__