I am new to Redis and developing code using Spring Boot + Spring Data Redis
example. When I saved the records, I see KEYS gets created and out of these keys 4 are HASH
, 1 ZSET
and all others are SET
.
I did not see in the Spring docs, meaning of each KEY is getting saved. .
127.0.0.1:6379> KEYS *
1) "persons:c5cfd49d-6688-4b83-a9b7-be55dd1c36ad"
2) "persons:firstname:bran"
3) "persons:39e14dae-fa23-4935-948f-1922d668d1c2"
4) "persons:f0b6dd26-8922-4a36-bd2a-792a17dddff7"
5) "persons:address.city:Achalpur"
6) "persons:e493385a-64ae-42be-8398-51757153d273:idx"
7) "persons:053cdfea-e430-4e1c-acbd-ac40050b10cd:idx"
8) "persons:firstname:rickon"
9) "persons:e493385a-64ae-42be-8398-51757153d273"
10) "persons:address.country:India"
11) "persons:e7fc3ebe-9b48-48a8-a5f4-33a0e21f782f:idx"
12) "persons:firstname:sansa"
13) "persons:address:location"
14) "persons:firstname:robb"
15) "persons:firstname:jon"
16) "persons:lastname:snow"
17) "persons:e7fc3ebe-9b48-48a8-a5f4-33a0e21f782f"
18) "persons:c5cfd49d-6688-4b83-a9b7-be55dd1c36ad:idx"
19) "persons:lastname:stark"
20) "persons:f0b6dd26-8922-4a36-bd2a-792a17dddff7:idx"
21) "persons:053cdfea-e430-4e1c-acbd-ac40050b10cd"
22) "persons:39e14dae-fa23-4935-948f-1922d668d1c2:idx"
23) "persons:firstname:arya"
24) "persons:83cd4f58-c272-4d81-9023-8c66c8ac34b0:idx"
25) "persons:83cd4f58-c272-4d81-9023-8c66c8ac34b0"
26) "persons:address.city:Nagpur"
27) "persons:firstname:eddard"
28) "persons"
Person.java
@Data
@EqualsAndHashCode(exclude = { "children" })
@NoArgsConstructor
@AllArgsConstructor
@Builder
@RedisHash("persons")
public class Person {
private @Id String id;
private @Indexed String firstname;
private @Indexed String lastname;
private Gender gender;
private Address address;
private @Reference List<Person> children;
}
Address.java
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Address {
private @Indexed String city;
private @Indexed String country;
private @GeoIndexed Point location;
}
Gender.java
public enum Gender {
FEMALE, MALE
}
RedisExampleBootApplication.java
@SpringBootApplication
public class RedisExampleBootApplication implements CommandLineRunner{
@Autowired PersonRepository repository;
public static void main(String[] args) {
SpringApplication.run(RedisExampleBootApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Address address1 = Address.builder().city("the north").country("winterfell").location(new Point(52.9541053, -1.2401016)).build();
Address address2 = Address.builder().city("Casterlystein").country("Westerland").location(new Point(51.5287352, -0.3817819)).build();
Person eddard = Person.builder().firstname("eddard").lastname("stark").gender(Gender.MALE).address(address1).build();
Person robb = Person.builder().firstname("robb").lastname("stark").gender(Gender.MALE).address(address2).build();
Person sansa = Person.builder().firstname("sansa").lastname("stark").gender(Gender.FEMALE).address(address1).build();
Person arya = Person.builder().firstname("arya").lastname("stark").gender(Gender.FEMALE).address(address2).build();
Person bran = Person.builder().firstname("bran").lastname("stark").gender(Gender.MALE).address(address1).build();
Person rickon = Person.builder().firstname("rickon").lastname("stark").gender(Gender.MALE).address(address2).build();
Person jon = Person.builder().firstname("jon").lastname("snow").gender(Gender.MALE).address(address1).build();
repository.save(eddard);
repository.save(robb);
repository.save(sansa);
repository.save(arya);
repository.save(bran);
repository.save(rickon);
repository.save(jon);
List<Person> starks = repository.findByLastname(eddard.getLastname());
System.out.println("Person ="+starks.size());
}
}