Spring数据Cassandra对UDT的注释

时间:2014-11-08 04:19:22

标签: user-defined-types spring-data-cassandra

springdatacassandra如何支持Cassandra在POJO中提供的用户定义数据类型?我正在寻找UDT的注释。

2 个答案:

答案 0 :(得分:1)

Spring数据cassandra最新版本(1.2.0.BUILD-SNAPSHOT)依赖于datastax驱动程序2.0.4,其中自数据存储驱动程序2.1.x以来支持UDT。

您可以尝试将datastax驱动程序覆盖到2.1.x以使用2.1驱动程序提供的最新功能。

答案 1 :(得分:1)

    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-mapping</artifactId>
        <version>2.1.9</version>
    </dependency>
需要

,但总的来说它不起作用,spring数据cassandra不支持UDT映射。

详情请见: https://jira.spring.io/browse/DATACASS-172

我遇到了这个问题,调试过程向我展示了spring数据cassandra检查 仅@Table,@ Persistent或@PrimaryKeyClass Annotation并在另一种情况下引发异常

  

&GT;   调用init方法失败;嵌套异常是org.springframework.data.cassandra.mapping.VerifierMappingExceptions:   Cassandra实体必须具有@ Table,@ Persistent或@PrimaryKeyClass Annotation

但我找到了解决方案。 我想出了允许我管理包含UDT的实体和不包含UDT的实体的方法。在我的应用程序中,我使用spring cassandra数据项目以及使用直接数据存储核心驱动程序。不包含UDT对象的存储库使用spring cassanta数据方法,包含UDT的对象使用自定义存储库。 自定义存储库使用datastax映射器,它们可以与UDT一起正常工作 (它们位于单独的包中,请参阅下面的注释,为什么需要它):

package com.fyb.cassandra.custom.repositories.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import com.datastax.driver.mapping.Result;
import com.google.common.collect.Lists;
import com.fyb.cassandra.custom.repositories.AccountDeviceRepository;
import com.fyb.cassandra.dto.AccountDevice;

public class AccountDeviceRepositoryImpl implements AccountDeviceRepository {

    @Autowired
    public CassandraSessionFactoryBean session;

    private Mapper<AccountDevice> mapper;

    @PostConstruct
    void initialize() {
        mapper = new MappingManager(session.getObject()).mapper(AccountDevice.class);
    }

    @Override
    public List<AccountDevice> findAll() {
        return fetchByQuery("SELECT * FROM account_devices");
    }

    @Override
    public void save(AccountDevice accountDevice) {
        mapper.save(accountDevice);
    }

    @Override
    public void deleteByConditions(UUID accountId, UUID systemId, UUID deviceId) {
        final String query = "DELETE FROM account_devices where account_id =" + accountId + " AND system_id=" + systemId
                + " AND device_id=" + deviceId;
        session.getObject().execute(query);
    }

    @Override
    public List<AccountDevice> findByAccountId(UUID accountId) {
        final String query = "SELECT * FROM account_devices where account_id=" + accountId;
        return fetchByQuery(query);
    }

    /*
     * Take any valid CQL query and try to map result set to the given list of appropriates <T> types.
     */
    private List<AccountDevice> fetchByQuery(String query) {
        ResultSet results = session.getObject().execute(query);
        Result<AccountDevice> accountsDevices = mapper.map(results);
        List<AccountDevice> result = Lists.newArrayList();
        for (AccountDevice accountsDevice : accountsDevices) {
            result.add(accountsDevice);
        }
        return result;
    }
}

与管理不包含UDT对象的实体相关的弹簧数据相关repos如下所示:

package com.fyb.cassandra.repositories;

import org.springframework.data.cassandra.repository.CassandraRepository;

import com.fyb.cassandra.dto.AccountUser;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface AccountUserRepository extends CassandraRepository<AccountUser> {

    @Query("SELECT * FROM account_users WHERE account_id=?0")
    List<AccountUser> findByAccountId(UUID accountId);
}

我已经测试了这个解决方案,它的工作效率为100%。 另外我附上了我的POJO对象:

只使用数据stax annatation的Pojo:

package com.fyb.cassandra.dto;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.datastax.driver.mapping.annotations.ClusteringColumn;
import com.datastax.driver.mapping.annotations.Column;
import com.datastax.driver.mapping.annotations.Frozen;
import com.datastax.driver.mapping.annotations.FrozenValue;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;

@Table(name = "account_systems")
public class AccountSystem {

    @PartitionKey
    @Column(name = "account_id")
    private java.util.UUID accountId;

    @ClusteringColumn
    @Column(name = "system_id")
    private java.util.UUID systemId;

    @Frozen
    private Location location;

    @FrozenValue
    @Column(name = "user_token")
    private List<UserToken> userToken;

    @Column(name = "product_type_id")
    private int productTypeId;

    @Column(name = "serial_number")
    private String serialNumber;   
}

Pojo没有使用UDT并且只使用spring数据cassandra框架:

package com.fyb.cassandra.dto;

import java.util.Date;
import java.util.UUID;

import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;

@Table(value = "accounts")
public class Account {

    @PrimaryKeyColumn(name = "account_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private java.util.UUID accountId;

    @Column(value = "account_name")
    private String accountName;

    @Column(value = "currency")
    private String currency;    
}

请注意,以下实体使用不同的注释:

@PrimaryKeyColumn(name = "account_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)and @PartitionKey
@ClusteringColumn and @PrimaryKeyColumn(name = "area_parent_id", ordinal = 2, type = PrimaryKeyType.CLUSTERED)

乍一看 - 它很不舒服,但是它允许你处理包含UDT但不包含UDT的对象。

一个重要的注意事项。那两个repos(使用UDT而不应该驻留在不同的包中)导致Spring配置寻找带有repos的基础包:

@Configuration
@EnableCassandraRepositories(basePackages = {
        "com.fyb.cassandra.repositories" })
public class CassandraConfig {
..........
}