Apache Ignite javax.cache.CacheException: индексирование отключено для кеша

Когда я запускаю следующий код, выдается исключение Apache Ignite javax.cache.CacheException: индексирование отключено для кеша. Не уверен, в чем проблема. Может ли кто-нибудь помочь? Спасибо!

package com.xyz.ignite.sqlgrid;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;

import javax.cache.Cache;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Arrays;


import org.apache.ignite.cache.query.annotations.QuerySqlField;

class Person {
    @QuerySqlField
    private String name;

    @QuerySqlField
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class IgniteSQLQueryTest {
    public static void main(String[] args) throws Exception {
        String configPath = "D:/apache-ignite-fabric-1.7.0-bin/config/default-config.xml";
        Ignite ignite = Ignition.start(configPath);
        CacheConfiguration<Integer, Person> cfg = new CacheConfiguration<Integer, Person>();
        cfg.setName("person_cache");
        cfg.setIndexedTypes(Integer.class, Person.class);
        Cache cache = ignite.getOrCreateCache(cfg);
        String cacheName = cache.getName();

        for (int i = 1; i <= 100; i++) {
            Person p = new Person();
            p.setAge(i);
            p.setName("name-" + i);
            cache.put(i, p);
        }
        for (int i = 1; i <= 100; i++) {
            System.out.println("Cache get:" + cache.get(i));
        }

        Class.forName("org.apache.ignite.IgniteJdbcDriver");

        Connection conn = DriverManager.getConnection(String.format("jdbc:ignite:cfg://cache=%s@file:///%s", cacheName, configPath));
        ResultSet rs = conn.createStatement().executeQuery("select name from Person");

        while (rs.next()) {
            String name1 = rs.getString(1);
            System.out.println(name1);
        }

        ignite.close();
    }
}

произошло исключение, сообщение об исключении:

Exception in thread "main" java.sql.SQLException: Failed to query Ignite.
    at org.apache.ignite.internal.jdbc2.JdbcStatement.executeQuery(JdbcStatement.java:115)
    at com.xyz.ignite.sqlgrid.IgniteSQLQueryTest.main(IgniteSQLQueryTest.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:88)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
    at java.lang.reflect.Method.invoke(Method.java:613)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: javax.cache.CacheException: Indexing is disabled for cache: person_cache. Use setIndexedTypes or setTypeMetadata methods on CacheConfiguration to enable.
    at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.validate(IgniteCacheProxy.java:732)
    at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:664)
    at org.apache.ignite.internal.jdbc2.JdbcQueryTask.call(JdbcQueryTask.java:158)
    at org.apache.ignite.internal.jdbc2.JdbcStatement.executeQuery(JdbcStatement.java:102)

person Tom    schedule 07.12.2016    source источник


Ответы (1)


Похоже, я нашел, в чем проблема. Человек Pojo должен быть

class Person implements Serializable {
    @QuerySqlField(index = true)
    private String name;

    @QuerySqlField(index = true)
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Аннотация QuerySqlField должна добавить параметр index = true

person Tom    schedule 07.12.2016
comment
Этот параметр только определяет, создается ли индекс для определенного поля. Если он равен false или не установлен, поле не будет проиндексировано, но будет доступно для запросов. Сказав это, я сомневаюсь, что это было причиной исключения. Можете ли вы предоставить свой тест (в идеале в виде небольшого проекта GitHub), чтобы я мог его воспроизвести и исследовать? - person Valentin Kulichenko; 12.12.2016