Skip to content

Instantly share code, notes, and snippets.

@shahamit
Last active July 1, 2016 17:47
Show Gist options
  • Save shahamit/60be5c35dc8b3eee8907e8fc416136cb to your computer and use it in GitHub Desktop.
Save shahamit/60be5c35dc8b3eee8907e8fc416136cb to your computer and use it in GitHub Desktop.
Includes code to load data into an Ignite<String, BinaryObject>. Data Load succeeds but iterating through the cache fails
public static CacheConfiguration<String, BinaryObject> getCacheConfigWithRandomIdAsKey(Table table) {
CacheConfiguration<String, 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);
}
public void loadTableWithRandomIdKey(Ignite ignite, Table table) {
CsvReader csvfile = null;
try (IgniteDataStreamer<String, BinaryObject> streamer = ignite.dataStreamer(table.getName())) {
csvfile = new CsvReader(FILE_PATH + table.getName() + ".csv");
csvfile.readHeaders();
BinaryObjectBuilder builder = ignite.binary().builder(table.getCacheValueType());
while (csvfile.readRecord()) {
for (Column col : table.getColumns()) {
builder.setField(col.getName(), csvfile.get(col.getName());
}
streamer.addData(UUID.randomUUID().toString(), builder.build());
}
} catch (IOException e) {
logger.error("Error while reading csv", e);
} finally {
if (csvfile != null) {
csvfile.close();
}
}
}
CacheConfiguration<String, BinaryObject> cfg = getCacheConfigWithRandomIdAsKey(table);
ignite.getOrCreateCache(cfg).withKeepBinary();
loadTableWithRandomIdKey(ignite, table);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment