8-数据写入与数据删除

21 阅读7分钟

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

经验建议

优先选择批量写入方式,次之选择单条写入。
批量写入数据避免数据包过大,限制总条数或者数据量,建议10mb以内且10000条以下。

数据准备

{
    "end_station_id": "6756.05",
    "member_casual": "member",
    "@timestamp": "2022-12-11T05:36:16.645Z",
    "end_lat": "40.762009",
    "start_station_name": "W 21 St & 6 Ave",
    "start_lat": "40.74173969",
    "ended_at": "2022-01-22 14:53:18",
    "start_station_id": "6140.05",
    "start_lng": "-73.99415556",
    "rideable_type": "classic_bike",
    "started_at": "2022-01-22 14:28:32",
    "tags": [
        "citibike",
        "tripdata"
    ],
    "ride_id": "D1FCEF55EB4A807F",
    "end_station_name": "W 44 St & 11 Ave",
    "end_lng": "-73.996975"
}

mapping参考

DELETE citibike - 202202
PUT citibike - 202202
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1,
    "refresh_interval": "1s"
  },
  "mappings": {
    "_source": {
      "enabled": true
    },
    "dynamic": true,
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "end_lat": {
        "type": "float"
      },
      "end_lng": {
        "type": "float"
      },
      "end_station_id": {
        "type": "keyword"
      },
      "end_station_name": {
        "type": "text"
      },
      "ended_at": {
        "type": "date",
        "format": [
          "yyyy - MM - dd HH:mm:ss"
        ]
      },
      "member_casual": {
        "type": "keyword"
      },
      "ride_id": {
        "type": "keyword"
      },
      "rideable_type": {
        "type": "keyword"
      },
      "start_lat": {
        "type": "float"
      },
      "start_lng": {
        "type": "float"
      },
      "start_station_id": {
        "type": "keyword"
      },
      "start_station_name": {
        "type": "text"
      },
      "started_at": {
        "type": "date",
        "format": [
          "yyyy - MM - dd HH:mm:ss"
        ]
      },
      "tags": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "citibike": {},
    "citibike - tripdata": {},
    "citibike - tripdata - 2022": {}
  }
}

template参考

DELETE _index_template/citibike-tripdata-2022
PUT _index_template/citibike-tripdata-2022
{
  "index_patterns": [
    "citibike-20*",
    "citibike-tripdata-20*"
  ],
  "_meta": {},
  "priority": 0,
  "template": {
    "settings": {
      "number_of_replicas": 1,
      "number_of_shards": 1,
      "refresh_interval": "1s"
    },
    "mappings": {
      "_source": {
        "enabled": true
      },
      "dynamic": true,
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "end_lat": {
          "type": "float"
        },
        "end_lng": {
          "type": "float"
        },
        "end_station_id": {
          "type": "keyword"
        },
        "end_station_name": {
          "type": "text"
        },
        "ended_at": {
          "type": "date",
          "format": [
            "yyyy-MM-dd HH:mm:ss"
          ]
        },
        "member_casual": {
          "type": "keyword"
        },
        "ride_id": {
          "type": "keyword"
        },
        "rideable_type": {
          "type": "keyword"
        },
        "start_lat": {
          "type": "float"
        },
        "start_lng": {
          "type": "float"
        },
        "start_station_id": {
          "type": "keyword"
        },
        "start_station_name": {
          "type": "text"
        },
        "started_at": {
          "type": "date",
          "format": [
            "yyyy-MM-dd HH:mm:ss"
          ]
        },
        "tags": {
          "type": "keyword"
        }
      }
    },
    "aliases": {
      "citibike": {},
      "citibike-tripdata": {},
      "citibike-tripdata-2022": {}
    }
  }
}

单条写入数据

PUT _doc - 新增或者更新数据

DELETE citibike-tripdata-sample
PUT citibike-tripdata-sample
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "end_station_id": { "type": "keyword" },
      "member_casual": { "type": "keyword" },
      "@timestamp": { "type": "date" },
      "end_lat": { "type": "float" },
      "start_station_name": { "type": "text" },
      "start_lat": { "type": "float" },
      "ended_at": { 
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "start_station_id": { "type": "keyword" },
      "start_lng": { "type": "float" },
      "rideable_type": { "type": "keyword" },
      "started_at": { 
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "tags": { "type": "keyword" },
      "ride_id": { "type": "keyword" },
      "end_station_name": { "type": "text" },
      "end_lng": { "type": "float" }
    }
  }
}

