Skip to content

Instantly share code, notes, and snippets.

@shahamit
Created July 1, 2016 17:41
Show Gist options
  • Save shahamit/561e638f02ec6a67c4185035d999fb41 to your computer and use it in GitHub Desktop.
Save shahamit/561e638f02ec6a67c4185035d999fb41 to your computer and use it in GitHub Desktop.
Includes code that tries to load an igniteCache<BinaryObject, BinaryObject>
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