From 4841bbdc11ac0d3d685327ef7f04f2fe026742fa Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Tue, 7 Oct 2014 20:28:41 +0200 Subject: [PATCH] #19 - Add example for Spring Data Cassandra. Shows java-config as well as Repository configuration. --- cassandra/example/Readme.MD | 42 +++++++++++++ cassandra/example/pom.xml | 17 ++++++ .../cassandra/simple/SimpleConfiguration.java | 49 +++++++++++++++ .../simple/SimpleUserRepository.java | 26 ++++++++ .../springdata/cassandra/simple/User.java | 45 ++++++++++++++ .../cassandra/simple/package-info.java | 5 ++ .../simple/SimpleUserRepositoryTests.java | 61 +++++++++++++++++++ cassandra/pom.xml | 43 +++++++++++++ pom.xml | 1 + 9 files changed, 289 insertions(+) create mode 100644 cassandra/example/Readme.MD create mode 100644 cassandra/example/pom.xml create mode 100644 cassandra/example/src/main/java/example/springdata/cassandra/simple/SimpleConfiguration.java create mode 100644 cassandra/example/src/main/java/example/springdata/cassandra/simple/SimpleUserRepository.java create mode 100644 cassandra/example/src/main/java/example/springdata/cassandra/simple/User.java create mode 100644 cassandra/example/src/main/java/example/springdata/cassandra/simple/package-info.java create mode 100644 cassandra/example/src/test/java/example/springdata/cassandra/simple/SimpleUserRepositoryTests.java create mode 100644 cassandra/pom.xml diff --git a/cassandra/example/Readme.MD b/cassandra/example/Readme.MD new file mode 100644 index 00000000..ed96c1d5 --- /dev/null +++ b/cassandra/example/Readme.MD @@ -0,0 +1,42 @@ +# Spring Data Cassandra Example Application + +## Preparation + +### Install Cassandra +Before we can start we have to install Cassandra, e.g. via brew on Max OS. +``` +brew install cassandra +pip install cassandra-driver +brew install cassandra-driver + +More details can be found here: https://wiki.apache.org/cassandra/GettingStarted +``` + +### Start Cassandra +``` +/usr/local/bin/cassandra -f +``` + +### Start a Cassandra shell cqlsh +``` +/usr/local/bin/cqlsh +``` + +### Setup Keyspace and Tables + +``` +CREATE KEYSPACE example +WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }; + +USE example; + +CREATE TABLE users ( + user_id int PRIMARY KEY, + uname text, + fname text, + lname text +); +``` + +That should be enough to get you started. +Now you can simply type ```mvn clean install``` to run the example. \ No newline at end of file diff --git a/cassandra/example/pom.xml b/cassandra/example/pom.xml new file mode 100644 index 00000000..5caab14a --- /dev/null +++ b/cassandra/example/pom.xml @@ -0,0 +1,17 @@ + + 4.0.0 + + spring-data-cassandra-example + + + org.springframework.data.examples + spring-data-cassandra-examples + 1.0.0.BUILD-SNAPSHOT + ../pom.xml + + + Spring Data Cassandra - Example + Small sample project showing the usage of Spring Data Cassandra. + + diff --git a/cassandra/example/src/main/java/example/springdata/cassandra/simple/SimpleConfiguration.java b/cassandra/example/src/main/java/example/springdata/cassandra/simple/SimpleConfiguration.java new file mode 100644 index 00000000..8c06bc5c --- /dev/null +++ b/cassandra/example/src/main/java/example/springdata/cassandra/simple/SimpleConfiguration.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.cassandra.simple; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cassandra.config.java.AbstractCqlTemplateConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.cassandra.core.CassandraTemplate; +import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; + +import com.datastax.driver.core.Session; + +/** + * @author Oliver Gierke + * @author Thomas Darimont + */ +@Configuration +@EnableAutoConfiguration +class SimpleConfiguration { + + @Configuration + @EnableCassandraRepositories + static class CassandraConfig extends AbstractCqlTemplateConfiguration { + + @Override + public String getKeyspaceName() { + return "example"; + } + + @Bean + public CassandraTemplate cassandraTemplate(Session session) { + return new CassandraTemplate(session); + } + } +} diff --git a/cassandra/example/src/main/java/example/springdata/cassandra/simple/SimpleUserRepository.java b/cassandra/example/src/main/java/example/springdata/cassandra/simple/SimpleUserRepository.java new file mode 100644 index 00000000..a9c25f4f --- /dev/null +++ b/cassandra/example/src/main/java/example/springdata/cassandra/simple/SimpleUserRepository.java @@ -0,0 +1,26 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.cassandra.simple; + +import org.springframework.data.repository.CrudRepository; + +/** + * Simple repository interface for {@link User} instances. The interface is used to declare so called query methods, + * methods to retrieve single entities or collections of them. + * + * @author Thomas Darimont + */ +public interface SimpleUserRepository extends CrudRepository {} diff --git a/cassandra/example/src/main/java/example/springdata/cassandra/simple/User.java b/cassandra/example/src/main/java/example/springdata/cassandra/simple/User.java new file mode 100644 index 00000000..effc721d --- /dev/null +++ b/cassandra/example/src/main/java/example/springdata/cassandra/simple/User.java @@ -0,0 +1,45 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.cassandra.simple; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import org.springframework.data.cassandra.mapping.Column; +import org.springframework.data.cassandra.mapping.PrimaryKey; +import org.springframework.data.cassandra.mapping.Table; + +/** + * Sample user class. + * + * @author Oliver Gierke + * @author Thomas Darimont + */ +@Data +@NoArgsConstructor +@Table(value = "users") +public class User { + + @PrimaryKey("user_id") private Long id; + + @Column("uname") private String username; + @Column("fname") private String firstname; + @Column("lname") private String lastname; + + public User(Long id) { + this.setId(id); + } +} diff --git a/cassandra/example/src/main/java/example/springdata/cassandra/simple/package-info.java b/cassandra/example/src/main/java/example/springdata/cassandra/simple/package-info.java new file mode 100644 index 00000000..0e8a9f70 --- /dev/null +++ b/cassandra/example/src/main/java/example/springdata/cassandra/simple/package-info.java @@ -0,0 +1,5 @@ +/** + * Package showing a simple repository interface to use basic query method execution functionality. + */ +package example.springdata.cassandra.simple; + diff --git a/cassandra/example/src/test/java/example/springdata/cassandra/simple/SimpleUserRepositoryTests.java b/cassandra/example/src/test/java/example/springdata/cassandra/simple/SimpleUserRepositoryTests.java new file mode 100644 index 00000000..cb524982 --- /dev/null +++ b/cassandra/example/src/test/java/example/springdata/cassandra/simple/SimpleUserRepositoryTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.cassandra.simple; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Integration test showing the basic usage of {@link SimpleUserRepository}. + * + * @author Oliver Gierke + * @author Thomas Darimont + * @author Christoph Strobl + */ +@Ignore +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SimpleConfiguration.class) +public class SimpleUserRepositoryTests { + + @Autowired SimpleUserRepository repository; + User user; + + @Before + public void setUp() { + + user = new User(); + user.setId(42L); + user.setUsername("foobar"); + user.setFirstname("firstname"); + user.setLastname("lastname"); + } + + @Test + public void findSavedUserById() { + + user = repository.save(user); + + assertThat(repository.findOne(user.getId()), is(user)); + } +} diff --git a/cassandra/pom.xml b/cassandra/pom.xml new file mode 100644 index 00000000..abb92cce --- /dev/null +++ b/cassandra/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + + spring-data-cassandra-examples + pom + + + org.springframework.data.examples + spring-data-examples + 1.0.0.BUILD-SNAPSHOT + + + Spring Data Cassandra - Examples + Sample projects for Spring Data Cassandra + http://projects.spring.io/spring-data-cassandra + 2014 + + + example + + + + + + org.springframework.boot + spring-boot-starter-aop + + + + org.springframework.data + spring-data-cassandra + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + diff --git a/pom.xml b/pom.xml index 511aeb7a..c864435e 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ rest redis solr + cassandra