I'm trying to use Binary Marshaller to replace old OptimizedMarshaller which
is really a pain for deployment.
But according to document of Type Metadata, it could be changed at runtime,
that means if I access to a 3rd party cache which was created by someone
else without explicit type settings, I'll have to get a sample row to
retrieve its type manually prior to further operations. is this correct? why
not just set a default optional meta type in cache level? as an option, it
won't be negative for dynamic row schema.



#access to a cache 'SQL_PUBLIC_CITY' (created by JDBC 'Create Table'
statement) and do some normal operations.
#GetSchema() is weird

public class App 
{
        private static SchemaType schema=null;
    public static void main( String[] args )
    {
        Ignite ignite =
Ignition.start("C://apache//ignite//apache-ignite-fabric-2.2.0-bin//config//client.xml");
       
        CacheConfiguration<BinaryObject, BinaryObject> cfg = new
CacheConfiguration<BinaryObject, BinaryObject>("SQL_PUBLIC_CITY");
        IgniteCache<BinaryObject, BinaryObject> cache =
ignite.getOrCreateCache(cfg).withKeepBinary();
        
        if(schema==null) schema=GetSchema(cache);
        Put(ignite,cache,4L,"Los Angeles");
        GetAll(cache);
        Get(ignite,cache,4L);
        Query(cache,4L);
        ignite.close();
    }
    
    public static void GetAll(IgniteCache<BinaryObject, BinaryObject> cache)
{
        Iterator<Cache.Entry&lt;BinaryObject, BinaryObject>>  itr =
cache.iterator();
        while(itr.hasNext()){
                Cache.Entry<BinaryObject, BinaryObject> item = itr.next();
                System.out.println("id="+item.getKey().field("id")+" 
KeyType="+item.getKey().type().typeName().toString()+" 
V="+item.getKey().type().field("id"));
                System.out.println("CITY="+item.getValue().field("name")+ "  of
id="+item.getValue().field("id"));
        }
    }
    
    public static SchemaType GetSchema(IgniteCache<BinaryObject,
BinaryObject> cache) {
        SchemaType sch=new SchemaType();
                Cache.Entry<BinaryObject, BinaryObject> item = 
cache.iterator().next();
                sch.KeyType=item.getKey().type().typeName();
                sch.KeyFields=item.getKey().type().fieldNames();
                sch.ValueType=item.getValue().type().typeName();
                sch.ValueFields=item.getValue().type().fieldNames();
        return sch;
    }
    
    
    
    public static void Get(Ignite ignite, IgniteCache<BinaryObject,
BinaryObject> cache, Long CityId) {
        BinaryObjectBuilder keyBuilder =
ignite.binary().builder(schema.KeyType)
                .setField("id", CityId);
        
        BinaryObject value = cache.get(keyBuilder.build());
        if(value!=null) System.out.println("CITY="+value.field("name"));
    }
    
    public static void Put(Ignite ignite, IgniteCache<BinaryObject,
BinaryObject> cache,Long CityId,String CityName) {
        BinaryObjectBuilder keyBuilder =
ignite.binary().builder(schema.KeyType)
                .setField("id", CityId);
        BinaryObjectBuilder valueBuilder =
ignite.binary().builder(schema.ValueType)
                .setField("name", CityName);
        
        cache.put(keyBuilder.build(),valueBuilder.build());
    }
    
    public static void Query(IgniteCache<BinaryObject, BinaryObject> cache,
Long CityId) {
        QueryCursor<List&lt;?>> query = cache.query(new
SqlFieldsQuery("select * from City where id="+CityId));
        System.out.println(query.getAll());
    }
}






--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Reply via email to