Create and query a binary cache in ignite -


i trying use binaryobjects create cache @ runtime. example, instead of writing pojo class such employee , configuring cache value type, need able dynamically configure cache field names , field types particular cache.

here sample code:

public class employeequery {  public static void main(string[] args) throws exception {     ignition.setclientmode(true);     try (ignite ignite = ignition.start("examples/config/example-ignite.xml")) {         if (!examplesutils.hasservernodes(ignite))             return;         cacheconfiguration<integer, binaryobject> cfg = getbinarycache("emplcache", 1);          ignite.destroycache(cfg.getname());         try (ignitecache<integer, binaryobject> emplcache = ignite.getorcreatecache(cfg)) {              sqlfieldsquery top5qry = new sqlfieldsquery("select * employee salary > 500 limit 5", true);             while (true) {                 querycursor<list<?>> top5qryresult = emplcache.query(top5qry);                      system.out.println(">>> employees ");                     list<list<?>> = top5qryresult.getall();                     (list<?> list : all) {                         system.out.println("top 5 query result : "+list.get(0) + " , "+ list.get(1) + " , " + list.get(2));                     }                     system.out.println("..... ");                 thread.sleep(5000);             }         }         {             ignite.destroycache(cfg.getname());         }     } }  private static queryentity createemployeequeryentity() {     queryentity employeeentity = new queryentity();     employeeentity.settablename("employee");     employeeentity.setvaluetype(binaryobject.class.getname());     employeeentity.setkeytype(integer.class.getname());     linkedhashmap<string, string> fields = new linkedhashmap<>();      fields.put("id", integer.class.getname());     fields.put("firstname", string.class.getname());     fields.put("lastname", string.class.getname());     fields.put("salary", float.class.getname());     fields.put("gender", string.class.getname());      employeeentity.setfields(fields);     employeeentity.setindexes(arrays.aslist(         new queryindex("id"),         new queryindex("firstname"),         new queryindex("lastname"),         new queryindex("salary"),         new queryindex("gender")     ));      return employeeentity; }  public static cacheconfiguration<integer, binaryobject> getbinarycache(string cachename, int duration) {     cacheconfiguration<integer, binaryobject> cfg = new cacheconfiguration<>(cachename);     cfg.setcachemode(cachemode.partitioned);     cfg.setname(cachename);     cfg.setstorekeepbinary(true);     cfg.setatomicitymode(cacheatomicitymode.atomic);     cfg.setindexedtypes(integer.class, binaryobject.class);     cfg.setexpirypolicyfactory(factorybuilder.factoryof(new createdexpirypolicy(new duration(seconds, duration))));     cfg.setqueryentities(arrays.aslist(createemployeequeryentity()));     return cfg; }  } 

i trying configure cache employeeid (integer) key , whole employee record (binaryobject) value. when run above class, following exception :

caused by: org.h2.jdbc.jdbcsqlexception: table "employee" not found; sql statement: select * "emplcache".employee salary > 500 limit 5 

what doing wrong here? there more other line:

employeeentity.settablename("employee"); 

next, trying stream data cache. right way it?

public class csvstreamer {  public static void main(string[] args) throws ioexception {     ignition.setclientmode(true);      try (ignite ignite = ignition.start("examples/config/example-ignite.xml")) {         if (!examplesutils.hasservernodes(ignite))             return;         cacheconfiguration<integer, binaryobject> cfg = employeequery.getbinarycache("emplcache", 1);         try (ignitedatastreamer<integer, binaryobject> stmr = ignite.datastreamer(cfg.getname())) {             while (true) {                 inputstream in = new fileinputstream(new file(args[0]));                 try (linenumberreader rdr = new linenumberreader(new inputstreamreader(in))) {                     int count =0;                     (string line = rdr.readline(); line != null; line = rdr.readline()) {                         string[] words = line.split(",");                         binaryobject emp = getbinaryobject(words);                          stmr.adddata(new integer(words[0]), emp);                         system.out.println("sent data "+count++ +" , sal : "+words[6]);                     }                 }             }         }     } }  private static binaryobject getbinaryobject(string[] rawdata) {     binaryobjectbuilder builder = ignition.ignite().binary().builder("employee");     builder.setfield("id", new integer(rawdata[0]));     builder.setfield("firstname", rawdata[1]);     builder.setfield("lastname", rawdata[2]);     builder.setfield("salary", new float(rawdata[6]));     builder.setfield("gender", rawdata[4]);     binaryobject binaryobj = builder.build();     return binaryobj; }  } 

note: running in cluster mode. both employeequery , csvstreamer run 1 machine, , have ignite running in server mode in 2 other machines. ideally want avoid use of pojo class in application , make things dynamic , generic possible.

you getting exception because didn't configure sql scheme. in case (you don't want create pojo object , etc) recommend use sql syntacsis added apache ignite since 2.0 version. sure following example helps configuration: https://github.com/apache/ignite/blob/master/examples/src/main/java/org/apache/ignite/examples/datagrid/cachequeryddlexample.java


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -