@Override protected void doHealthCheck(Health.Builder builder) throws Exception { Select select = QueryBuilder.select("release_version").from("system", "local"); ResultSet results = this.cassandraOperations.getCqlOperations() .queryForResultSet(select); if (results.isExhausted()) { builder.up(); return; } String version = results.one().getString(0); builder.up().withDetail("version", version); }
/** * Create tables from types known to {@link CassandraMappingContext}. * * @param ifNotExists {@literal true} to create tables using {@code IF NOT EXISTS}. */ public void createTables(boolean ifNotExists) { createTableSpecifications(ifNotExists).stream() // .map(CreateTableCqlGenerator::toCql) // .forEach(cql -> this.cassandraAdminOperations.getCqlOperations().execute(cql)); }
@Override public <T> List<T> select(Statement statement, Class<T> entityClass) { Assert.notNull(statement, "Statement must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); Function<Row, T> mapper = getMapper(entityClass, entityClass, QueryUtils.getTableName(statement)); return getCqlOperations().query(statement, (row, rowNum) -> mapper.apply(row)); }
long doCount(Query query, Class<?> entityClass, CqlIdentifier tableName) { RegularStatement countStatement = getStatementFactory().count(query, getRequiredPersistentEntity(entityClass), tableName); Long count = getCqlOperations().queryForObject(countStatement, Long.class); return count != null ? count : 0L; }
@Override public long count(Class<?> entityClass) { Assert.notNull(entityClass, "Entity type must not be null"); Select select = QueryBuilder.select().countAll().from(getTableName(entityClass).toCql()); Long count = getCqlOperations().queryForObject(select, Long.class); return count != null ? count : 0L; }
@Override public Object execute(Statement statement, Class<?> type) { return operations.getCqlOperations().queryForResultSet(statement); } }
/** * Create user types from types known to {@link CassandraMappingContext}. * * @param ifNotExists {@literal true} to create types using {@code IF NOT EXISTS}. */ public void createUserTypes(boolean ifNotExists) { createUserTypeSpecifications(ifNotExists).stream() // .map(CreateUserTypeCqlGenerator::toCql) // .forEach(cql -> this.cassandraAdminOperations.getCqlOperations().execute(cql)); }
<T> List<T> doSelect(Query query, Class<?> entityClass, CqlIdentifier tableName, Class<T> returnType) { Function<Row, T> mapper = getMapper(entityClass, returnType, tableName); RegularStatement select = getStatementFactory().select(query, getRequiredPersistentEntity(entityClass), tableName); return getCqlOperations().query(select, (row, rowNum) -> mapper.apply(row)); }
@Override public WriteResult execute() { if (executed.compareAndSet(false, true)) { return WriteResult.of(operations.getCqlOperations().queryForResultSet(batch)); } throw new IllegalStateException("This Cassandra Batch was already executed"); }
/** * Executes the given Cassandra CQL scripts. The {@link Session} must be connected when this method is called. */ protected void executeScripts(List<String> scripts) { if (!CollectionUtils.isEmpty(scripts)) { CqlOperations template = newCqlOperations(getSession()); scripts.forEach(script -> { logger.info("executing raw CQL [{}]", script); template.execute(script); }); } }
@Override public <T> T selectOneById(Object id, Class<T> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CqlIdentifier tableName = getTableName(entityClass); Select select = QueryBuilder.select().all().from(tableName.toCql()); getConverter().write(id, select.where(), getRequiredPersistentEntity(entityClass)); Function<Row, T> mapper = getMapper(entityClass, entityClass, tableName); List<T> result = getCqlOperations().query(select, (row, rowNum) -> mapper.apply(row)); return result.isEmpty() ? null : result.get(0); }
@Override public <T> Stream<T> stream(Statement statement, Class<T> entityClass) throws DataAccessException { Assert.notNull(statement, "Statement must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); ResultSet resultSet = getCqlOperations().queryForResultSet(statement); return StreamSupport.stream(resultSet.spliterator(), false) .map(getMapper(entityClass, entityClass, QueryUtils.getTableName(statement))); }
/** * Create indexes from types known to {@link CassandraMappingContext}. * * @param ifNotExists {@literal true} to create tables using {@code IF NOT EXISTS}. */ public void createIndexes(boolean ifNotExists) { createIndexSpecifications(ifNotExists).stream() // .map(CreateIndexCqlGenerator::toCql) // .forEach(cql -> this.cassandraAdminOperations.getCqlOperations().execute(cql)); }
@Override public Object execute(Statement statement, Class<?> type) { ResultSet resultSet = this.operations.getCqlOperations().queryForResultSet(statement); Iterator<Row> iterator = resultSet.iterator(); if (iterator.hasNext()) { Row row = iterator.next(); if (!iterator.hasNext() && ProjectionUtil.qualifiesAsCountProjection(row)) { Object object = row.getObject(0); return ((Number) object).longValue() > 0; } return true; } return false; } }
@Override public boolean update(Query query, org.springframework.data.cassandra.core.query.Update update, Class<?> entityClass) throws DataAccessException { Assert.notNull(query, "Query must not be null"); Assert.notNull(update, "Update must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); Statement updateStatement = getStatementFactory().update(query, update, getRequiredPersistentEntity(entityClass)); return getCqlOperations().execute(updateStatement); }
@Override public <T> Slice<T> slice(Statement statement, Class<T> entityClass) { Assert.notNull(statement, "Statement must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); ResultSet resultSet = getCqlOperations().queryForResultSet(statement); Function<Row, T> mapper = getMapper(entityClass, entityClass, QueryUtils.getTableName(statement)); return QueryUtils.readSlice(resultSet, (row, rowNum) -> mapper.apply(row), 0, getEffectiveFetchSize(statement)); }
@Override public void dropUserType(CqlIdentifier typeName) { Assert.notNull(typeName, "Type name must not be null"); String dropUserTypeCql = DropUserTypeCqlGenerator.toCql(DropUserTypeSpecification.dropType(typeName)); getCqlOperations().execute(dropUserTypeCql); }
<T> Stream<T> doStream(Query query, Class<?> entityClass, CqlIdentifier tableName, Class<T> returnType) { RegularStatement statement = getStatementFactory().select(query, getRequiredPersistentEntity(entityClass), tableName); ResultSet resultSet = getCqlOperations().queryForResultSet(statement); return StreamSupport.stream(resultSet.spliterator(), false).map(getMapper(entityClass, returnType, tableName)); }
@Override public Optional<TableMetadata> getTableMetadata(String keyspace, CqlIdentifier tableName) { Assert.hasText(keyspace, "Keyspace name must not be empty"); Assert.notNull(tableName, "Table name must not be null"); return Optional.ofNullable(getCqlOperations().execute((SessionCallback<TableMetadata>) session -> session .getCluster().getMetadata().getKeyspace(keyspace).getTable(tableName.toCql()))); }
boolean doExists(Query query, Class<?> entityClass, CqlIdentifier tableName) { RegularStatement select = getStatementFactory().select(query.limit(1), getRequiredPersistentEntity(entityClass), tableName); return getCqlOperations().queryForResultSet(select).iterator().hasNext(); }