# 插入文档示例
PUT citibike-tripdata-sample/_doc/1
{
  "end_station_id": "6876.04",
  "member_casual": "membera",
  "@timestamp": "2022-12-11T05:36:16.650Z",
  "end_lat": "40.76590936",
  "start_station_name": "E 66 St & Madison Ave",
  "start_lat": "40.76800889305947",
  "ended_at": "2022-01-30 15:39:08",
  "start_station_id": "6969.08",
  "start_lng": "-73.96845281124115",
  "rideable_type": "electric_bike",
  "started_at": "2022-01-30 15:33:25",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "9F1E1AB4E5C8E11D",
  "end_station_name": "Central Park S & 6 Ave",
  "end_lng": "-73.97634151"
}

PUT _create - 显示新增数据

DELETE citibike-202202
PUT citibike-202202/_create/ride_id_9F1E1AB4E5C8E11D
{
  "end_station_id": "6876.04",
  "member_casual": "membera",
  "@timestamp": "2022-12-11T05:36:16.650Z",
  "end_lat": "40.76590936",
  "start_station_name": "E 66 St & Madison Ave",
  "start_lat": "40.76800889305947",
  "ended_at": "2022-01-30 15:39:08",
  "start_station_id": "6969.08",
  "start_lng": "-73.96845281124115",
  "rideable_type": "electric_bike",
  "started_at": "2022-01-30 15:33:25",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "9F1E1AB4E5C8E11D",
  "end_station_name": "Central Park S & 6 Ave",
  "end_lng": "-73.97634151"
}

POST _doc - POST方式填充,自动生成id

DELETE citibike-202202
POST citibike-202202/_doc
{
  "end_station_id": "6876.04",
  "member_casual": "membera",
  "@timestamp": "2022-12-11T05:36:16.650Z",
  "end_lat": "40.76590936",
  "start_station_name": "E 66 St & Madison Ave",
  "start_lat": "40.76800889305947",
  "ended_at": "2022-01-30 15:39:08",
  "start_station_id": "6969.08",
  "start_lng": "-73.96845281124115",
  "rideable_type": "electric_bike",
  "started_at": "2022-01-30 15:33:25",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "9F1E1AB4E5C8E11D",
  "end_station_name": "Central Park S & 6 Ave",
  "end_lng": "-73.97634151"
}
POST citibike-202202/_doc/ride_id_9F1E1AB4E5C8E11D
{
  "end_station_id": "6876.04",
  "member_casual": "membera",
  "@timestamp": "2022-12-11T05:36:16.650Z",
  "end_lat": "40.76590936",
  "start_station_name": "E 66 St & Madison Ave",
  "start_lat": "40.76800889305947",
  "ended_at": "2022-01-30 15:39:08",
  "start_station_id": "6969.08",
  "start_lng": "-73.96845281124115",
  "rideable_type": "electric_bike",
  "started_at": "2022-01-30 15:33:25",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "9F1E1AB4E5C8E11D",
  "end_station_name": "Central Park S & 6 Ave",
  "end_lng": "-73.97634151"
}

POST _create

DELETE citibike-202202
POST citibike-202202/_create/ride_id_9F1E1AB4E5C8E11D
{
  "end_station_id": "6876.04",
  "member_casual": "membera",
  "@timestamp": "2022-12-11T05:36:16.650Z",
  "end_lat": "40.76590936",
  "start_station_name": "E 66 St & Madison Ave",
  "start_lat": "40.76800889305947",
  "ended_at": "2022-01-30 15:39:08",
  "start_station_id": "6969.08",
  "start_lng": "-73.96845281124115",
  "rideable_type": "electric_bike",
  "started_at": "2022-01-30 15:33:25",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "9F1E1AB4E5C8E11D",
  "end_station_name": "Central Park S & 6 Ave",
  "end_lng": "-73.97634151"
}

image.png

