- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用用户定义的类型作为分区键在 cassandra 中存储对象。我正在使用 datastax java 驱动程序进行对象映射,虽然我能够插入到数据库中,但我无法检索对象。如果我更改分区键以使用非 udt(例如文本),我可以保存和检索(即使对象上有其他相同类型的 udt)。通过阅读文档,似乎允许 UDT 作为键。我也找不到任何迹象表明 java 驱动程序也不支持 udts 作为键。看起来它在对象的映射过程中失败了,但只有当 UDT 是分区键时。
这是一项不受支持的功能吗?使用对象映射器时是否只需要使用默认类型作为键?还是我只是做错了什么?
这是我用来设置数据库的cql命令:
create keyspace example_keyspace;
use example_keyspace;
create type my_data_type (value text);
create table my_classes (key frozen<my_data_type>, value frozen<my_data_type>, primary key (key));
这是我用来尝试插入和检索的 java 代码:
package me.example;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import com.datastax.driver.mapping.annotations.Frozen;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import com.datastax.driver.mapping.annotations.UDT;
public class Main {
public static void main(String[] args) {
try (Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1")
.build()) {
Mapper<MyClass> mapper = new MappingManager(cluster.newSession())
.mapper(MyClass.class);
MyDataType value = new MyDataType();
value.setValue("theValue");
MyDataType key = new MyDataType();
key.setValue("theKey");
MyClass myClass = new MyClass();
myClass.setKey(key);
myClass.setValue(value);
mapper.save(myClass);
MyClass toret = mapper.get(key);
System.out.println(toret.getKey());
System.out.println(toret.getValue().getValue());
}
}
@Table(keyspace = "example_keyspace", name = "my_classes")
public static class MyClass {
@PartitionKey
@Frozen
private MyDataType key;
@Frozen
private MyDataType value;
public MyDataType getKey() {
return key;
}
public void setKey(MyDataType key) {
this.key = key;
}
public MyDataType getValue() {
return value;
}
public void setValue(MyDataType value) {
this.value = value;
}
}
@UDT(keyspace = "example_keyspace", name = "my_data_type")
public static class MyDataType {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof MyDataType)) {
return false;
}
MyDataType other = (MyDataType) obj;
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}
}
}
选择显示对象已成功插入:
select * from my_classes;
产量:
key | value
-------------------+---------------------
{value: 'theKey'} | {value: 'theValue'}
但是我的 eclipse 控制台输出错误:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.datastax.driver.core.exceptions.InvalidTypeException: Invalid value for CQL type frozen<example_keyspace.my_data_type>, expecting class com.datastax.driver.core.UDTValue but class me.example.Main$MyDataType provided
at com.datastax.driver.core.DataType.serialize(DataType.java:619)
at com.datastax.driver.mapping.Mapper.getQuery(Mapper.java:320)
at com.datastax.driver.mapping.Mapper.get(Mapper.java:342)
at me.example.Main.main(Main.java:31)
我也试过使用 UDTValue 对象作为检索对象的键,但我仍然遇到同样的错误。
我在 OS X 10.10.4 上运行 cassandra 2.1.7。 Java 版本为 1.8.0_45。 Datastax java 驱动核心和映射器是 2.1.6 版本。
谢谢!
最佳答案
这是驱动程序中的错误。我创建了 JAVA-831在我们的问题跟踪器中。
同时,您可以使用以下解决方法:
MappingManager manager = new MappingManager(cluster.newSession());
UDTMapper<MyDataType> myDataTypeMapper = manager.udtMapper(MyDataType.class);
UDTValue keyAsUDTValue = myDataTypeMapper.toUDT(key);
MyClass toret = mapper.get(keyAsUDTValue);
关于java - 无法使用 datastax java 驱动程序通过 UDT key 从 cassandra 检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373008/
我通过 spring ioc 编写了一些 Rest 应用程序。但我无法解决这个问题。这是我的异常(exception): org.springframework.beans.factory.BeanC
我对 TestNG、Spring 框架等完全陌生,我正在尝试使用注释 @Value通过 @Configuration 访问配置文件注释。 我在这里想要实现的目标是让控制台从配置文件中写出“hi”,通过
为此工作了几个小时。我完全被难住了。 这是 CS113 的实验室。 如果用户在程序(二进制计算器)结束时选择继续,我们需要使用 goto 语句来到达程序的顶部。 但是,我们还需要释放所有分配的内存。
我正在尝试使用 ffmpeg 库构建一个小的 C 程序。但是我什至无法使用 avformat_open_input() 打开音频文件设置检查错误代码的函数后,我得到以下输出: Error code:
使用 Spring Initializer 创建一个简单的 Spring boot。我只在可用选项下选择 DevTools。 创建项目后,无需对其进行任何更改,即可正常运行程序。 现在,当我尝试在项目
所以我只是在 Mac OS X 中通过 brew 安装了 qt。但是它无法链接它。当我尝试运行 brew link qt 或 brew link --overwrite qt 我得到以下信息: ton
我在提交和 pull 时遇到了问题:在提交的 IDE 中,我看到: warning not all local changes may be shown due to an error: unable
我跑 man gcc | grep "-L" 我明白了 Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more inf
我有一段代码,旨在接收任何 URL 并将其从网络上撕下来。到目前为止,它运行良好,直到有人给了它这个 URL: http://www.aspensurgical.com/static/images/a
在过去的 5 个小时里,我一直在尝试在我的服务器上设置 WireGuard,但在完成所有设置后,我无法 ping IP 或解析域。 下面是服务器配置 [Interface] Address = 10.
我正在尝试在 GitLab 中 fork 我的一个私有(private)项目,但是当我按下 fork 按钮时,我会收到以下信息: No available namespaces to fork the
我这里遇到了一些问题。我是 node.js 和 Rest API 的新手,但我正在尝试自学。我制作了 REST API,使用 MongoDB 与我的数据库进行通信,我使用 Postman 来测试我的路
下面的代码在控制台中给出以下消息: Uncaught DOMException: Failed to execute 'appendChild' on 'Node': The new child el
我正在尝试调用一个新端点来显示数据,我意识到在上一组有效的数据中,它在数据周围用一对额外的“[]”括号进行控制台,我认为这就是问题是,而新端点不会以我使用数据的方式产生它! 这是 NgFor 失败的原
我正在尝试将我的 Symfony2 应用程序部署到我的 Azure Web 应用程序,但遇到了一些麻烦。 推送到远程时,我在终端中收到以下消息 remote: Updating branch 'mas
Minikube已启动并正在运行,没有任何错误,但是我无法 curl IP。我在这里遵循:https://docs.traefik.io/user-guide/kubernetes/,似乎没有提到关闭
每当我尝试docker组成任何项目时,都会出现以下错误。 我尝试过有和没有sudo 我在这台机器上只有这个问题。我可以在Mac和Amazon WorkSpace上运行相同的容器。 (myslabs)
我正在尝试 pip install stanza 并收到此消息: ERROR: No matching distribution found for torch>=1.3.0 (from stanza
DNS 解析看起来不错,但我无法 ping 我的服务。可能是什么原因? 来自集群中的另一个 Pod: $ ping backend PING backend.default.svc.cluster.l
我正在使用Hibernate 4 + Spring MVC 4当我开始 Apache Tomcat Server 8我收到此错误: Error creating bean with name 'wel
我是一名优秀的程序员,十分优秀!