Adding initial files
This commit is contained in:
parent
609fdc67ca
commit
24094c838a
|
@ -0,0 +1,33 @@
|
|||
package org.baeldung.spring.data.cassandra.config;
|
||||
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
|
||||
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
|
||||
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
|
||||
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
|
||||
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
|
||||
|
||||
@Configuration
|
||||
@EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository")
|
||||
public class CassandraConfig extends AbstractCassandraConfiguration {
|
||||
|
||||
@Override
|
||||
protected String getKeyspaceName() {
|
||||
return "event";
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CassandraClusterFactoryBean cluster() {
|
||||
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
|
||||
cluster.setContactPoints("127.0.0.1");
|
||||
cluster.setPort(9142);
|
||||
return cluster;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
|
||||
return new BasicCassandraMappingContext();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.baeldung.spring.data.cassandra.model;
|
||||
|
||||
import org.springframework.cassandra.core.Ordering;
|
||||
import org.springframework.cassandra.core.PrimaryKeyType;
|
||||
import org.springframework.data.cassandra.mapping.Column;
|
||||
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
|
||||
import org.springframework.data.cassandra.mapping.Table;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Table
|
||||
public class Event {
|
||||
@PrimaryKeyColumn(name = "id", ordinal = 2, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
|
||||
private UUID id;
|
||||
@PrimaryKeyColumn(name = "type", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
|
||||
private String type;
|
||||
@PrimaryKeyColumn(name = "bucket", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
|
||||
private String bucket;
|
||||
@Column
|
||||
private Set tags = new HashSet();
|
||||
|
||||
public Event(UUID id, String type, String bucket, Set tags) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.bucket = bucket;
|
||||
this.tags.addAll(tags);
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getBucket() {
|
||||
return bucket;
|
||||
}
|
||||
|
||||
public Set getTags() {
|
||||
return tags;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
|
||||
import org.baeldung.spring.data.cassandra.model.Event;
|
||||
import org.springframework.data.cassandra.repository.CassandraRepository;
|
||||
import org.springframework.data.cassandra.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface EventRepository extends CassandraRepository<Event> {
|
||||
@Query("select * from event where type = ?0 and bucket=?1")
|
||||
Iterable<Event> findByTypeAndBucket(String type, String bucket);
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
|
||||
|
||||
<cassandra:session keyspace-name="testKeySpace" />
|
||||
|
||||
<!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter -->
|
||||
<cassandra:mapping />
|
||||
|
||||
<!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->
|
||||
<cassandra:converter />
|
||||
|
||||
<!-- REQUIRED: The Cassandra Template is the building block of all Spring
|
||||
Data Cassandra -->
|
||||
<cassandra:template id="cassandraTemplate" />
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,20 @@
|
|||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
Binary file not shown.
After Width: | Height: | Size: 855 B |
|
@ -0,0 +1,66 @@
|
|||
package org.baeldung.spring.data.cassandra.config;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
|
||||
import org.springframework.data.cassandra.config.SchemaAction;
|
||||
import org.springframework.data.cassandra.convert.CassandraConverter;
|
||||
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
|
||||
import org.springframework.data.cassandra.core.CassandraOperations;
|
||||
import org.springframework.data.cassandra.core.CassandraTemplate;
|
||||
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
|
||||
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
|
||||
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Configuration
|
||||
@EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository")
|
||||
public class CassandraTestConfig{
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
public Cluster cluster() throws ConfigurationException, TTransportException, IOException, InterruptedException{
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
|
||||
Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
|
||||
Session session = cluster.connect();
|
||||
session.execute("CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = {"
|
||||
+ " 'class': 'SimpleStrategy', "
|
||||
+ " 'replication_factor': '3' "
|
||||
+ "};" );
|
||||
// session.execute("CREATE KEYSPACE testKeySpace WITH replication={'class' : 'SimpleStrategy', 'replication_factor':1}");
|
||||
// session.execute("USE testKeySpace");
|
||||
return cluster;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CassandraMappingContext mappingContext() {
|
||||
return new BasicCassandraMappingContext();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CassandraConverter converter() {
|
||||
return new MappingCassandraConverter(mappingContext());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CassandraSessionFactoryBean session() throws Exception {
|
||||
|
||||
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
|
||||
session.setCluster(cluster());
|
||||
session.setKeyspaceName("testKeySpace");
|
||||
session.setConverter(converter());
|
||||
session.setSchemaAction(SchemaAction.NONE);
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CassandraOperations cassandraTemplate() throws Exception {
|
||||
return new CassandraTemplate(session().getObject());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package org.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import org.baeldung.spring.data.cassandra.model.Event;
|
||||
import org.cassandraunit.spring.CassandraUnitTestExecutionListener;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cassandra.core.cql.CqlIdentifier;
|
||||
import org.springframework.data.cassandra.core.CassandraAdminOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.hasItem;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {CassandraConfig.class})
|
||||
@TestExecutionListeners({CassandraUnitTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
|
||||
public class EventRepositoryIntegrationTest {
|
||||
|
||||
public static final String TIME_BUCKET = "2014-01-01";
|
||||
|
||||
@Autowired
|
||||
private EventRepository eventRepository;
|
||||
|
||||
@Autowired
|
||||
private CassandraAdminOperations adminTemplate;
|
||||
|
||||
@Test
|
||||
public void repositoryStoresAndRetrievesEvents() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.execute("CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = {"
|
||||
+ " 'class': 'SimpleStrategy', "
|
||||
+ " 'replication_factor': '3' "
|
||||
+ "};" );
|
||||
adminTemplate.dropTable(CqlIdentifier.cqlId("event"));
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId("event"), Event.class, new HashMap<String, Object>());
|
||||
Event event1 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag1", "tag2"));
|
||||
Event event2 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag3"));
|
||||
eventRepository.save(ImmutableSet.of(event1, event2));
|
||||
|
||||
Iterable<Event> events = eventRepository.findByTypeAndBucket("type1", TIME_BUCKET);
|
||||
|
||||
assertThat(events, hasItem(event1));
|
||||
assertThat(events, hasItem(event2));
|
||||
}
|
||||
|
||||
// @Test
|
||||
public void repositoryDeletesStoredEvents() {
|
||||
Event event1 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag1", "tag2"));
|
||||
Event event2 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag3"));
|
||||
eventRepository.save(ImmutableSet.of(event1, event2));
|
||||
|
||||
eventRepository.delete(event1);
|
||||
eventRepository.delete(event2);
|
||||
|
||||
Iterable<Event> events = eventRepository.findByTypeAndBucket("type1", TIME_BUCKET);
|
||||
|
||||
assertThat(events, not(hasItem(event1)));
|
||||
assertThat(events, not(hasItem(event2)));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue