如何检索Apache在JAVA中以排序格式点燃缓存的Cassandra表?

时间:2018-01-28 15:56:56

标签: ignite

我使用spring-boot代码从Cassandra表中加载名为“test”的数据。我已经定义了组索引并提到了持久性的顺序,但是当我尝试使用“findAll()”方法获取所有记录时,数据不是以排序的方式进行的。 请让我知道我走错了路。因为我不确定如何以排序的方式检索缓存表数据。

cassandra表:

create table test(key int primary key, city text, location text, subscribers int);

java对象:

public class Test {

    @QuerySqlField(orderedGroups = { @QuerySqlField.Group(name = "city_subscriber_location", order = 0) })
    String city;
    @QuerySqlField(orderedGroups = {
            @QuerySqlField.Group(name = "city_subscriber_location", order = 1, descending = true) })
    int subscribers;
    @QuerySqlField(orderedGroups = { @QuerySqlField.Group(name = "city_subscriber_location", order = 2) })
    String location;
}

xml文件:

<persistence keyspace="ignite" table="test" ttl="86400">
    <keyspaceOptions>
        REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1}
        AND DURABLE_WRITES = true
    </keyspaceOptions>
    <tableOption>
        comment = 'Cache test'
        AND read_repair_chance = 0.2
    </tableOption>
    <keyPersistence class="java.lang.Integer" strategy="PRIMITIVE" column="key"/>
    <valuePersistence class="com.cache.business.model.Test" strategy="POJO"/>
</persistence>

配置:

public Ignite igniteInstance() {
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setIgniteInstanceName("ignite-1");
    cfg.setPeerClassLoadingEnabled(true);
    cfg.setMetricsLogFrequency(0);

    DataSource dataSource = new DataSource();

    dataSource.setContactPoints(environment.getProperty("cassandra.contact-points"));
    RoundRobinPolicy robinPolicy = new RoundRobinPolicy();
    dataSource.setLoadBalancingPolicy(robinPolicy);
    dataSource.setReadConsistency("ONE");
    dataSource.setWriteConsistency("ONE");

    CacheConfiguration<Integer, Test> ccfg2 = new CacheConfiguration<>("test");// table name
    ccfg2.setIndexedTypes(Integer.class, Test.class);
    //ccfg2.setWriteBehindEnabled(true);
    ccfg2.setReadThrough(true);
    ccfg2.setWriteThrough(true);

    CassandraCacheStoreFactory<Integer, Test> f2 = new CassandraCacheStoreFactory<>();
    f2.setDataSource(dataSource);

    KeyValuePersistenceSettings persistenceSettings = new KeyValuePersistenceSettings(
            new ClassPathResource("test.xml"));// corresponding xml file

    f2.setPersistenceSettings(persistenceSettings);

    ccfg2.setCacheStoreFactory(f2);

    cfg.setCacheConfiguration(ccfg, ccfg2);

    /**
     * Start Ignite server and load cache for all the cache configurations
     */
    Ignite ignite = Ignition.start(cfg);

    IgniteCache<Integer, Test> cache = ignite.getOrCreateCache("test");

    cache.loadCache(null);

    return ignite;
}

输出:

[
  {
    "city": "mumbai",
    "subscribers": 986,
    "location": "marthalli"
  },
  {
    "city": "mumbai",
    "subscribers": 427,
    "location": "centra"
  },
  {
    "city": "mumbai",
    "subscribers": 739,
    "location": "krmarket"
  },
  {
    "city": "hyd",
    "subscribers": 637,
    "location": "krmarket"
  },
  {
    "city": "bgr",
    "subscribers": 575,
    "location": "krpuram"
  }

我没有以排序格式获得输出。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

我们不应该使用findAll方法点燃存储库。

使用以下方式查询

@Query("SELECT subscribers FROM test where city = ?")
    List<Integer> selectAll(String city);