Spring Data【Spring Data Redis, Spring Data ElasticSearch】(2)-Comprehensive Detailed Explanation (Learning Summary --- From Introduction to Deepening)

 

Table of contents

四、 Spring Data Redis

五、 Spring Data ElasticSearch


四、 Spring Data Redis

Redis is a memory-based data structure storage system that can be used as a database or a cache. It supports multiple types of data structures, these data structure types are String (string), List (list), Set (collection), Hash (hash) and Zset (ordered collection).

Spring Data Redis allows JAVA programs to access redis services through a simple configuration. Its underlying layer is a high-level encapsulation of redis development kits (such as Jedis and JedisPool).

1 Spring Data Redis project construction

1. Install redis

2. Install Redis Desktop Manager

3. Create a SpringBoot project and add the Spring Data Redis starter dependency when creating it.

4. Write the configuration file

spring:
  redis:
    # Redis 服务器主机。
    host: localhost
    # Redis 服务器端口。
    port: 6379
    jedis:
      pool:
        # 连接池在给定时间可以分配的最大连接数。
        max-active: 8
        # 连接池中空闲连接的最大数量。
        max-idle: 8
        # 连接池中空闲连接的最小数量。
        min-idle: 0

5. Test

// 注:对象名必须叫 redisTemplate,否则由于容器中有多个 RedisTemplate 类型对象造成无法注入
@Autowired
private RedisTemplate redisTemplate;
@Test
public void t1() {
    //获取操作 string 数据的工具
    ValueOperations operations = redisTemplate.opsForValue();
    operations.set("name","tong"); // 存
    Object name = operations.get("name"); // 取
    System.out.println(name);
}

2 Serializers

In the introductory case, we saw that what Spring Data Redis stores in Redis is a string of binary data. This is because when Spring Data Redis saves data, there is a serializer working at the bottom, which will save the data (key and value) are serialized according to certain rules before being stored. spring-data-redis provides the following serializers:

JdkSerializationRedisSerializer: default, serialized as binary data

StringRedisSerializer: simple serialization to string

GenericToStringSerializer: can serialize any object to a string

Jackson2JsonRedisSerializer: Serialize objects as json strings

GenericJackson2JsonRedisSerializer: Same function as above, but easier to deserialize

OxmSerializer: Serialize objects to xml

If you want to change the serializer used, you can set it through the redisTemplate object.

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());

3 Spring Data Redis operation string

There are five common types in Redis. Spring Data Redis provides a xxxOperations tool class for each data type, which are:

 ValueOperations: used to operate string type data

 HashOperations: used to operate hash type data

 ListOperations: used to operate list type data

 SetOperations: used to operate set type numbers

  ZSetOperations: used to operate zset

Common methods of RedisTemplate:

 delete(K key): delete key-value pair

 delete(Collection keys): Batch delete

Common methods of ValueOperations:

 void set(K key, V value): set key-value pair

 void set(K key, V value, long timeout, TimeUnit unit): set time-sensitive key-value pairs

 void multiSet(Map map): set key-value pairs in batches

 V get(Object key): get the value

 List multiGet(Collection keys): Get values ​​in batches

 Long size(K key): Get the length of the value

 Long increment(K key): digital value auto-increment

 Long increment(K key, long delta): digital type value auto-increment set value

 Long decrement(K key): the number type value is decremented

 Long decrement(K key, long delta): digital type value is decremented from the set value

4 Spring Data Redis operations hash

Common methods of HashOperations:

 put(H key, HK hashKey, HV value):新增 hash

 HV get(H key, Object hashKey): get the hash value

 Boolean hasKey(H key, Object hashKey): Determine whether the hash has the key

 Set keys(H key): get all keys of hash

 List values(H key): get all values ​​of hash

 Map entries(H key): Get all key-value pairs of hash

 Long delete(H key, Object... hashKeys): delete the hash value according to the hash key

5 Spring Data Redis operation list

 Long leftPush(K key, V value): add elements to the left

 Long leftPushAll(K key, V... values): Add multiple elements to the left

 Long rightPush(K key, V value): add elements to the right

 Long rightPushAll(K key, V... values): add multiple elements to the right

 V index(K key, long index) Query according to the index:

        0 starts from the left: 0 1 2...

        -1 starts from the right: -1 -2 -3...

 List range(K key, long start, long end): query according to the index range

 V leftPop(K key): Delete an element from the left

 V rightPop(K key): Delete an element from the right

