|
| 1 | +/* |
| 2 | + * Copyright 2025 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; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | + |
| 20 | +import org.springframework.boot.CommandLineRunner; |
| 21 | +import org.springframework.boot.SpringApplication; |
| 22 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.data.cassandra.core.CassandraOperations; |
| 25 | +import org.springframework.data.domain.Limit; |
| 26 | + |
| 27 | +/** |
| 28 | + * Basic Spring Boot application. |
| 29 | + * |
| 30 | + * @author Mark Paluch |
| 31 | + */ |
| 32 | +@SpringBootApplication |
| 33 | +public class AotApplication { |
| 34 | + |
| 35 | + public static void main(String[] args) { |
| 36 | + SpringApplication.run(AotApplication.class, args); |
| 37 | + } |
| 38 | + |
| 39 | + @Bean |
| 40 | + CommandLineRunner commandLineRunner(UserRepository repository, CassandraOperations operations) |
| 41 | + throws InterruptedException { |
| 42 | + |
| 43 | + operations.getCqlOperations().execute("CREATE INDEX IF NOT EXISTS user_username ON users (uname);"); |
| 44 | + operations.getCqlOperations().execute( |
| 45 | + "CREATE CUSTOM INDEX IF NOT EXISTS users_lname_idx_1 ON users (lname) USING 'org.apache.cassandra.index.sasi.SASIIndex';"); |
| 46 | + |
| 47 | + /* |
| 48 | + Cassandra secondary indexes are created in the background without the possibility to check |
| 49 | + whether they are available or not. So we are forced to just wait. *sigh* |
| 50 | + */ |
| 51 | + Thread.sleep(1000); |
| 52 | + |
| 53 | + return args -> { |
| 54 | + |
| 55 | + User user = new User(); |
| 56 | + user.setId(42L); |
| 57 | + user.setUsername("heisenberg"); |
| 58 | + user.setFirstname("Walter"); |
| 59 | + user.setLastname("White"); |
| 60 | + |
| 61 | + user.setCurrent(new Address("308 Negra Arroyo Lane", "87104", "Albuquerque")); |
| 62 | + user.setPrevious(Collections.singletonList(new Address("12000 – 12100 Coors Rd SW", "87045", "Albuquerque"))); |
| 63 | + |
| 64 | + repository.save(user); |
| 65 | + |
| 66 | + System.out.println("------- Annotated Single -------"); |
| 67 | + System.out.println(repository.findUserByIdIn(1000)); |
| 68 | + System.out.println(repository.findUserByIdIn(42)); |
| 69 | + |
| 70 | + System.out.println("------- Derived Single -------"); |
| 71 | + System.out.println(repository.findUserByUsername(user.getUsername())); |
| 72 | + |
| 73 | + System.out.println("------- Derived SASI -------"); |
| 74 | + System.out.println(repository.findUsersByLastnameStartsWith("White", Limit.of(1))); |
| 75 | + }; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments