'Apache Ignite No data inserted while using cache.put method with BinaryObjects

After going through several posts on getting and putting data into a SQL table in appche ignite with the help of BinaryObjects I wrote below code for a test. The code run successfully without any error but no data get inserted using BinaryObjects.

It is puzzling why cache.put is not working when BinaryObjects are used


import java.io.Serializable;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.binary.BinaryObject;
import org.apache.ignite.binary.BinaryObjectBuilder;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
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.vm.TcpDiscoveryVmIpFinder;

public class BinaryPutExample {
    private static void print(String msg) {
        System.out.println();
        System.out.println(">>> " + msg);
    }
    private static final String DUMMY_CACHE_NAME = "dummy_cache";
    

    public static void putBinaryData() {
        IgniteConfiguration cfg= new IgniteConfiguration();
        cfg.setPeerClassLoadingEnabled(true);
        Ignition.setClientMode(true);
        TcpDiscoverySpi discoverySpi = new TcpDiscoverySpi();
        TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryVmIpFinder();
        
        ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500..47509"));
        
        discoverySpi.setIpFinder(ipFinder);
        
        discoverySpi.setJoinTimeout(1000);
        discoverySpi.setClientReconnectDisabled(false);
        
        cfg.setDiscoverySpi(discoverySpi);
        try(Ignite ignite = Ignition.getOrStart(cfg)){
            CacheConfiguration<?, ?> cacheCfg = new CacheConfiguration<>(DUMMY_CACHE_NAME).setSqlSchema("PUBLIC");

            try (
                IgniteCache<?, ?> cache = ignite.getOrCreateCache(cacheCfg)
            ) {
                // Create reference City table based on REPLICATED template.
                cache.query(new SqlFieldsQuery(
                    "CREATE TABLE city (id BIGINT PRIMARY KEY, name VARCHAR) WITH \"template=replicated, CACHE_NAME=City\"")).getAll();

                // Create table based on PARTITIONED template with one backup.
                cache.query(new SqlFieldsQuery(
                    "CREATE TABLE person (id BIGINT, name VARCHAR, city_id BIGINT, PRIMARY KEY (id, city_id)) " +
                    "WITH \"backups=1, affinity_key=city_id, CACHE_NAME=Person\"")).getAll();

                // Create an index.
                cache.query(new SqlFieldsQuery("CREATE INDEX person_idx ON Person (city_id)")).getAll();

                print("Created database objects.");

                SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO city (id, name) VALUES (?, ?)");

                cache.query(qry.setArgs(1L, "Forest Hill")).getAll();
                cache.query(qry.setArgs(2L, "Denver")).getAll();
                cache.query(qry.setArgs(3L, "St. Petersburg")).getAll();

                qry = new SqlFieldsQuery("INSERT INTO person (id, name, city_id) values (?, ?, ?)");

                cache.query(qry.setArgs(1L, "John Doe", 3L)).getAll();
                cache.query(qry.setArgs(2L, "Jane Roe", 2L)).getAll();
                cache.query(qry.setArgs(3L, "Mary Major", 1L)).getAll();
                cache.query(qry.setArgs(4L, "Richard Miles", 2L)).getAll();

                print("Populated data.");

                List<List<?>> res = cache.query(new SqlFieldsQuery(
                    "SELECT p.name, c.name FROM Person p INNER JOIN City c on c.id = p.city_id")).getAll();

                print("Query results:");

                for (Object next : res)
                    System.out.println(">>>    " + next);

                print("test binaries");

                

                
                IgniteCache<BinaryObject, BinaryObject> bincache = ignite.cache("Person").withKeepBinary();

                
                
                print("put some data using BinaryObject");
                

                bincache.put(createPersonKey(5, 3,ignite), createPersonVal("person5",ignite));
                bincache.put(createPersonKey(6, 2,ignite), createPersonVal("person6",ignite));
                bincache.put(createPersonKey(7, 1,ignite), createPersonVal("person7",ignite));
                

                print("Query results after binary addition :");
                res = cache.query(new SqlFieldsQuery(
                    "SELECT p.name, c.name FROM Person p INNER JOIN City c on c.id = p.city_id")).getAll();
         

                for (Object next : res)
                    System.out.println(">>>    " + next);

                print("Bin Query results after binary addition :");
                SqlFieldsQuery sql = new SqlFieldsQuery(
                    "select _key,_val from Person ");
                
                try (QueryCursor<List<?>> cursor = bincache.query(sql)) {
                    for (List<?> row :  cursor) {
                        BinaryObject p = (BinaryObject) row.get(0);
                        System.out.println(p);
                    }
                } 



                cache.query(new SqlFieldsQuery("drop table Person")).getAll();
                cache.query(new SqlFieldsQuery("drop table City")).getAll();

                print("Dropped database objects.");
            }
            finally {
                // Distributed cache can be removed from cluster only by #destroyCache() call.
                ignite.destroyCache(DUMMY_CACHE_NAME);
            }
            
        };
        
    }
    private static BinaryObject createPersonKey(Integer id, Integer city_id, Ignite ignite) {
        BinaryObjectBuilder builder = ignite.binary().builder("key-type1");
        builder.setField("id", id);
        builder.setField("city_id", city_id);
        return builder.build();
    }
    private static BinaryObject createPersonVal(String name,Ignite ignite) {
        BinaryObjectBuilder builder = ignite.binary().builder("value-type1");
        builder.setField("name", name);
        
        return builder.build();
    }
    
}


Solution 1:[1]

I resoved it by Editing my create table statment as below

cache.query(new SqlFieldsQuery("CREATE TABLE person (id BIGINT, name VARCHAR, city_id BIGINT, PRIMARY KEY (id, city_id)) " + "WITH \"backups=1, affinity_key=city_id, CACHE_NAME=Person,key_type=PersonKey, value_type=MyPerson\"")).getAll();

notice I now assigned custom names to Key and Value

I also fixed issues with function createPersonKey as below

private static BinaryObject createPersonKey(Long id, Long city_id, Ignite ignite) {
        BinaryObjectBuilder builder = ignite.binary().builder("PersonKey");
        builder.setField("id", id);
        builder.setField("city_id", city_id);
        return builder.build();
    } 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kashif sayeed