6 Spring Data Redis operations set

 Long add(K key, V... values): add elements

 Set members(K key): View all elements

 V randomMember(K key): Obtain an element randomly

 Long remove(K key, Object... values): delete multiple elements

 Set intersect(K key, K otherKey): the intersection of two sets

 Set union(K key, K otherKey): the union of two sets

7 Spring Data Redis Operations zse

Boolean add(K key, V value, double score): add element

 Double incrementScore(K key, V value, double delta): increase or decrease the score for the element

 Double score(K key, Object o): query the score of an element

 Long rank(K key, Object o): query the rank of an element in the collection, starting from 0

 Set range(K key, long start, long end): Obtain the list of elements according to the ranking range

 Set> rangeWithScores(K key, long start, long end): Obtain the list of elements according to the ranking range, including scores

 Set rangeByScore(K key, double min, double max): Get the element list according to the score range

 Set> rangeByScoreWithScores(K key, double min, double max): Obtain the list of elements according to the score range, including the score

 Long zCard(K key): Statistical set size

 Long count(K key, double min, double max): Count the number of elements in the score interval

 Long remove(K key, Object... values): delete elements according to key

 Long removeRange(K key, long start, long end): delete elements according to the ranking range

 Long removeRangeByScore(K key, double min, double max): delete elements according to the score range

 

8 Use Repository to operate Redis 

Generally, we use the Template method to operate Redis more often, because Redis is not like a relational database that stores most of the objects, and the operation of Redis is not necessarily related to objects. So we only need to understand how to use Repository to operate Redis.

创建实体类:
// 表示将对象存入 key 为 student 的 hash 中
@RedisHash("student")
public class Student {
    // hash 的键存的是 id 字段
    @Id
    private String id;
    private String name;
    private Integer age;
    // 省略 getter/setter、构造方法、toString 方法
    }
    创建 Repository 接口:
public interface StudentRepository extends CrudRepository<Student, String> {}

 test:

@Autowired
private StudentRepository studentRepository;
@Test
public void t1() {
    Student student = new Student("1001", "懒羊羊", 10);
    studentRepository.save(student);
}

五、 Spring Data ElasticSearch

1 concept

Elasticsearch is a real-time distributed search and analytics engine. It encapsulates the Lucene framework at the bottom and can provide distributed full-text search services.

Spring Data ElasticSearch is the product of SpringData technology's encapsulation of ElasticSearch's native API. By encapsulating the native API, programmers can easily perform various operations on ElasticSearch.

2 Elasticsearch review

2.1 Core Concepts

 Index (index): An index is a collection of documents with somewhat similar characteristics, similar to the concept of a library in a relational database.

 Type (type): A type is a logical classification/partition in an index, similar to the concept of a data table in a relational database.

 Document (document): A document is a basic information unit that can be indexed, similar to the concept of a record in a relational database.

 Field (Fied): A document consists of multiple fields. Fields with the same name in different types of documents must have the same type, which is similar to the concept of fields in relational databases.

Comparison of concepts in ElasticSearch and relational databases: 

Note: The concept of type is deleted after ES7.X, an index does not represent a library, but a table. We use ES7 in our course, so the concept comparison at this time is:

 

2.2 Install ElasticSearch

        2.2.1 Install ES service

          1. Unzip the elasticsearch compressed file

          2. Unzip elasticsearch-analysis-ik, and copy the unzipped folder to the plugins directory of elasticsearch

          3. Modify the yml file under the config directory of the es server and add the following configuration

                    http.cors.enabled: true 

                    http.cors.allow-origin: "*"

           4. Start bin/elasticsearch.bat

           5. Visit http://127.0.0.1:9200 

2.2.2 Install ES graphical management software kibana

ES needs a graphical management software for our operation, here we install kibana.

        1. Unzip the kibana compressed file

        2. Start bin/kibana.bat

        3. Visit http://127.0.0.1:5601 

 2.3 Use Restful style http request to operate ES

