Skip to content

Instantly share code, notes, and snippets.

@sumanentc
Created October 29, 2021 17:53
Show Gist options
  • Save sumanentc/1f42cbcfd1508732b01754780017b22f to your computer and use it in GitHub Desktop.
Save sumanentc/1f42cbcfd1508732b01754780017b22f to your computer and use it in GitHub Desktop.
public List<Player> sqlQuerySearch(String text) {
log.info("SqlQuery search to find players whose team contain the given text");
IgniteCache<Long, Player> playerCache = Ignition.ignite().getOrCreateCache(PLAYER);
Collection<QueryEntity> queryEntities = playerCache.getConfiguration(CacheConfiguration.class).getQueryEntities();
String tableName = queryEntities.stream().findFirst().orElseThrow(() -> new IllegalArgumentException(PLAYER + "doesn't exists")).getTableName();
SqlFieldsQuery sqlQuery = new SqlFieldsQuery("select * from " + tableName + " where team='" + text + "'");
log.info("Query :: " + sqlQuery.getSql());
List<List<?>> records = playerCache.query(sqlQuery).getAll();
return records.stream().map(objects -> convert((List<Object>) objects)).collect(Collectors.toList());
}
private Player convert(List<Object> values) {
return Player.builder().id((Long) values.get(0)).name(String.valueOf(values.get(1)))
.team(String.valueOf(values.get(2)))
.salary(Double.parseDouble(values.get(3).toString())).build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment