#553 - Adapt Spring Data Cassandra.

This commit is contained in:
Mark Paluch
2020-02-10 15:28:29 +01:00
parent c88ad331c0
commit 523d2c8a56
18 changed files with 72 additions and 97 deletions

View File

@@ -28,7 +28,7 @@ import org.springframework.core.convert.converter.Converter;
import org.springframework.data.cassandra.core.convert.CassandraCustomConversions;
import org.springframework.util.StringUtils;
import com.datastax.driver.core.Row;
import com.datastax.oss.driver.api.core.cql.Row;
import com.fasterxml.jackson.databind.ObjectMapper;
/**

View File

@@ -23,8 +23,7 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.core.mapping.CassandraType;
import org.springframework.data.cassandra.core.mapping.Table;
import com.datastax.driver.core.DataType.Name;
import com.datastax.driver.core.UDTValue;
import com.datastax.oss.driver.api.core.data.UdtValue;
/**
* @author Mark Paluch
@@ -39,6 +38,5 @@ public class Person {
Address current;
List<Address> previous;
@CassandraType(type = Name.UDT, userTypeName = "address")
UDTValue alternative;
@CassandraType(type = CassandraType.Name.UDT, userTypeName = "address") UdtValue alternative;
}

View File

@@ -1,2 +1,3 @@
spring.data.cassandra.keyspace-name=example
spring.data.cassandra.schema-action=recreate
spring.data.cassandra.local-datacenter=datacenter1

View File

@@ -25,12 +25,13 @@ import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.util.Version;
import org.springframework.test.context.junit4.SpringRunner;
import com.datastax.driver.core.Session;
import com.datastax.oss.driver.api.core.CqlSession;
/**
* Integration test showing the basic usage of {@link BasicUserRepository}.
@@ -49,7 +50,7 @@ public class BasicUserRepositoryTests {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
@Autowired BasicUserRepository repository;
@Autowired Session session;
@Autowired CqlSession session;
User user;
@Before

View File

@@ -28,6 +28,7 @@ import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.cassandra.core.AsyncCassandraTemplate;
@@ -36,10 +37,10 @@ import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.concurrent.ListenableFuture;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.querybuilder.Insert;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import com.datastax.oss.driver.api.querybuilder.insert.Insert;
/**
* Integration test showing the basic usage of {@link CassandraTemplate}.
@@ -52,7 +53,7 @@ public class CassandraOperationsIntegrationTests {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
@Autowired Session session;
@Autowired CqlSession session;
@Autowired CassandraOperations template;
@Before
@@ -67,18 +68,18 @@ public class CassandraOperationsIntegrationTests {
@Test
public void insertAndSelect() {
Insert insert = QueryBuilder.insertInto("users").value("user_id", 42L) //
.value("uname", "heisenberg") //
.value("fname", "Walter") //
.value("lname", "White") //
Insert insert = QueryBuilder.insertInto("users").value("user_id", QueryBuilder.literal(42L)) //
.value("uname", QueryBuilder.literal("heisenberg")) //
.value("fname", QueryBuilder.literal("Walter")) //
.value("lname", QueryBuilder.literal("White")) //
.ifNotExists(); //
template.getCqlOperations().execute(insert);
template.getCqlOperations().execute(insert.asCql());
User user = template.selectOneById(42L, User.class);
assertThat(user.getUsername()).isEqualTo("heisenberg");
List<User> users = template.select(QueryBuilder.select().from("users"), User.class);
List<User> users = template.select(QueryBuilder.selectFrom("users").all().asCql(), User.class);
assertThat(users).hasSize(1);
assertThat(users.get(0)).isEqualTo(user);
}
@@ -148,13 +149,13 @@ public class CassandraOperationsIntegrationTests {
template.insert(user);
Long id = template.selectOne(QueryBuilder.select("user_id").from("users"), Long.class);
Long id = template.selectOne(QueryBuilder.selectFrom("users").column("user_id").asCql(), Long.class);
assertThat(id).isEqualTo(user.getId());
Row row = template.selectOne(QueryBuilder.select("user_id").from("users"), Row.class);
Row row = template.selectOne(QueryBuilder.selectFrom("users").column("user_id").asCql(), Row.class);
assertThat(row.getLong(0)).isEqualTo(user.getId());
Map<String, Object> map = template.selectOne(QueryBuilder.select().from("users"), Map.class);
Map<String, Object> map = template.selectOne(QueryBuilder.selectFrom("users").all().asCql(), Map.class);
assertThat(map).containsEntry("user_id", user.getId());
assertThat(map).containsEntry("fname", "Walter");
}

View File

@@ -17,6 +17,9 @@ package example.springdata.cassandra.convert;
import static org.assertj.core.api.Assertions.*;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.data.TupleValue;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import example.springdata.cassandra.util.CassandraKeyspace;
import java.util.Arrays;
@@ -33,9 +36,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.test.context.junit4.SpringRunner;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.TupleValue;
import com.datastax.driver.core.querybuilder.QueryBuilder;
/**
* @author Mark Paluch
@@ -68,7 +68,7 @@ public class ConversionIntegrationTests {
operations.insert(addressbook);
Row row = operations.selectOne(QueryBuilder.select().from("addressbook"), Row.class);
Row row = operations.selectOne(QueryBuilder.selectFrom("addressbook").all().asCql(), Row.class);
assertThat(row).isNotNull();
@@ -92,7 +92,7 @@ public class ConversionIntegrationTests {
operations.insert(addressbook);
Addressbook loaded = operations.selectOne(QueryBuilder.select().from("addressbook"), Addressbook.class);
Addressbook loaded = operations.selectOne(QueryBuilder.selectFrom("addressbook").all().asCql(), Addressbook.class);
assertThat(loaded.getMe()).isEqualTo(addressbook.getMe());
assertThat(loaded.getFriends()).isEqualTo(addressbook.getFriends());
@@ -113,7 +113,8 @@ public class ConversionIntegrationTests {
operations.insert(addressbook);
CustomAddressbook loaded = operations.selectOne(QueryBuilder.select().from("addressbook"), CustomAddressbook.class);
CustomAddressbook loaded = operations.selectOne(QueryBuilder.selectFrom("addressbook").all().asCql(),
CustomAddressbook.class);
assertThat(loaded.getTheId()).isEqualTo(addressbook.getId());
assertThat(loaded.getMyDetailsAsJson()).contains("\"firstname\":\"Walter\"");
@@ -142,7 +143,7 @@ public class ConversionIntegrationTests {
operations.insert(addressbook);
Row row = operations.selectOne(QueryBuilder.select().from("addressbook"), Row.class);
Row row = operations.selectOne(QueryBuilder.selectFrom("addressbook").all().asCql(), Row.class);
assertThat(row).isNotNull();

View File

@@ -25,6 +25,7 @@ import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
@@ -34,9 +35,9 @@ import org.springframework.data.cassandra.core.CassandraAdminOperations;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.test.context.junit4.SpringRunner;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.UDTValue;
import com.datastax.driver.core.UserType;
import com.datastax.oss.driver.api.core.data.UdtValue;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import com.datastax.oss.driver.api.core.type.UserDefinedType;
/**
* Integration test to show User-Defined type support.
@@ -58,6 +59,11 @@ public class UserDefinedTypeIntegrationTest {
return "example";
}
@Override
protected String getLocalDataCenter() {
return "datacenter1";
}
@Override
public String[] getEntityBasePackages() {
return new String[] { Person.class.getPackage().getName() };
@@ -106,9 +112,9 @@ public class UserDefinedTypeIntegrationTest {
public void insertRawUdt() {
KeyspaceMetadata keyspaceMetadata = adminOperations.getKeyspaceMetadata();
UserType address = keyspaceMetadata.getUserType("address");
UserDefinedType address = keyspaceMetadata.getUserDefinedType("address").get();
UDTValue udtValue = address.newValue();
UdtValue udtValue = address.newValue();
udtValue.setString("street", "308 Negra Arroyo Lane");
udtValue.setString("zip", "87104");
udtValue.setString("city", "Albuquerque");