Created
December 6, 2016 18:04
-
-
Save kalypzo/5dbdef83632b22e4d2e6e0686e3ed0f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.config; | |
import com.datastax.driver.core.Session; | |
import com.datastax.driver.mapping.MappingManager; | |
import com.example.repository.CassandraCrudRepositoryImpl; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; | |
@Configuration | |
@EnableCassandraRepositories(repositoryBaseClass = CassandraCrudRepositoryImpl.class) | |
public class CassandraConfig { | |
@Autowired | |
private Session session; | |
@Bean | |
public MappingManager mappingManager() { | |
return new MappingManager(session); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.repository; | |
import org.springframework.data.cassandra.repository.CassandraRepository; | |
import org.springframework.data.repository.NoRepositoryBean; | |
@NoRepositoryBean | |
public interface CassandraCrudRepository<T> extends CassandraRepository<T> { | |
T save(T t, boolean failIfExists); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.repository; | |
import com.datastax.driver.core.*; | |
import com.datastax.driver.core.querybuilder.Insert; | |
import com.datastax.driver.core.querybuilder.QueryBuilder; | |
import com.datastax.driver.mapping.Mapper; | |
import com.datastax.driver.mapping.MappingManager; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.cassandra.core.CassandraOperations; | |
import org.springframework.data.cassandra.repository.MapId; | |
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation; | |
import org.springframework.data.cassandra.repository.support.SimpleCassandraRepository; | |
public class CassandraCrudRepositoryImpl<T> extends SimpleCassandraRepository<T, MapId> implements CassandraCrudRepository<T> { | |
private static final Logger log = LoggerFactory.getLogger(CassandraCrudRepositoryImpl.class); | |
@Autowired | |
private MappingManager mappingManager; | |
@Autowired | |
private Session session; | |
public CassandraCrudRepositoryImpl(CassandraEntityInformation<T, MapId> metadata, CassandraOperations operations) | |
{ | |
super(metadata, operations); | |
log.info("Initializing CassandraCrudRepositoryImpl"); | |
} | |
@SuppressWarnings("unchecked") | |
public T save(T t, boolean failIfExists) { | |
log.info("Calling save with failIfExists"); | |
Mapper<T> mapper = mappingManager.mapper((Class<T>) t.getClass()); | |
Insert insert = buildInsertStatement(mapper.saveQuery(t), mapper.getTableMetadata()); | |
if (!session.execute(insert).wasApplied()) { | |
throw new IllegalArgumentException("Data already exists"); | |
} | |
return t; | |
} | |
private Insert buildInsertStatement(Statement statement, TableMetadata tableMd) { | |
BoundStatement boundStatement = (BoundStatement) statement; | |
String table = tableMd.getName(); | |
Insert insert = QueryBuilder.insertInto(boundStatement.getKeyspace(), table).ifNotExists(); | |
String name; | |
Object value; | |
for (ColumnDefinitions.Definition col : boundStatement.preparedStatement().getVariables()) { | |
name = col.getName(); | |
value = boundStatement.getObject(name); | |
insert.value(name, value); | |
} | |
return insert; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<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> | |
<groupId>com.example.test</groupId> | |
<artifactId>spring-example</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<properties> | |
<datastax.version>3.1.1</datastax.version> | |
<java.version>1.8</java.version> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
</properties> | |
<repositories> | |
<repository> | |
<id>spring-libs-snapshot</id> | |
<url>http://repo.spring.io/libs-snapshot</url> | |
<snapshots> | |
<enabled>true</enabled> | |
</snapshots> | |
</repository> | |
</repositories> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-cassandra</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.datastax.cassandra</groupId> | |
<artifactId>cassandra-driver-core</artifactId> | |
<version>${datastax.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.datastax.cassandra</groupId> | |
<artifactId>cassandra-driver-mapping</artifactId> | |
<version>${datastax.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.datastax.cassandra</groupId> | |
<artifactId>cassandra-driver-extras</artifactId> | |
<version>${datastax.version}</version> | |
</dependency> | |
</dependencies> | |
<pluginRepositories> | |
<pluginRepository> | |
<id>spring-snapshots</id> | |
<url>http://repo.spring.io/snapshot</url> | |
</pluginRepository> | |
<pluginRepository> | |
<id>spring-milestones</id> | |
<url>http://repo.spring.io/milestone</url> | |
</pluginRepository> | |
</pluginRepositories> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<!-- Import dependency management from Spring Boot --> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-dependencies</artifactId> | |
<version>2.0.0.BUILD-SNAPSHOT</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
<version>2.0.0.BUILD-SNAPSHOT</version> | |
<configuration> | |
<mainClass>com.example.Startup</mainClass> | |
</configuration> | |
<executions> | |
<execution> | |
<goals> | |
<goal>repackage</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
@SpringBootApplication | |
public class Startup { | |
private static final Logger LOG = LoggerFactory.getLogger(Startup.class); | |
public static void main(String[] args) throws InterruptedException { | |
SpringApplication.run(Startup.class, args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.model; | |
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.UUID; | |
@Table(User.TABLE) | |
public class User { | |
public static final String TABLE = "user"; | |
@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED) | |
private UUID id; | |
@Column | |
private String userName; | |
@Column | |
private String email; | |
public UUID getId() { | |
return id; | |
} | |
public void setId(UUID id) { | |
this.id = id; | |
} | |
public String getUserName() { | |
return userName; | |
} | |
public void setUserName(String userName) { | |
this.userName = userName; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.controller; | |
import com.example.model.User; | |
import com.example.service.UserService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RestController; | |
import java.util.List; | |
import java.util.UUID; | |
@RestController | |
@RequestMapping("/user") | |
public class UserController { | |
@Autowired | |
private UserService userService; | |
@RequestMapping(method = RequestMethod.POST) | |
public User createUser(@RequestBody User user) { | |
return userService.createUser(user); | |
} | |
@RequestMapping(method = RequestMethod.GET) | |
public List<User> getUsers() { | |
return userService.getUsers(); | |
} | |
@RequestMapping(method = RequestMethod.GET) | |
public User getUserById(UUID id) { | |
return userService.getUserById(id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.repository; | |
import com.example.model.User; | |
public interface UserRepository extends UserRepositoryCustom, CassandraCrudRepository<User> { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.repository; | |
import com.example.model.User; | |
import org.springframework.data.repository.NoRepositoryBean; | |
import java.util.UUID; | |
@NoRepositoryBean | |
public interface UserRepositoryCustom { | |
User findById(UUID id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.repository; | |
import com.example.model.User; | |
import org.springframework.data.cassandra.core.CassandraOperations; | |
import org.springframework.data.cassandra.repository.MapId; | |
import org.springframework.data.cassandra.repository.query.CassandraEntityInformation; | |
import java.util.UUID; | |
public class UserRepositoryImpl extends CassandraCrudRepositoryImpl<User> implements UserRepository { | |
public UserRepositoryImpl(CassandraEntityInformation<User, MapId> metadata, CassandraOperations operations) | |
{ | |
super(metadata, operations); | |
} | |
@Override | |
public User findById(UUID id) { | |
User user = new User(); | |
user.setUserName("jdoe"); | |
user.setId(UUID.randomUUID()); | |
user.setEmail("jdoe@example.com"); | |
return user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.service; | |
import com.example.model.User; | |
import com.example.repository.UserRepository; | |
import com.google.common.collect.Lists; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Qualifier; | |
import org.springframework.stereotype.Service; | |
import java.util.List; | |
import java.util.UUID; | |
@Service | |
public class UserService { | |
@Autowired | |
@Qualifier("userRepository") | |
private UserRepository userRepo; | |
public List<User> getUsers() { | |
return Lists.newArrayList(userRepo.findAll()); | |
} | |
public User getUserById(UUID id) { | |
return userRepo.findById(id); | |
} | |
public User createUser(User user) { | |
return userRepo.save(user, true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment