@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); }
@Override public Object execute(Statement statement, Class<?> type) { return operations.select(statement, type); } }
@Override public long count() { return operations.count(entityInformation.getJavaType()); }
@Test public void testCassandraBatchIngest() { List<Book> books = BookSampler.getBookList(5); List<List<?>> ingestBooks = new ArrayList<>(); for (Book b : books) { List<Object> l = new ArrayList<>(); l.add(b.getIsbn()); l.add(b.getTitle()); l.add(b.getAuthor()); l.add(b.getPages()); l.add(b.getSaleDate()); l.add(b.isInStock()); ingestBooks.add(l); } Message<List<List<?>>> message = MessageBuilder.withPayload(ingestBooks).build(); this.cassandraMessageHandler3.handleMessage(message); Select select = QueryBuilder.select().all().from("book"); books = this.template.select(select, Book.class); assertThat(books).hasSize(5); this.template.batchOps().delete(books); }
@Test public void testBasicCassandraInsert() { Book b1 = new Book(); b1.setIsbn("123456-1"); b1.setTitle("Spring Integration Cassandra"); b1.setAuthor("Cassandra Guru"); b1.setPages(521); b1.setSaleDate(new Date()); b1.setInStock(true); Message<Book> message = MessageBuilder.withPayload(b1).build(); this.cassandraMessageHandler1.handleMessage(message); Select select = QueryBuilder.select().all().from("book"); List<Book> books = this.template.select(select, Book.class); assertThat(books).hasSize(1); this.template.delete(b1); }
private static CassandraConverter toConverter(CassandraOperations operations) { Assert.notNull(operations, "CassandraOperations must not be null"); return operations.getConverter(); }
@Override public <S extends T> S insert(S entity) { Assert.notNull(entity, "Entity must not be null"); return operations.insert(entity); }
private String getTableName(Object entity) { Assert.notNull(entity, "Entity must not be null"); return operations.getTableName(entity.getClass()).toCql(); } }
@Override public boolean existsById(ID id) { Assert.notNull(id, "The given id must not be null"); return operations.exists(id, entityInformation.getJavaType()); }
@Override public void deleteById(ID id) { Assert.notNull(id, "The given id must not be null"); operations.deleteById(id, entityInformation.getJavaType()); }
/** * Create a {@link Insert} statement containing all properties including these with {@literal null} values. * * @param entity the entity, must not be {@literal null}. * @return the constructed {@link Insert} statement. * @deprecated since 2.1, use {@link InsertOptions#isInsertNulls()} with * {@link CassandraOperations#insert(Object, InsertOptions)}. */ protected <S extends T> Insert createInsert(S entity) { return InsertUtil.createInsert(operations.getConverter(), entity); }
@Override public <S extends T> List<S> insert(Iterable<S> entities) { Assert.notNull(entities, "The given Iterable of entities must not be null"); List<S> result = new ArrayList<>(); for (S entity : entities) { result.add(operations.insert(entity)); } return result; }
@Override public Object execute(Statement statement, Class<?> type) { return operations.getCqlOperations().queryForResultSet(statement); } }
@Override @SuppressWarnings("unchecked") public Object execute(Statement statement, Class<?> type) { List<Object> objects = operations.select(statement, (Class) type); if (objects.isEmpty()) { return null; } if (objects.size() == 1 || limiting) { return objects.get(0); } throw new IncorrectResultSizeDataAccessException(1, objects.size()); } }
/** * Create a new {@link CassandraRepositoryFactory} with the given {@link CassandraOperations}. * * @param operations must not be {@literal null} */ public CassandraRepositoryFactory(CassandraOperations operations) { Assert.notNull(operations, "CassandraOperations must not be null"); this.operations = operations; this.mappingContext = operations.getConverter().getMappingContext(); }
@Override public <S extends T> List<S> saveAll(Iterable<S> entities) { Assert.notNull(entities, "The given Iterable of entities must not be null"); List<S> result = new ArrayList<>(); for (S entity : entities) { result.add(operations.insert(entity, INSERT_NULLS).getEntity()); } return result; }
@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"); }
@Override public List<T> findAllById(Iterable<ID> ids) { Assert.notNull(ids, "The given Iterable of id's must not be null"); List<ID> idCollection = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList()); return operations.select(Query.query(where(entityInformation.getIdAttribute()).in(idCollection)), entityInformation.getJavaType()); }
@Override @SuppressWarnings("unchecked") public <T, ID> CassandraEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) { CassandraPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(domainClass); return new MappingCassandraEntityInformation<>((CassandraPersistentEntity<T>) entity, operations.getConverter()); }
@Override public <S extends T> S save(S entity) { Assert.notNull(entity, "Entity must not be null"); return operations.insert(entity, INSERT_NULLS).getEntity(); }