Skip to content

Commit 54ab817

Browse files
mp911deodrotbohm
authored andcommitted
spring-projects#250 - Add example for Cassandra User-defined type usage.
1 parent d9f7790 commit 54ab817

File tree

4 files changed

+205
-2
lines changed

4 files changed

+205
-2
lines changed

cassandra/example/src/main/java/example/springdata/cassandra/basic/BasicUserRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public interface BasicUserRepository extends CrudRepository<User, Long> {
4848

4949
/**
5050
* Derived query method using SASI (SSTable Attached Secondary Index) features through the {@code LIKE} keyword. This
51-
* query corresponds with {@code SELECT * FROM users WHERE uname LIKE '?0%'}. {@link User#username} is not part of the
52-
* primary so it requires a secondary index.
51+
* query corresponds with {@code SELECT * FROM users WHERE lname LIKE '?0'}. {@link User#lastname} is not part of the
52+
* primary key so it requires a secondary index.
5353
*
5454
* @param lastnamePrefix
5555
* @return
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.cassandra.udt;
17+
18+
import lombok.AllArgsConstructor;
19+
import lombok.Data;
20+
21+
import org.springframework.data.cassandra.mapping.UserDefinedType;
22+
23+
/**
24+
* @author Mark Paluch
25+
*/
26+
@Data
27+
@UserDefinedType
28+
@AllArgsConstructor
29+
public class Address {
30+
31+
String street, zip, city;
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.cassandra.udt;
17+
18+
import com.datastax.driver.core.DataType.Name;
19+
import com.datastax.driver.core.UDTValue;
20+
import lombok.Data;
21+
22+
import java.util.List;
23+
24+
import org.springframework.data.annotation.Id;
25+
import org.springframework.data.cassandra.mapping.CassandraType;
26+
import org.springframework.data.cassandra.mapping.Table;
27+
28+
/**
29+
* @author Mark Paluch
30+
*/
31+
@Data
32+
@Table
33+
public class Person {
34+
35+
@Id int id;
36+
37+
String firstname, lastname;
38+
Address current;
39+
List<Address> previous;
40+
41+
@CassandraType(type = Name.UDT, userTypeName = "address")
42+
UDTValue alternative;
43+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.cassandra.udt;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import example.springdata.cassandra.util.RequiresCassandraKeyspace;
21+
22+
import java.util.Collections;
23+
24+
import org.junit.Before;
25+
import org.junit.ClassRule;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.data.cassandra.config.SchemaAction;
32+
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
33+
import org.springframework.data.cassandra.core.CassandraAdminOperations;
34+
import org.springframework.data.cassandra.core.CassandraOperations;
35+
import org.springframework.test.context.junit4.SpringRunner;
36+
37+
import com.datastax.driver.core.KeyspaceMetadata;
38+
import com.datastax.driver.core.UDTValue;
39+
import com.datastax.driver.core.UserType;
40+
41+
/**
42+
* Integration test to show User-Defined type support.
43+
*
44+
* @author Mark Paluch
45+
*/
46+
@RunWith(SpringRunner.class)
47+
@SpringBootTest
48+
public class UserDefinedTypeIntegrationTest {
49+
50+
@ClassRule public final static RequiresCassandraKeyspace CASSANDRA_KEYSPACE = RequiresCassandraKeyspace.onLocalhost();
51+
52+
@Configuration
53+
static class Config extends AbstractCassandraConfiguration {
54+
55+
@Override
56+
public String getKeyspaceName() {
57+
return "example";
58+
}
59+
60+
@Override
61+
public String[] getEntityBasePackages() {
62+
return new String[] { Person.class.getPackage().getName() };
63+
}
64+
65+
@Override
66+
public SchemaAction getSchemaAction() {
67+
return SchemaAction.RECREATE;
68+
}
69+
}
70+
71+
@Autowired CassandraOperations operations;
72+
@Autowired CassandraAdminOperations adminOperations;
73+
74+
@Before
75+
public void before() throws Exception {
76+
operations.truncate("person");
77+
}
78+
79+
/**
80+
* Insert a row with a mapped User-defined type.
81+
*/
82+
@Test
83+
public void insertMappedUdt() {
84+
85+
Person person = new Person();
86+
person.setId(42);
87+
person.setFirstname("Walter");
88+
person.setLastname("White");
89+
90+
person.setCurrent(new Address("308 Negra Arroyo Lane", "87104", "Albuquerque"));
91+
person.setPrevious(Collections.singletonList(new Address("12000 – 12100 Coors Rd SW", "87045", "Albuquerque")));
92+
93+
operations.insert(person);
94+
95+
Person loaded = operations.selectOne("SELECT * FROM person WHERE id = 42", Person.class);
96+
97+
assertThat(loaded.getCurrent()).isEqualTo(person.getCurrent());
98+
assertThat(loaded.getPrevious()).containsAll(person.getPrevious());
99+
}
100+
101+
/**
102+
* Insert a row with a raw User-defined type.
103+
*/
104+
@Test
105+
public void insertRawUdt() {
106+
107+
KeyspaceMetadata keyspaceMetadata = adminOperations.getKeyspaceMetadata();
108+
UserType address = keyspaceMetadata.getUserType("address");
109+
110+
UDTValue udtValue = address.newValue();
111+
udtValue.setString("street", "308 Negra Arroyo Lane");
112+
udtValue.setString("zip", "87104");
113+
udtValue.setString("city", "Albuquerque");
114+
115+
Person person = new Person();
116+
person.setId(42);
117+
person.setFirstname("Walter");
118+
person.setLastname("White");
119+
120+
person.setAlternative(udtValue);
121+
122+
operations.insert(person);
123+
124+
Person loaded = operations.selectOne("SELECT * FROM person WHERE id = 42", Person.class);
125+
126+
assertThat(loaded.getAlternative().getString("zip")).isEqualTo("87104");
127+
}
128+
}

0 commit comments

Comments
 (0)