public int save(String tableName, Map<String, Object> dataMap) throws IllegalAccessException {
SimpleStatement saveStatement = QueryBuilder.insertInto(tableName).values()
return 1;
}
I have tried other inbuilt methods of QueryBuilder to save map values as a whole but only a map of type map<String,Term> can be used to save data if "values()" method is used and I already have a map<String,Object>.This needs to be done for a dynamic table p.s. I am not that much familiar with Dynamic Tables. The other method I tried using with a return type integer was
public int save(String tableName, Map<String, Object> dataMap) throws IllegalAccessException {
SimpleStatement updateStatement = QueryBuilder.update(tableName)
.set(appendSet(dataMap))
.where(appendWhere(domainId))
.build();
log.info(updateStatement.getQuery());
return 1;
}
private Iterable<Assignment> appendSet(Map<String, Object> dataMap) throws IllegalAccessException {
List<Assignment> assignments = new ArrayList<>();
for (Field field : dataMap.getClass().getDeclaredFields()) {
if (!field.getName().equals("key")) {
try {
field.setAccessible(true);
if (field.get(dataMap) != null) {
if (field.getType().equals(Long.class)) {
assignments.add(Assignment.setColumn(field.getName(), literal(Long.valueOf(field.get(dataMap).toString()))));
} else {
assignments.add(Assignment.setColumn(field.getName(), literal(field.get(dataMap))));
}
}
} catch (IllegalAccessException e) {
log.catching(e);
}
}
}
return assignments;
}
private Iterable<Relation> appendWhere(Object key) {
List<Relation> relations = new ArrayList<>();
for (Field field : key.getClass().getDeclaredFields()) {
try {
field.setAccessible(true);
if (field.get(key) != null) {
if (field.getType().equals(Long.class)) {
relations.add(Relation.column(field.getName()).isEqualTo(literal(Long.valueOf(field.get(key).toString()))));
} else {
relations.add(Relation.column(field.getName()).isEqualTo(literal(field.get(key))));
}
}
} catch (IllegalAccessException e) {
log.catching(e);
}
}
return relations;
}
This probably didn't work out too. I need to return an integer with the save method but I just cannot figure out how to insert map values into the cassandra table itself using QueryBuilders or CassandraTemplate. I am working with Cassandra Database and my table is dynamic. Can anyone suggest me a good article or anything? I found articles but those did not help me insert map key value pairs into the table so I am quite struggling. Any help would be appreciated.