Created
July 1, 2016 17:41
-
-
Save shahamit/561e638f02ec6a67c4185035d999fb41 to your computer and use it in GitHub Desktop.
Includes code that tries to load an igniteCache<BinaryObject, BinaryObject>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void loadTableWithCustomKeyColumns(Ignite ignite, Table table) { | |
CsvReader csvfile = null; | |
try (IgniteDataStreamer<BinaryObject, BinaryObject> streamer = ignite.dataStreamer(table.getName())) { | |
csvfile = new CsvReader(FILE_PATH + table.getName() + ".csv"); | |
csvfile.readHeaders(); | |
BinaryObjectBuilder keyBuilder = ignite.binary().builder(table.getCacheValueType()); | |
BinaryObjectBuilder valueBuilder = ignite.binary().builder(table.getCacheValueType()); | |
while (csvfile.readRecord()) { | |
List<Object> keyValues = Lists.newArrayList(); | |
for (Column col : table.getColumns()) { | |
if(col.isKeyColumn()) { | |
Object keyColValue = getValue(col, csvfile.get(col.getName())); | |
keyValues.add(keyColValue); | |
keyBuilder.setField(col.getName(), keyColValue); | |
} else { | |
valueBuilder.setField(col.getName(), getValue(col, csvfile.get(col.getName()))); | |
} | |
} | |
keyBuilder.hashCode(JdbcTypeDefaultHasher.INSTANCE.hashCode(keyValues)); | |
streamer.addData(keyBuilder.build(), valueBuilder.build()); | |
} | |
} catch (IOException e) { | |
logger.error("Error while reading csv", e); | |
} finally { | |
if (csvfile != null) { | |
csvfile.close(); | |
} | |
} | |
} | |
public static CacheConfiguration<BinaryObject, BinaryObject> getAffinityBasedCacheConfig(Table table) { | |
CacheConfiguration<BinaryObject, BinaryObject> cfg = new CacheConfiguration<>(); | |
cfg.setName(table.getName()); | |
cfg.setCacheMode(table.getCacheMode()); | |
setQueryEntities(table, cfg); | |
return cfg; | |
} | |
private static void setQueryEntities(Table table, CacheConfiguration cfg) { | |
QueryEntity qe = new QueryEntity(); | |
qe.setKeyType(String.class.getCanonicalName()); | |
qe.setValueType(table.getCacheValueType()); | |
LinkedHashMap<String, String> columns = new LinkedHashMap<>(); | |
for (Column c : table.getColumns()) { | |
columns.put(c.getName(), c.getDataType().getJavaType().getCanonicalName()); | |
} | |
qe.setFields(columns); | |
ArrayList<QueryEntity> qryEntities = Lists.newArrayList(qe); | |
cfg.setQueryEntities(qryEntities); | |
} | |
CacheConfiguration<BinaryObject, BinaryObject> cfg = getAffinityBasedCacheConfig(table); | |
ignite.getOrCreateCache(cfg).withKeepBinary(); | |
loadTableWithCustomKeyColumns(ignite, table); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment