| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8f2e92d commit 639de96
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,8 +60,32 @@ | |||
| 60 | 60 | <version>${system.stubs.version}</version> | |
| 61 | 61 | <scope>test</scope> | |
| 62 | 62 | </dependency> | |
| 63 | + <dependency> | ||
| 64 | + <groupId>com.datastax.oss</groupId> | ||
| 65 | + <artifactId>java-driver-mapper-runtime</artifactId> | ||
| 66 | + <version>4.15.0</version> | ||
| 67 | + </dependency> | ||
| 63 | 68 | </dependencies> | |
| 64 | 69 | ||
| 70 | + <build> | ||
| 71 | + <plugins> | ||
| 72 | + <plugin> | ||
| 73 | + <groupId>org.apache.maven.plugins</groupId> | ||
| 74 | + <artifactId>maven-compiler-plugin</artifactId> | ||
| 75 | + <version>3.8.1</version> | ||
| 76 | + <configuration> | ||
| 77 | + <annotationProcessorPaths> | ||
| 78 | + <path> | ||
| 79 | + <groupId>com.datastax.oss</groupId> | ||
| 80 | + <artifactId>java-driver-mapper-processor</artifactId> | ||
| 81 | + <version>4.15.0</version> | ||
| 82 | + </path> | ||
| 83 | + </annotationProcessorPaths> | ||
| 84 | + </configuration> | ||
| 85 | + </plugin> | ||
| 86 | + </plugins> | ||
| 87 | + </build> | ||
| 88 | + | ||
| 65 | 89 | <properties> | |
| 66 | 90 | <java.version>11</java.version> | |
| 67 | 91 | <org.springframework.data.version>3.1.11</org.springframework.data.version> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + package org.baeldung.objectmapper; | ||
| 2 | + | ||
| 3 | + import org.springframework.boot.SpringApplication; | ||
| 4 | + import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| 5 | + | ||
| 6 | + @SpringBootApplication | ||
| 7 | + public class CassandraMapperApplication { | ||
| 8 | + | ||
| 9 | + public static void main(String[] args) { | ||
| 10 | + SpringApplication.run(CassandraMapperApplication.class, args); | ||
| 11 | + } | ||
| 12 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + package org.baeldung.objectmapper; | ||
| 2 | + | ||
| 3 | + import com.datastax.oss.driver.api.core.CqlIdentifier; | ||
| 4 | + import com.datastax.oss.driver.api.mapper.annotations.DaoFactory; | ||
| 5 | + import com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace; | ||
| 6 | + import com.datastax.oss.driver.api.mapper.annotations.Mapper; | ||
| 7 | + import org.baeldung.objectmapper.dao.CounterDao; | ||
| 8 | + import org.baeldung.objectmapper.dao.UserDao; | ||
| 9 | + | ||
| 10 | + @Mapper | ||
| 11 | + public interface DaoMapper { | ||
| 12 | + | ||
| 13 | + @DaoFactory | ||
| 14 | + UserDao getUserDao(@DaoKeyspace CqlIdentifier keyspace); | ||
| 15 | + | ||
| 16 | + @DaoFactory | ||
| 17 | + CounterDao getUserCounterDao(@DaoKeyspace CqlIdentifier keyspace); | ||
| 18 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,16 @@ | |||
| 1 | + package org.baeldung.objectmapper.dao; | ||
| 2 | + | ||
| 3 | + import com.datastax.oss.driver.api.mapper.annotations.Dao; | ||
| 4 | + import com.datastax.oss.driver.api.mapper.annotations.Increment; | ||
| 5 | + import com.datastax.oss.driver.api.mapper.annotations.Select; | ||
| 6 | + import org.baeldung.objectmapper.entity.Counter; | ||
| 7 | + | ||
| 8 | + @Dao | ||
| 9 | + public interface CounterDao { | ||
| 10 | + | ||
| 11 | + @Increment(entityClass = Counter.class) | ||
| 12 | + void incrementCounter(String id, long count); | ||
| 13 | + | ||
| 14 | + @Select | ||
| 15 | + Counter getCounterById(String id); | ||
| 16 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,38 @@ | |||
| 1 | + package org.baeldung.objectmapper.dao; | ||
| 2 | + | ||
| 3 | + import com.datastax.oss.driver.api.core.PagingIterable; | ||
| 4 | + import com.datastax.oss.driver.api.core.cql.BoundStatement; | ||
| 5 | + import com.datastax.oss.driver.api.core.cql.Row; | ||
| 6 | + import com.datastax.oss.driver.api.mapper.annotations.*; | ||
| 7 | + import org.baeldung.objectmapper.entity.User; | ||
| 8 | + | ||
| 9 | + @Dao | ||
| 10 | + public interface UserDao { | ||
| 11 | + | ||
| 12 | + @Insert | ||
| 13 | + void insertUser(User user); | ||
| 14 | + | ||
| 15 | + @Select | ||
| 16 | + User getUserById(int id); | ||
| 17 | + | ||
| 18 | + @Select | ||
| 19 | + PagingIterable<User> getAllUsers(); | ||
| 20 | + | ||
| 21 | + @Update | ||
| 22 | + void updateUser(User user); | ||
| 23 | + | ||
| 24 | + @Delete | ||
| 25 | + void deleteUser(User user); | ||
| 26 | + | ||
| 27 | + @GetEntity | ||
| 28 | + User getUser(Row row); | ||
| 29 | + | ||
| 30 | + @SetEntity | ||
| 31 | + BoundStatement setUser(BoundStatement udtValue, User user); | ||
| 32 | + | ||
| 33 | + @Query(value = "select * from user_profile where user_age > :userAge ALLOW FILTERING") | ||
| 34 | + PagingIterable<User> getUsersOlderThanAge(int userAge); | ||
| 35 | + | ||
| 36 | + @QueryProvider(providerClass = UserQueryProvider.class, entityHelpers = User.class, providerMethod = "getUsersOlderThanAge") | ||
| 37 | + PagingIterable<User> getUsersOlderThan(String age); | ||
| 38 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,34 @@ | |||
| 1 | + package org.baeldung.objectmapper.dao; | ||
| 2 | + | ||
| 3 | + import com.datastax.oss.driver.api.core.CqlSession; | ||
| 4 | + import com.datastax.oss.driver.api.core.PagingIterable; | ||
| 5 | + import com.datastax.oss.driver.api.core.cql.PreparedStatement; | ||
| 6 | + import com.datastax.oss.driver.api.core.cql.SimpleStatement; | ||
| 7 | + import com.datastax.oss.driver.api.mapper.MapperContext; | ||
| 8 | + import com.datastax.oss.driver.api.mapper.entity.EntityHelper; | ||
| 9 | + import com.datastax.oss.driver.api.querybuilder.QueryBuilder; | ||
| 10 | + import org.baeldung.objectmapper.entity.User; | ||
| 11 | + | ||
| 12 | + public class UserQueryProvider { | ||
| 13 | + | ||
| 14 | + private final CqlSession session; | ||
| 15 | + private final EntityHelper<User> userHelper; | ||
| 16 | + | ||
| 17 | + public UserQueryProvider(MapperContext context, EntityHelper<User> userHelper) { | ||
| 18 | + this.session = context.getSession(); | ||
| 19 | + this.userHelper = userHelper; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + public PagingIterable<User> getUsersOlderThanAge(String age) { | ||
| 23 | + SimpleStatement statement = QueryBuilder.selectFrom("user_profile") | ||
| 24 | + .all() | ||
| 25 | + .whereColumn("user_age") | ||
| 26 | + .isGreaterThan(QueryBuilder | ||
| 27 | + .bindMarker(age)) | ||
| 28 | + .build(); | ||
| 29 | + PreparedStatement preparedSelectUser = session.prepare(statement); | ||
| 30 | + return session | ||
| 31 | + .execute(preparedSelectUser.getQuery()) | ||
| 32 | + .map(result -> userHelper.get(result, true)); | ||
| 33 | + } | ||
| 34 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,31 @@ | |||
| 1 | + package org.baeldung.objectmapper.entity; | ||
| 2 | + | ||
| 3 | + import com.datastax.oss.driver.api.mapper.annotations.CqlName; | ||
| 4 | + import com.datastax.oss.driver.api.mapper.annotations.Entity; | ||
| 5 | + import com.datastax.oss.driver.api.mapper.annotations.HierarchyScanStrategy; | ||
| 6 | + | ||
| 7 | + @Entity | ||
| 8 | + @CqlName("admin_profile") | ||
| 9 | + @HierarchyScanStrategy(highestAncestor = User.class, includeHighestAncestor = true) | ||
| 10 | + public class Admin extends User { | ||
| 11 | + private String role; | ||
| 12 | + private String department; | ||
| 13 | + | ||
| 14 | + public String getRole() { | ||
| 15 | + return role; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + public void setRole(String role) { | ||
| 19 | + this.role = role; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + public String getDepartment() { | ||
| 23 | + return department; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public void setDepartment(String department) { | ||
| 27 | + this.department = department; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + } | ||
| 31 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,29 @@ | |||
| 1 | + package org.baeldung.objectmapper.entity; | ||
| 2 | + | ||
| 3 | + import com.datastax.oss.driver.api.mapper.annotations.Entity; | ||
| 4 | + import com.datastax.oss.driver.api.mapper.annotations.PartitionKey; | ||
| 5 | + | ||
| 6 | + @Entity | ||
| 7 | + public class Counter { | ||
| 8 | + | ||
| 9 | + @PartitionKey | ||
| 10 | + private String id; | ||
| 11 | + private long count; | ||
| 12 | + | ||
| 13 | + public String getId() { | ||
| 14 | + return id; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + public void setId(String id) { | ||
| 18 | + this.id = id; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public long getCount() { | ||
| 22 | + return count; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + public void setCount(long count) { | ||
| 26 | + this.count = count; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,58 @@ | |||
| 1 | + package org.baeldung.objectmapper.entity; | ||
| 2 | + | ||
| 3 | + import com.datastax.oss.driver.api.mapper.annotations.*; | ||
| 4 | + | ||
| 5 | + @Entity | ||
| 6 | + @CqlName("user_profile") | ||
| 7 | + public class User { | ||
| 8 | + @PartitionKey | ||
| 9 | + private int id; | ||
| 10 | + @CqlName("username") | ||
| 11 | + private String userName; | ||
| 12 | + @ClusteringColumn | ||
| 13 | + private int userAge; | ||
| 14 | + | ||
| 15 | + @Computed("writetime(userName)") | ||
| 16 | + private long writetime; | ||
| 17 | + | ||
| 18 | + public User() { | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public User(int id, String userName, int userAge) { | ||
| 22 | + this.id = id; | ||
| 23 | + this.userName = userName; | ||
| 24 | + this.userAge = userAge; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + public int getId() { | ||
| 28 | + return id; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + public void setId(int id) { | ||
| 32 | + this.id = id; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public String getUserName() { | ||
| 36 | + return userName; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + public void setUserName(String userName) { | ||
| 40 | + this.userName = userName; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public int getUserAge() { | ||
| 44 | + return userAge; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public void setUserAge(int userAge) { | ||
| 48 | + this.userAge = userAge; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + public long getWritetime() { | ||
| 52 | + return writetime; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + public void setWritetime(long writetime) { | ||
| 56 | + this.writetime = writetime; | ||
| 57 | + } | ||
| 58 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,96 @@ | |||
| 1 | + package org.baeldung.objectmapper; | ||
| 2 | + | ||
| 3 | + import com.datastax.oss.driver.api.core.CqlIdentifier; | ||
| 4 | + import com.datastax.oss.driver.api.core.CqlSession; | ||
| 5 | + import org.baeldung.objectmapper.dao.CounterDao; | ||
| 6 | + import org.baeldung.objectmapper.dao.UserDao; | ||
| 7 | + import org.baeldung.objectmapper.entity.Counter; | ||
| 8 | + import org.baeldung.objectmapper.entity.User; | ||
| 9 | + import org.junit.jupiter.api.Assertions; | ||
| 10 | + import org.junit.jupiter.api.BeforeAll; | ||
| 11 | + import org.junit.jupiter.api.Test; | ||
| 12 | + import org.springframework.boot.test.context.SpringBootTest; | ||
| 13 | + import org.testcontainers.containers.CassandraContainer; | ||
| 14 | + import org.testcontainers.junit.jupiter.Container; | ||
| 15 | + import org.testcontainers.junit.jupiter.Testcontainers; | ||
| 16 | + | ||
| 17 | + import java.util.List; | ||
| 18 | + | ||
| 19 | + @Testcontainers | ||
| 20 | + @SpringBootTest | ||
| 21 | + public class MapperLiveTest { | ||
| 22 | + | ||
| 23 | + private static final String KEYSPACE_NAME = "baeldung"; | ||
| 24 | + | ||
| 25 | + @Container | ||
| 26 | + private static final CassandraContainer cassandra = (CassandraContainer) new CassandraContainer("cassandra:3.11.2") | ||
| 27 | + .withExposedPorts(9042); | ||
| 28 | + | ||
| 29 | + static void setupCassandraConnectionProperties() { | ||
| 30 | + System.setProperty("spring.data.cassandra.keyspace-name", KEYSPACE_NAME); | ||
| 31 | + System.setProperty("spring.data.cassandra.contact-points", cassandra.getContainerIpAddress()); | ||
| 32 | + System.setProperty("spring.data.cassandra.port", String.valueOf(cassandra.getMappedPort(9042))); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + static UserDao userDao; | ||
| 36 | + static CounterDao counterDao; | ||
| 37 | + | ||
| 38 | + @BeforeAll | ||
| 39 | + static void setup() { | ||
| 40 | + setupCassandraConnectionProperties(); | ||
| 41 | + CqlSession session = CqlSession.builder().build(); | ||
| 42 | + | ||
| 43 | + String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS baeldung " + | ||
| 44 | + "WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};"; | ||
| 45 | + String useKeyspace = "USE baeldung;"; | ||
| 46 | + String createUserTable = "CREATE TABLE IF NOT EXISTS user_profile " + | ||
| 47 | + "(id int, username text, user_age int, writetime bigint, PRIMARY KEY (id, user_age)) " + | ||
| 48 | + "WITH CLUSTERING ORDER BY (user_age DESC);"; | ||
| 49 | + String createAdminTable = "CREATE TABLE IF NOT EXISTS admin_profile " + | ||
| 50 | + "(id int, username text, user_age int, role text, writetime bigint, department text, " + | ||
| 51 | + "PRIMARY KEY (id, user_age)) " + | ||
| 52 | + "WITH CLUSTERING ORDER BY (user_age DESC);"; | ||
| 53 | + String createCounter = "CREATE TABLE IF NOT EXISTS counter " + | ||
| 54 | + "(id text, count counter, PRIMARY KEY (id));"; | ||
| 55 | + | ||
| 56 | + session.execute(createKeyspace); | ||
| 57 | + session.execute(useKeyspace); | ||
| 58 | + session.execute(createUserTable); | ||
| 59 | + session.execute(createAdminTable); | ||
| 60 | + session.execute(createCounter); | ||
| 61 | + | ||
| 62 | + DaoMapper mapper = new DaoMapperBuilder(session).build(); | ||
| 63 | + userDao = mapper.getUserDao(CqlIdentifier.fromCql("baeldung")); | ||
| 64 | + counterDao = mapper.getUserCounterDao(CqlIdentifier.fromCql("baeldung")); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + @Test | ||
| 68 | + void givenUser_whenInsert_thenRetrievedDuringGet() { | ||
| 69 | + User user = new User(1, "JohnDoe", 31); | ||
| 70 | + userDao.insertUser(user); | ||
| 71 | + User retrievedUser = userDao.getUserById(1); | ||
| 72 | + Assertions.assertEquals(retrievedUser.getUserName(), user.getUserName()); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + @Test | ||
| 76 | + void givenCounter_whenIncrement_thenIncremented() { | ||
| 77 | + Counter users = counterDao.getCounterById("users"); | ||
| 78 | + long initialCount = users != null ? users.getCount(): 0; | ||
| 79 | + | ||
| 80 | + counterDao.incrementCounter("users", 1); | ||
| 81 | + | ||
| 82 | + users = counterDao.getCounterById("users"); | ||
| 83 | + long finalCount = users != null ? users.getCount(): 0; | ||
| 84 | + | ||
| 85 | + Assertions.assertEquals(finalCount - initialCount, 1); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + @Test | ||
| 89 | + void givenUser_whenGetUsersOlderThan_thenRetrieved() { | ||
| 90 | + User user = new User(2, "JaneDoe", 20); | ||
| 91 | + userDao.insertUser(user); | ||
| 92 | + List<User> retrievedUsers = userDao.getUsersOlderThanAge(30).all(); | ||
| 93 | + Assertions.assertEquals(retrievedUsers.size(), 1); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments