Search code examples
javaigniteapacheignite

Data not getting cached while implementing Apache ignite as IMDb


I want to implement Apache ignite as in memory database so that the data retrieval will be faster. I've tried implementing the Key-Value API of Apache Ignite. But as my requirement got changed, I'm trying to implement using SQL API.

This is my server code:

public class ImdbApplication {

    public static void main(String[] args) {

        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setIgniteInstanceName("Instance");
        cfg.setConsistentId("NodePoswavier");

        TcpCommunicationSpi commSpi = new TcpCommunicationSpi();

        TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();
        TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
        ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500..47509"));
        discoSpi.setIpFinder(ipFinder);
        cfg.setDiscoverySpi(discoSpi);

        Ignition.start();

        CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>("personCache");

        QueryEntity queryEntity = new QueryEntity();
        queryEntity.setKeyFieldName("id").setKeyType(Long.class.getName()).setValueType(Person.class.getName());

        LinkedHashMap<String, String> fields = new LinkedHashMap<>();
        fields.put("id", "java.lang.Long");
        fields.put("name", "java.lang.String");
        fields.put("salary", "java.lang.Float");

        queryEntity.setFields(fields);

        queryEntity.setIndexes(Arrays.asList(
                new QueryIndex("name"),
                new QueryIndex(Arrays.asList("id", "salary"), QueryIndexType.SORTED)
        ));

        cacheCfg.setQueryEntities(Arrays.asList(queryEntity));

        // Get or create cache
        Ignite ignite = Ignition.ignite();
        IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheCfg);


    }
}

My client side code is:

public class ImdbClientApplication {

    public static void main(String[] args) {
        // Register the Ignite JDBC driver.
        try {
            Class.forName("org.apache.ignite.IgniteJdbcDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        Ignite ignite = Ignition.start();

        IgniteCache<Long, Person> cache = ignite.cache("personCache");

        // Open a JDBC connection to the source database.
        try (Connection sourceConn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/person", "root", "root")) {
            // Execute a SELECT query to fetch data from the source database.
            PreparedStatement sourceStmt = sourceConn.prepareStatement("SELECT id, name, salary FROM person");
            ResultSet rs = sourceStmt.executeQuery();

            while (rs.next()) {
                long id = rs.getLong("id");
                String name = rs.getString("name");
                int salary = rs.getInt("salary");

                cache.query(new SqlFieldsQuery("INSERT INTO personCache(id, firstName, lastName) VALUES(?, ?, ?)")
                                .setArgs(id, name, salary))
                        .getAll();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

When I run this client, the SELECT statement, it works fine. But that INSERT statement throws :

javax.cache.CacheException: Failed to parse query. Table "PERSONCACHE" not found; SQL statement:

What am I doing wrong?


Solution

  • You almost answer it yourself. Compare your queries:

            PreparedStatement sourceStmt = sourceConn.prepareStatement("SELECT id, name, salary FROM person");
            
            cache.query(new SqlFieldsQuery("INSERT INTO personCache(id, firstName, lastName) VALUES(?, ?, ?)")
                                .setArgs(id, name, salary))
    

    Note that you use a different table name. In the SELECT, you use person (which is correct) and in the INSERT you use personCache (which isn't). Update your second query to use the person table and it should work.