create index

路径:localhost:9200/索引
提交方式:put
请求体:
{ 

     "settings":{ 
            "number_of_shards":5, 
            "number_of_replicas":1
            },
      "mappings":
               { 
         "properties":{
              "id":{ 
                  "type":"long", 
                  "store":true
                 },
              "title":{
                    "type":"text", 
                    "store":true, 
                    "index":true, 
                    "analyzer":"ik_smart
                   },
              "content":{
                    "type":"text", 
                    "store":true, 
                    "index":true, 
                    "analyzer":"ik_max_word"
                }
            }
      }
}

Create/modify documents

路径:localhost:9200/索引/_doc/[文档id]
提交方式:POST
请求体:
{ 
        "id":1, 
        "title":"ElasticSearch是一个基于Lucene的搜索服务器", 
        "content":"提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。"
}

delete document

路径:localhost:9200/索引/_doc/文档 id
提交方式:DELETE

query document

路径:localhost:9200/索引/_search
提交方式:POST
请求体:
{ 
    "query": {
         "查询类型": {
              "查询属性": "查询条件值"
             }
        }
 }
查询类型:match_all、term、range、fuzzy等

Query documents using SQL

After ES7.X version, ES supports native SQL query documents

路径:localhost:9200/_sql?format=txt
提交方式:POST
请求体:
{
    "query": "SELECT * FROM travel where id = 1"
}

2.4 Use JAVA native code to operate ES

Using JAVA native code to operate ES code is very cumbersome, we just need to understand it.

Here's a piece of code that creates an index:

CreateIndexRequest request = new CreateIndexRequest(indexName);
request.settings(Settings.builder()
        .put("index.number_of_shards", 3)
        .put("index.number_of_replicas", 2)
);
// 创建索引结构,es7 及以后去掉了映射类型
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject()
   .startObject("properties")
      .startObject("name")
        .field("type", "text")
        .field("analyzer", "ik_smart")
      .endObject()
      .startObject("age")
        .field("type", "integer")
      .endObject()
      .startObject("desc")
        .field("type", "text")
        .field("analyzer", "ik_smart")
      .endObject()
      .startObject("id")
        .field("type", "integer")
      .endObject()
    .endObject()
.endObject();
request.mapping(builder);
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
// 指示是否所有节点都已确认请求
boolean acknowledged = response.isAcknowledged();
// 指示是否在超时之前为索引中的每个分片启动了必需的分片副本数
boolean shardsAcknowledged = response.isShardsAcknowledged();
if (acknowledged || shardsAcknowledged) {
    log.info("创建索引成功!索引名称为{}", indexName);
    return true;
}
return false;

Since it is very cumbersome to operate ES code using JAVA native code, we use the Spring Data ElasticSearch framework to operate ES more in development. Next, we will learn to use Spring Data ElasticSearch.

 3 Spring Data ElasticSearch project construction

1. Create a SpringBoot project and add the Spring Data Redis starter dependency when creating it.

2. Write the configuration file

spring:
  elasticsearch:
    rest:
    uris: http://localhost:9200

3. Create entity class

@Document(indexName = "travel")
public class Product {
    @Id
    @Field(type = FieldType.Integer,store = true)
    private Integer id;

    @Field(type = FieldType.text,store = true,analyzer = "ik_max_word")
    private String productName;

    @Field(type = FieldType.text,store = true,analyzer = "ik_max_word")
    private String productDesc;
}

@Document: marked on the class, marking the entity class as a document object, generally has the following attributes, as follows:

         indexName: the name of the corresponding index library

         shards: number of shards

         replicas: the number of replicas

         createIndex: Whether to automatically create an index @Id: mark on the member variable, mark a field as the primary key @Field: mark on the member variable, mark it as a field in the document, and specify the field mapping attribute:

         type: field type

         index: whether to index, the default is true

         store: whether to store, the default is false

         analyzer: the name of the tokenizer

         searchAnalyzer: the name of the word breaker when searching

4. Create a ProductRepository interface to inherit from ElasticsearchRepository

5. Inject in test class

Guess you like

Origin blog.csdn.net/m0_58719994/article/details/132004502