gpt4 book ai didi

user-defined-types - UserTypeResolver 不能为空

转载 作者:行者123 更新时间:2023-12-04 20:32:25 25 4
gpt4 key购买 nike

我一直在尝试测试 Cassandra UDT 的插入,但一直遇到以下错误:
线程“main”中的异常 java.lang.IllegalArgumentException: UserTypeResolver 不能为 null

在试图找出自己的方法之后,我试图完全复制以下概述的方法:
User Defined Type with spring-data-cassandra

但是,我仍然遇到相同的错误。

当我删除 UDT 并只插入简单类型时,我能够插入到目标数据库中,所以我知道我正在正确连接。我的配置如下:

@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
//@EnableCassandraRepositories(basePackages = { "org.spring.cassandra.example.repo" })
public class CassandraConfig {

private static final Logger LOG = LoggerFactory.getLogger(CassandraConfig.class);

@Autowired
private Environment env;

@Bean
public CassandraClusterFactoryBean cluster() {

CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port")));

return cluster;
}

@Bean
public CassandraMappingContext mappingContext() {
BasicCassandraMappingContext mappingContext = new BasicCassandraMappingContext();
mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(), "campaign_management"));
return mappingContext;
}

@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}

@Bean
public CassandraSessionFactoryBean session() throws Exception {

CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(env.getProperty("cassandra.keyspace"));
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);

return session;
}

@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}
}

我的 Address 和 Employee 类与我上面引用的 SO 问题中显示的完全一样,而我的 Main 只是:
public class MainClass {

public static void main(String[] args) {

ApplicationContext service = new AnnotationConfigApplicationContext(CassandraConfig.class);

Employee employee = new Employee();
employee.setEmployee_id(UUID.randomUUID());
employee.setEmployee_name("Todd");
Address address = new Address();
address.setAddress_type("Home");
address.setId("ToddId");
employee.setAddress(address);
CassandraOperations operations = service.getBean("cassandraTemplate", CassandraOperations.class);

operations.insert(employee);

System.out.println("Done");
}
}

我在用:
datastax.cassandra.driver.version=3.1.3
spring.data.cassandra.version=1.5.1
spring.data.commons.version=1.13.1
spring.cql.version=1.5.1

上一个 SO 问题中引用的版本是 1.5.0,尽管 spring.io 将 1.5.1 列为当前版本,所以我正在使用它,但没有显示可用的 1.5.0。

任何帮助将不胜感激,因为这让我有点疯狂。

最佳答案

当您错过 cassandra 映射下的 UserTypeResolver 时,您通常会收到此错误,它本身由 cassandra 转换器使用,本身由 Spring Data Cassandra 模板使用

详情:

假设你有一个基本的 Spring MVC Controller 并在其他地方运行......

Cassandra 中的 UserDefinedTypes 在 SET 和 MAP 中最有趣,下面的示例就是这种类型。

具有所有默认值的 Spring Bean 配置示例(Spring XML 应用程序上下文提取):

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
...
<!-- ===== CASSANDRA ===== -->
<!-- Loads the properties into the Spring Context and uses them to fill in placeholders in bean definitions below -->
<context:property-placeholder location="/WEB-INF/spring/cassandra.properties" />

<!-- REQUIRED: The Cassandra Cluster -->
<cassandra:cluster contact-points="${cassandra.contactpoints}"
port="${cassandra.port}" username="cassandra" password="cassandra"
auth-info-provider-ref="authProvider" />

<!-- REQUIRED: The Cassandra Session, built from the Cluster, and attaching to a keyspace -->
<cassandra:session keyspace-name="${cassandra.keyspace}" />

<!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter
DO include a userTypeResolver for UDT support -->
<cassandra:mapping entity-base-packages="fr.woobe.model">
<cassandra:user-type-resolver keyspace-name="${cassandra.keyspace}" />
</cassandra:mapping>

<!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->
<cassandra:converter />

<bean id="authProvider" class="com.datastax.driver.core.PlainTextAuthProvider">
<constructor-arg index="0" value="myCassandraUser" />
<constructor-arg index="1" value="somePassword" />
</bean>

<!-- REQUIRED: The Cassandra Template is the building block of all Spring Data Cassandra -->
<cassandra:template id="cassandraTemplate" />
...

然后在 java 中,通常在您的 Spring MVC Controller 中:
import org.springframework.data.cassandra.core.CassandraOperations;
...
// acquire DB template
CassandraOperations cOps = this.beanFactory.getBean("cassandraTemplate", CassandraOperations.class);
// for instance: load everything
List<MyData> rows = cOps.select("SELECT * FROM mydatatable", MyData.class);
// assuming an entry with index i exists...
Set<Pair> mySetOfPairs = rows.get(i).pairSet;
if (mySetOfPairs!=null)
for (Pair p : mySetOfPairs) {
... handle p.first and p.second ...
...

使用这种实体映射:
package example.model;
import java.util.Set;
import org.springframework.data.cassandra.core.mapping.CassandraType;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
import com.datastax.driver.core.DataType.Name;

@Table public class MyData {
@PrimaryKey
public String myKey;
// some other basic fields...
public String moreStuff;
// a SET of user defined 'pair type'
@CassandraType(type = Name.SET, userTypeName = "pairType")
public Set<Pair> pairSet;

// your constructors and other methods here...
}

和用户定义的实体,如:
package example.model;
import org.springframework.data.cassandra.core.mapping.UserDefinedType;

@UserDefinedType("pairType")
public class Pair {
public String first;
public String second;

public Pair() {
}
public Pair(String f, String s) {
this.first= f;
this.second= s;
}
}

全部基于创建为的 Cassandra 表:
CREATE TYPE pairType (first text, second text);

CREATE TABLE MyData (
myKey text,
moreStuff text,
pairSet set<frozen<pairType>>,
PRIMARY KEY (myKey)
) ;

INSERT INTO MyData (myKey, moreStuff, pairSet)
VALUES ('hello', 'world', {
{ first:'one', second:'two' },
{ first:'out', second:'there' } }
) ;

就 Maven 工件或库而言,如果您在 Web MVC Spring Controller 中运行,则确实需要 spring-webmvc,然后是 spring-context-support 和 spring-data-cassandra。 DataStax cassandra 驱动程序作为依赖项出现。

关于user-defined-types - UserTypeResolver 不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43310588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com