DELETE citibike-202202
PUT citibike-202202
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 6,
    "refresh_interval": "15s"
  }
}
POST citibike-202202/_doc/ride_id_9F1E1AB4E5C8E11D?refresh=true&wait_for_active_shards=1&op_type=index&routing=abc&require_alias=false&timeout=1m&version=1&version_type=external
{
  "end_station_id": "6876.04",
  "member_casual": "membera",
  "@timestamp": "2022-12-11T05:36:16.650Z",
  "end_lat": "40.76590936",
  "start_station_name": "E 66 St & Madison Ave",
  "start_lat": "40.76800889305947",
  "ended_at": "2022-01-30 15:39:08",
  "start_station_id": "6969.08",
  "start_lng": "-73.96845281124115",
  "rideable_type": "electric_bike",
  "started_at": "2022-01-30 15:33:25",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "9F1E1AB4E5C8E11D",
  "end_station_name": "Central Park S & 6 Ave",
  "end_lng": "-73.97634151"
}

image.png

Mapping自动更新

DELETE citibike-202202
PUT citibike-202202
{
  "mappings": {
    "dynamic": true
  }
}
PUT citibike-202202/_create/ride_id_571ECC43B92B62CF
{
  "end_station_id": "6920.03",
  "member_casual": "member",
  "@timestamp": "2022-12-11T05:36:16.655Z"
}
PUT citibike-202202/_create/ride_id_1729DAF28B7D3CCC
{
  "end_station_id": "5515.08",
  "member_casual": "member",
  "@timestamp": "2022-12-11T05:36:16.656Z",
  "end_lat": "40.723346832180155",
  "start_station_name": "Division St & Bowery",
  "start_lat": "40.714193",
  "ended_at": "2022-01-20 17:14:52",
  "start_station_id": "5270.08",
  "start_lng": "-73.996732",
  "rideable_type": "electric_bike",
  "started_at": "2022-01-20 17:03:17",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "1729DAF28B7D3CCC",
  "end_station_name": "E 4 St & Ave B",
  "end_lng": "-73.98265913128853"
}

批量写入数据

_bulk

DELETE citibike-202202
POST _bulk
{
  "create": {
    "_index": "citibike-202202",
    "_id": "ride_id_571ECC43B92B62CF",
    "require_alias": false
  }
}
{
  "end_station_id": "6920.03",
  "member_casual": "member",
  "@timestamp": "2022-12-11T05:36:16.655Z",
  "end_lat": "40.76584941",
  "start_station_name": "W 49 St & 8 Ave",
  "start_lat": "40.76227205",
  "ended_at": "2022-01-02 12:58:05",
  "start_station_id": "6747.06",
  "start_lng": "-73.98788205",
  "rideable_type": "classic_bike",
  "started_at": "2022-01-02 12:55:34",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "571ECC43B92B62CF",
  "end_station_name": "W 54 St & 9 Ave",
  "end_lng": "-73.98690506"
}
{
  "index": {
    "_index": "citibike-202202",
    "_id": "ride_id_D1FCEF55EB4A807F",
    "require_alias": false
  }
}
{
  "end_station_id": "6756.05",
  "member_casual": "member",
  "@timestamp": "2022-12-11T05:36:16.645Z",
  "end_lat": "40.762009",
  "start_station_name": "W 21 St & 6 Ave",
  "start_lat": "40.74173969",
  "ended_at": "2022-01-22 14:53:18",
  "start_station_id": "6140.05",
  "start_lng": "-73.99415556",
  "rideable_type": "classic_bike",
  "started_at": "2022-01-22 14:28:32",
  "tags": [
    "citibike",
    "tripdata"
  ],
  "ride_id": "D1FCEF55EB4A807F",
  "end_station_name": "W 44 St & 11 Ave",
  "end_lng": "-73.996975"
}

image.png

image.png

Response响应参数概要说明

{
  "took": 94,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "citibike-202202",
        "_id": "ZwFfhYUBWoL2UdPoLMoC",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "citibike-202202",
        "_id": "ride_idD1FCEF55EB4A807F",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 201
      }
    }
  ]
}

索引删除数据

image.png

image.png

image.png

image.png

image.png

image.png

Response响应参数概要说明

{
  "took": 4,
  "errors": false,
  "items": [
    {
      "delete": {
        "_index": "citibike-202202",
        "_id": "ride_id_571ECC43B92B62CF",
        "_version": 2,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 1,
        "status": 200
      }
    },
    {
      "delete": {
        "_index": "citibike-202202",
        "_id": "ride_idD1FCEF55EB4A807F",
        "_version": 2,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 2,
          "failed": 0
        },
        "_seq_no": 3,
        "_primary_term": 1,
        "status": 200
      }
    }
  ]
}

条件删除数据

_delete_by_query 执行条件删除

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png