定制Cassandra转换器

时间:2016-01-14 09:50:59

标签: spring spring-data spring-data-cassandra

我开始寻找一个很好的解决方案,如何使用Spring CassandraOperations很好地持久化实体。问题开始是因为我的实体中的某些字段不支持cassandra,例如joda DateTime。

解决方法是在java.util.Date类型的同一实体中使用其他字段而不是joda DateTime,使用@Transient标记不需要的字段。但这不干净,所以我开始寻找自动定制转换。

目前spring-data-cassandra参考不提供如何注册自定义转换器的信息。 http://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.custom-converters

是否可以在CassandraSessionFactoryBean中注册自定义转换器(如此处Spring Data Cassandra LocalDateTime Conversion)?

这是我的代码 @组态 公共课CassandraConfig {

@Value("${cassandra.contactpoints}")
private String cassandraContactPoint;

@Value("${cassandra.port}")
private int cassandraPort;

@Value("${cassandra.keyspace}")
private String cassandraKeySpace;

@Bean
public CassandraClusterFactoryBean cluster() {

    CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
    cluster.setContactPoints(cassandraContactPoint);
    cluster.setPort(cassandraPort);

    return cluster;
}

@Bean
public CassandraMappingContext mappingContext() {
    return new BasicCassandraMappingContext();
}

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

@Bean
public CassandraSessionFactoryBean session() throws Exception {
    CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
    session.setCluster(cluster().getObject());
    session.setKeyspaceName(cassandraKeySpace);
    session.setConverter(converter());
    session.setSchemaAction(SchemaAction.NONE);

    return session;
}

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

@Bean
public ConversionService getConversionService() {
    ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
    bean.setConverters(new HashSet<>(getConverters()));
    bean.afterPropertiesSet();

    return bean.getObject();
}

private Set<Converter<?, ?>> getConverters() {
    Set<Converter<?, ?>> converters = new HashSet<>();
    converters.addAll(Jsr310Converters.getConvertersToRegister());
    converters.add(new DateTimeWriteConverter());
    converters.add(new DateTimeReadConverter());

    return converters;
}

public static class DateTimeWriteConverter implements Converter<DateTime, Long> {
    @Override
    public Long convert(DateTime source) {
        return source.getMillis();
    }
}

public static class DateTimeReadConverter implements Converter<Long, DateTime> {
    @Override
    public DateTime convert(Long source) {
        return new DateTime(source);
    }
}

}

1 个答案:

答案 0 :(得分:2)

Spring Cassandra的自定义转换在您创建CassandraCustomConversions bean时起作用。只需添加自定义转换器,默认转换器就会保留。下面是我的@Configuration类。

@Bean
CassandraCustomConversions cassandraCustomConversions() {
    List<Converter<?, ?>> converters = new ArrayList<>();
    converters.add(new MyDataReadConverter());
    return new CassandraCustomConversions(converters);
}

static class CounterDataReadConverter implements Converter<ByteBuffer, MyData> {
    @Override
    public MyData convert(ByteBuffer source) {
        try {
            return MyData.parseFrom(source.array());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

我正在使用基于SpringBoot 2.0.0.RELEASE的spring-boot-starter-data-cassandra。