Skip to content

Commit 24094c8

Browse files
committed
Adding initial files
1 parent 609fdc6 commit 24094c8

File tree

8 files changed

+272
-0
lines changed

8 files changed

+272
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.spring.data.cassandra.config;
2+
3+
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
7+
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
8+
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
9+
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
10+
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
11+
12+
@Configuration
13+
@EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository")
14+
public class CassandraConfig extends AbstractCassandraConfiguration {
15+
16+
@Override
17+
protected String getKeyspaceName() {
18+
return "event";
19+
}
20+
21+
@Bean
22+
public CassandraClusterFactoryBean cluster() {
23+
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
24+
cluster.setContactPoints("127.0.0.1");
25+
cluster.setPort(9142);
26+
return cluster;
27+
}
28+
29+
@Bean
30+
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
31+
return new BasicCassandraMappingContext();
32+
}
33+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.baeldung.spring.data.cassandra.model;
2+
3+
import org.springframework.cassandra.core.Ordering;
4+
import org.springframework.cassandra.core.PrimaryKeyType;
5+
import org.springframework.data.cassandra.mapping.Column;
6+
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
7+
import org.springframework.data.cassandra.mapping.Table;
8+
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
import java.util.UUID;
12+
13+
@Table
14+
public class Event {
15+
@PrimaryKeyColumn(name = "id", ordinal = 2, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
16+
private UUID id;
17+
@PrimaryKeyColumn(name = "type", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
18+
private String type;
19+
@PrimaryKeyColumn(name = "bucket", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
20+
private String bucket;
21+
@Column
22+
private Set tags = new HashSet();
23+
24+
public Event(UUID id, String type, String bucket, Set tags) {
25+
this.id = id;
26+
this.type = type;
27+
this.bucket = bucket;
28+
this.tags.addAll(tags);
29+
}
30+
31+
public UUID getId() {
32+
return id;
33+
}
34+
35+
public String getType() {
36+
return type;
37+
}
38+
39+
public String getBucket() {
40+
return bucket;
41+
}
42+
43+
public Set getTags() {
44+
return tags;
45+
}
46+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.spring.data.cassandra.repository;
2+
3+
4+
import org.baeldung.spring.data.cassandra.model.Event;
5+
import org.springframework.data.cassandra.repository.CassandraRepository;
6+
import org.springframework.data.cassandra.repository.Query;
7+
import org.springframework.stereotype.Repository;
8+
9+
@Repository
10+
public interface EventRepository extends CassandraRepository<Event> {
11+
@Query("select * from event where type = ?0 and bucket=?1")
12+
Iterable<Event> findByTypeAndBucket(String type, String bucket);
13+
}
14+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
7+
8+
<cassandra:session keyspace-name="testKeySpace" />
9+
10+
<!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter -->
11+
<cassandra:mapping />
12+
13+
<!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->
14+
<cassandra:converter />
15+
16+
<!-- REQUIRED: The Cassandra Template is the building block of all Spring
17+
Data Cassandra -->
18+
<cassandra:template id="cassandraTemplate" />
19+
20+
</beans>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="org.springframework" level="WARN" />
11+
<logger name="org.springframework.transaction" level="WARN" />
12+
13+
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
14+
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
15+
16+
<root level="INFO">
17+
<appender-ref ref="STDOUT" />
18+
</root>
19+
20+
</configuration>
855 Bytes
Loading
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.baeldung.spring.data.cassandra.config;
2+
3+
import com.datastax.driver.core.Cluster;
4+
import com.datastax.driver.core.Session;
5+
import org.apache.cassandra.exceptions.ConfigurationException;
6+
import org.apache.thrift.transport.TTransportException;
7+
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
11+
import org.springframework.data.cassandra.config.SchemaAction;
12+
import org.springframework.data.cassandra.convert.CassandraConverter;
13+
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
14+
import org.springframework.data.cassandra.core.CassandraOperations;
15+
import org.springframework.data.cassandra.core.CassandraTemplate;
16+
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
17+
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
18+
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
19+
20+
import java.io.IOException;
21+
22+
@Configuration
23+
@EnableCassandraRepositories(basePackages = "org.baeldung.spring.data.cassandra.repository")
24+
public class CassandraTestConfig{
25+
26+
@Bean(destroyMethod = "close")
27+
public Cluster cluster() throws ConfigurationException, TTransportException, IOException, InterruptedException{
28+
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
29+
Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
30+
Session session = cluster.connect();
31+
session.execute("CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = {"
32+
+ " 'class': 'SimpleStrategy', "
33+
+ " 'replication_factor': '3' "
34+
+ "};" );
35+
// session.execute("CREATE KEYSPACE testKeySpace WITH replication={'class' : 'SimpleStrategy', 'replication_factor':1}");
36+
// session.execute("USE testKeySpace");
37+
return cluster;
38+
}
39+
40+
@Bean
41+
public CassandraMappingContext mappingContext() {
42+
return new BasicCassandraMappingContext();
43+
}
44+
45+
@Bean
46+
public CassandraConverter converter() {
47+
return new MappingCassandraConverter(mappingContext());
48+
}
49+
50+
@Bean
51+
public CassandraSessionFactoryBean session() throws Exception {
52+
53+
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
54+
session.setCluster(cluster());
55+
session.setKeyspaceName("testKeySpace");
56+
session.setConverter(converter());
57+
session.setSchemaAction(SchemaAction.NONE);
58+
59+
return session;
60+
}
61+
62+
@Bean
63+
public CassandraOperations cassandraTemplate() throws Exception {
64+
return new CassandraTemplate(session().getObject());
65+
}
66+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.baeldung.spring.data.cassandra.repository;
2+
3+
import com.datastax.driver.core.utils.UUIDs;
4+
import com.google.common.collect.ImmutableSet;
5+
import org.apache.cassandra.exceptions.ConfigurationException;
6+
import org.apache.thrift.transport.TTransportException;
7+
import org.baeldung.spring.data.cassandra.config.CassandraConfig;
8+
import org.baeldung.spring.data.cassandra.model.Event;
9+
import org.cassandraunit.spring.CassandraUnitTestExecutionListener;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.cassandra.core.cql.CqlIdentifier;
14+
import org.springframework.data.cassandra.core.CassandraAdminOperations;
15+
import org.springframework.test.context.ContextConfiguration;
16+
import org.springframework.test.context.TestExecutionListeners;
17+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18+
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
19+
20+
import java.io.IOException;
21+
import java.util.HashMap;
22+
23+
import static org.hamcrest.CoreMatchers.hasItem;
24+
import static org.hamcrest.CoreMatchers.not;
25+
import static org.junit.Assert.assertThat;
26+
27+
@RunWith(SpringJUnit4ClassRunner.class)
28+
@ContextConfiguration(classes = {CassandraConfig.class})
29+
@TestExecutionListeners({CassandraUnitTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
30+
public class EventRepositoryIntegrationTest {
31+
32+
public static final String TIME_BUCKET = "2014-01-01";
33+
34+
@Autowired
35+
private EventRepository eventRepository;
36+
37+
@Autowired
38+
private CassandraAdminOperations adminTemplate;
39+
40+
@Test
41+
public void repositoryStoresAndRetrievesEvents() throws InterruptedException, TTransportException, ConfigurationException, IOException {
42+
adminTemplate.execute("CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = {"
43+
+ " 'class': 'SimpleStrategy', "
44+
+ " 'replication_factor': '3' "
45+
+ "};" );
46+
adminTemplate.dropTable(CqlIdentifier.cqlId("event"));
47+
adminTemplate.createTable(true, CqlIdentifier.cqlId("event"), Event.class, new HashMap<String, Object>());
48+
Event event1 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag1", "tag2"));
49+
Event event2 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag3"));
50+
eventRepository.save(ImmutableSet.of(event1, event2));
51+
52+
Iterable<Event> events = eventRepository.findByTypeAndBucket("type1", TIME_BUCKET);
53+
54+
assertThat(events, hasItem(event1));
55+
assertThat(events, hasItem(event2));
56+
}
57+
58+
// @Test
59+
public void repositoryDeletesStoredEvents() {
60+
Event event1 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag1", "tag2"));
61+
Event event2 = new Event(UUIDs.timeBased(), "type1", TIME_BUCKET, ImmutableSet.of("tag3"));
62+
eventRepository.save(ImmutableSet.of(event1, event2));
63+
64+
eventRepository.delete(event1);
65+
eventRepository.delete(event2);
66+
67+
Iterable<Event> events = eventRepository.findByTypeAndBucket("type1", TIME_BUCKET);
68+
69+
assertThat(events, not(hasItem(event1)));
70+
assertThat(events, not(hasItem(event2)));
71+
}
72+
73+
}

0 commit comments

Comments
 (0)