Skip to content

Instantly share code, notes, and snippets.

@mp911de
Last active November 14, 2016 18:45
Show Gist options
  • Select an option

  • Save mp911de/b587b5e7396e53a7cd5c00ff9d0e9ab2 to your computer and use it in GitHub Desktop.

Select an option

Save mp911de/b587b5e7396e53a7cd5c00ff9d0e9ab2 to your computer and use it in GitHub Desktop.
/*
* Copyright 2016 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 com.example;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScanPackages;
import org.springframework.context.annotation.Bean;
import org.springframework.data.cassandra.config.CassandraEntityClassScanner;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.mapping.SimpleUserTypeResolver;
import com.datastax.driver.core.Cluster;
@SpringBootApplication
public class App {
private final BeanFactory beanFactory;
public App(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public static void main(String[] args) {
SpringApplication.run(App.class);
}
@Bean
public CassandraMappingContext cassandraMapping(Cluster cluster, CassandraProperties cassandraProperties)
throws ClassNotFoundException {
BasicCassandraMappingContext context = new BasicCassandraMappingContext();
List<String> packages = EntityScanPackages.get(this.beanFactory).getPackageNames();
if (packages.isEmpty() && AutoConfigurationPackages.has(this.beanFactory)) {
packages = AutoConfigurationPackages.get(this.beanFactory);
}
if (!packages.isEmpty()) {
context.setInitialEntitySet(CassandraEntityClassScanner.scan(packages));
}
context.setUserTypeResolver(new SimpleUserTypeResolver(cluster, cassandraProperties.getKeyspaceName()));
return context;
}
@Bean
public CommandLineRunner demo(EmployeeRepository repository) {
System.out.println("Pre-save");
Employee employee = new Employee();
EmployeeIdKey key = new EmployeeIdKey();
key.setUserId("55550");
key.setUuId(UUID.randomUUID());
employee.setId(key);
List<String> emailIds = Arrays.asList("myemail@domain.com", "myemail2@domain2.com");
employee.setEmailId(emailIds);
Person person = new Person("John", "Mathew");
employee.setPerson(person);
repository.save(employee);
System.out.println("Saved");
return null;
}
}
spring.data.cassandra.keyspace-name=example
spring.data.cassandra.schema-action=RECREATE
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.2.RELEASE)
2016-11-14 19:40:43.236 INFO 1827 --- [ main] com.example.App : Starting App on mp911de-MBP.fritz.box with PID 1827 (/Users/mpaluch/playground/cassandra-playground/target/classes started by mpaluch in /Users/mpaluch/playground/cassandra-playground)
2016-11-14 19:40:43.239 INFO 1827 --- [ main] com.example.App : No active profile set, falling back to default profiles: default
2016-11-14 19:40:43.294 INFO 1827 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3972a855: startup date [Mon Nov 14 19:40:43 CET 2016]; root of context hierarchy
2016-11-14 19:40:44.547 INFO 1827 --- [ main] com.datastax.driver.core.ClockFactory : Using native clock to generate timestamps.
2016-11-14 19:40:44.792 INFO 1827 --- [ main] com.datastax.driver.core.NettyUtil : Did not find Netty's native epoll transport in the classpath, defaulting to NIO.
2016-11-14 19:40:45.139 WARN 1827 --- [ main] com.datastax.driver.core.Cluster : You listed localhost/0:0:0:0:0:0:0:1:9042 in your contact points, but it wasn't found in the control host's system.peers at startup
2016-11-14 19:40:45.247 INFO 1827 --- [ main] c.d.d.c.p.DCAwareRoundRobinPolicy : Using data-center name 'datacenter1' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor)
2016-11-14 19:40:45.248 INFO 1827 --- [ main] com.datastax.driver.core.Cluster : New Cassandra host localhost/127.0.0.1:9042 added
Pre-save
Saved
2016-11-14 19:40:45.654 INFO 1827 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-11-14 19:40:45.663 INFO 1827 --- [ main] com.example.App : Started App in 2.749 seconds (JVM running for 3.252)
Disconnected from the target VM, address: '127.0.0.1:61247', transport: 'socket'
2016-11-14 19:40:48.366 INFO 1827 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3972a855: startup date [Mon Nov 14 19:40:43 CET 2016]; root of context hierarchy
2016-11-14 19:40:48.368 INFO 1827 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
/*
* Copyright 2016 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 com.example;
import java.util.List;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;
import lombok.Data;
@Table("employee")
@Data
public class Employee {
@PrimaryKey private EmployeeIdKey id;
private Person person;
@Column("employee_no") private String employeeNo;
@Column("email_ids") private List<String> emailId;
// Getters and setters
}
/*
* Copyright 2016 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 com.example;
import java.io.Serializable;
import java.util.UUID;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.CassandraType;
import org.springframework.data.cassandra.mapping.PrimaryKeyClass;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import com.datastax.driver.core.DataType;
import lombok.Data;
@PrimaryKeyClass
@Data
public class EmployeeIdKey implements Serializable {
@PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED,
name = "id") @CassandraType(type = DataType.Name.UUID) private UUID id;
@PrimaryKeyColumn(ordinal = 1, type = PrimaryKeyType.CLUSTERED,
name = "user_id") @CassandraType(type = DataType.Name.TEXT) private String userId;
public EmployeeIdKey() {
id = null;
userId = null;
}
public UUID getUuId() {
return id;
}
public void setUuId(UUID uuId) {
this.id = uuId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
/*
* Copyright 2016 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 com.example;
import org.springframework.data.cassandra.repository.TypedIdCassandraRepository;
public interface EmployeeRepository extends TypedIdCassandraRepository<Employee,EmployeeIdKey> {
}
/*
* Copyright 2016 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 com.example;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.UserDefinedType;
import lombok.Data;
@UserDefinedType
@Data
public class Person {
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Column("first_name") private String firstName;
@Column("last_name") private String lastName;
// Getters adn setters;
}
<?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</groupId>
<artifactId>cassandra-playground</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cassandra-playground</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.13.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-cql</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment