Cassandra查询无法解析获取数据

时间:2018-11-01 04:47:38

标签: java spring cassandra datastax-enterprise

我的PersonRepository中有一个查询,如下所示:

 public interface PersonReposiroty extends CassandraRepository<Person>
    @Query("SELECT * FROM KeySp.person WHERE joinTime >= 21 ALLOW FILTERING")
    public List<Person> findByTime();
}

(其中keySp是我的键空间,person是表名)

我的人的实体是:

@Entity
@Table(name = "person", indexes = { @Index(name="peson_index", columnList = "joinTime, category, personId") })
public class Person {

    @Id
    @PrimaryKeyColumn
    private PersonContext context;

    @Column(name = "address", columnDefinition = "LONGTEXT")
    private String address;

    public PersonContext getContext() {
        return context;
    }

    public void setContext(PersonContext context) {
        this.context = context;
    }

    public String getAddress() {
        return adress;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    }

我的PersonContext是:

@Embeddable
@PrimaryKeyClass
public class PersonContext implements Serializable {

    private static final long serialVersionUID = 3L;

    @NotNull
    @PrimaryKeyColumn(name = "joinTime", type = PrimaryKeyType.PARTITIONED)
    private long joinTime;

    @NotNull
    @Column(name = "category")
    private String category;

    @Column(name = "person_id", length = 50)
    private String personId = StringUtils.EMPTY;

    public long getjoinTime() {
        return joinTime;
    }

    public void setjoinTime(long joinTime) {
        this.joinTime = joinTime;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(String personId) {
        this.personId = personId;
    }

}

StackTrace:

java.lang.NullPointerException
at org.springframework.data.cassandra.convert.MappingCassandraConverter.readProperty(MappingCassandraConverter.java:259)
at org.springframework.data.cassandra.convert.MappingCassandraConverter$1.doWithPersistentProperty(MappingCassandraConverter.java:233)
at org.springframework.data.cassandra.convert.MappingCassandraConverter$1.doWithPersistentProperty(MappingCassandraConverter.java:229)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:330)
at org.springframework.data.cassandra.convert.MappingCassandraConverter.readProperties(MappingCassandraConverter.java:229)
at org.springframework.data.cassandra.convert.MappingCassandraConverter.readPropertiesFromRow(MappingCassandraConverter.java:223)
at org.springframework.data.cassandra.convert.MappingCassandraConverter.readEntityFromRow(MappingCassandraConverter.java:190)
at org.springframework.data.cassandra.convert.MappingCassandraConverter.readRow(MappingCassandraConverter.java:176)
at org.springframework.data.cassandra.convert.MappingCassandraConverter.read(MappingCassandraConverter.java:298)
at org.springframework.data.cassandra.core.CassandraConverterRowCallback.doWith(CassandraConverterRowCallback.java:47)
at org.springframework.data.cassandra.core.CassandraTemplate.selectOne(CassandraTemplate.java:644)
at org.springframework.data.cassandra.core.CassandraTemplate.selectOne(CassandraTemplate.java:509)
at org.springframework.data.cassandra.repository.query.CassandraQueryExecution$SingleEntityExecution.execute(CassandraQueryExecution.java:104)
at org.springframework.data.cassandra.repository.query.CassandraQueryExecution$ResultProcessingExecution.execute(CassandraQueryExecution.java:143)
at org.springframework.data.cassandra.repository.query.AbstractCassandraQuery.execute(AbstractCassandraQuery.java:113)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy144.findByResource(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy144.findByResource(Unknown Source)
at MyProject.service.StatisticsCacheService.storeDataInternal(StatisticsCacheService.java:41)
at MyProject.service.CacheService$1.handleDelivery(CacheService.java:54)
at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:149)
at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:104)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

我认为问题是查询从数据库获取数据,但无法将其解析为我的Person实体,可能是因为Person具有复合主键PersonContext context。我之所以这样说,是因为如果我将PersonContext中的所有内容都添加到Person Entity中,则相同的查询可以正常工作。

cqlsh中的表Desc是:

CREATE TABLE haltdos.statistics (
    joinTime varint,
    category text,
    personid text,
    address text,
    PRIMARY KEY (joinTime, category, personid)
) WITH CLUSTERING ORDER BY (category ASC, personid ASC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';

我想使用PersonContext。请帮帮我 。 谢谢

我使用Spring Boot。

1 个答案:

答案 0 :(得分:0)

发现此问题的答案与我的实体定义有关。

所有详细信息都可以找到here