Spring数据Cassandra对UDT的注释



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

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

您可以尝试将数据税驱动程序重写为2.1.x,以使用2.1驱动程序提供的最新功能。

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

是必需的,但总的来说,它不起作用,春季数据cassandra不支持UDT映射。

查看此处的详细信息:https://jira.spring.io/browse/DATACASS-172

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

>init方法调用失败;嵌套异常为org.springframework.data.cassandra.maping.VerifierMappingException:Cassandra实体必须具有@Table、@Persistent或@PrimaryKeyClass Annotation

但我找到了解决办法。我找到了允许我管理包括UDT和不包括UDT的实体的方法。在我的应用程序中,我使用spring-cassandra数据项目和直接数据税核心驱动程序。不包含UDT对象的存储库使用spring-cassanta数据方法,而包含UDT的对象使用自定义存储库。自定义存储库使用数据税映射器,并且可以正确地与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对象的实体的春季数据相关repo如下所示:

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对象:

仅使用数据存储的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的对象。

一个重要的注意事项。这两个repo(使用UDT,不应该位于不同的包中)导致Spring config寻找带有repo:的基本包

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

相关内容

  • 没有找到相关文章

最新更新