#19 - Add example for Spring Data Cassandra.
Shows java-config as well as Repository configuration.
This commit is contained in:
42
cassandra/example/Readme.MD
Normal file
42
cassandra/example/Readme.MD
Normal file
@@ -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.
|
||||||
17
cassandra/example/pom.xml
Normal file
17
cassandra/example/pom.xml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>spring-data-cassandra-example</artifactId>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.data.examples</groupId>
|
||||||
|
<artifactId>spring-data-cassandra-examples</artifactId>
|
||||||
|
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<name>Spring Data Cassandra - Example</name>
|
||||||
|
<description>Small sample project showing the usage of Spring Data Cassandra.</description>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<User, Long> {}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
/**
|
||||||
|
* Package showing a simple repository interface to use basic query method execution functionality.
|
||||||
|
*/
|
||||||
|
package example.springdata.cassandra.simple;
|
||||||
|
|
||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
43
cassandra/pom.xml
Normal file
43
cassandra/pom.xml
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>spring-data-cassandra-examples</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.data.examples</groupId>
|
||||||
|
<artifactId>spring-data-examples</artifactId>
|
||||||
|
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<name>Spring Data Cassandra - Examples</name>
|
||||||
|
<description>Sample projects for Spring Data Cassandra</description>
|
||||||
|
<url>http://projects.spring.io/spring-data-cassandra</url>
|
||||||
|
<inceptionYear>2014</inceptionYear>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>example</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-cassandra</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
Reference in New Issue
Block a user