云搜索服务 CSS-使用Elasticsearch加速关系型数据库的查询分析:前提条件

时间:2024-12-19 10:11:26

前提条件

  • 已具备安全模式的Elasticsearch集群和MySQL数据库,且两者在同一个VPC与安全组内。
  • MySQL数据库中已经有待同步的数据。

    本文以如下表结构和初始数据举例。

    1. MySQL中创建一个学生信息表:
      CREATE TABLE `student` (
        `dsc` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
        `age` smallint unsigned DEFAULT NULL,
        `name` varchar(32) COLLATE utf8mb4_general_ci NOT NULL,
        `id` int unsigned NOT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
    2. MySQL中插入3个学生的初始数据:
      INSERT INTO student (id,name,age,dsc)
      VALUES 
      ('1','Jack Ma Yun','50','Jack Ma Yun is a business magnate, investor and philanthropist.'),
      ('2','will smith','22','also known by his stage name the Fresh Prince, is an actor, rapper, and producer.'),
      ('3','James Francis Cameron','68','the director of avatar');
  • Elasticsearch集群中已完成索引创建,且与MySQL中表相对应。
    本文执行如下命令创建Elasticsearch集群的索引。
    PUT student
    {
      "settings": {
        "number_of_replicas": 0,
        "number_of_shards": 3
    	},
      "mappings": {
        "properties": {
          "id": {
            "type": "keyword"
    		},
          "name": {
            "type": "short"
    		},
          "age": {
            "type": "short"
    		},
          "desc": {
            "type": "text"
    		}
        }
      }
    }

    其中的“number_of_shards”“number_of_replicas”需根据具体业务场景进行配置。

support.huaweicloud.com/bestpractice-css/css_07_0024.html