在大多數(shù)情況下,你的數(shù)據(jù)包含一個(gè)以 create_date 命名的字段。 即使沒有日期字段,處理各種格式和時(shí)區(qū)的日期對(duì)數(shù)據(jù)倉庫來說也是一個(gè)重大挑戰(zhàn)。 與此類似,如果要檢測(cè)變化的數(shù)據(jù),則必須準(zhǔn)確設(shè)置日期字段。
在 Elasticsearch 中還有一個(gè)選項(xiàng)可以自動(dòng)將服務(wù)器的日期設(shè)置為字段。
我們將使用攝取管道屬性的?set?和?date?處理器。
創(chuàng)建攝入管道
首先我們需要設(shè)置一個(gè)時(shí)間戳字段。 之后我們將使用日期處理器來更新字段。
日期處理器有一些功能。 target_field?屬性就是其中之一。 如果未定義 target_field?屬性,它將計(jì)算 field?并寫入一個(gè)名為 @timestamp?的新字段。 但我們想要改變一個(gè)已經(jīng)存在的字段。
PUT _ingest/pipeline/sales-timestamp
{
"description": "Set two different timestamp fields.",
"processors": [
{
"set": {
"field": "timestamp",
"value": "{{{_ingest.timestamp}}}"
}
},
{
"date": {
"field": "timestamp",
"target_field": "tr_timestamp",
"timezone": "+0300",
"formats": [ "ISO8601"]
}
}
]
}
運(yùn)行上面的腳本后,系統(tǒng)將顯示(“acknowledged”:true)消息:
{
"acknowledged": true
}
此外,還可以使用 DELETE 命令進(jìn)行刪除或使用 GET 命令驗(yàn)證屬性。 重新運(yùn)行 PUT 命令應(yīng)該足以更新管道。
GET _ingest/pipeline
DELETE _ingest/pipeline/sales-timestamp
下一步是使用管道創(chuàng)建索引。 對(duì)于新建立的攝取管道,必須設(shè)置 index.default_pipeline。
即使它會(huì)自動(dòng)填充 date 字段,你仍然需要在索引的映射中定義它們
# Create "sales" Index
PUT sales
{
"settings": {
"index.default_pipeline": "sales-timestamp"
},
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"tr_timestamp": { "type": "date" },
"name": { "type": "text" },
"authour": { "type": "keyword" }
}
}
}
寫入數(shù)據(jù)
當(dāng)你想同時(shí)向一個(gè)索引添加多個(gè)數(shù)據(jù)時(shí),可以使用 Bulk API。 它嘗試從每一行解析你的腳本。 這意味著你不能在批量插入期間使用格式化的 JSON。
通過這種技術(shù),Elasticsearch 會(huì)自動(dòng)為數(shù)據(jù)分配一個(gè) ID。
POST sales/_bulk
{"index":{}}
{"name":"The Lord of the Rings: The Fellowship of the Ring","authour":"J. R. R. Tolkien"}
{"index":{}}
{"name":"The Lord of the Rings 2: The Two Towers","authour":"J. R. R. Tolkien"}
下一步將允許我們列出或搜索我們的數(shù)據(jù)。
GET sales/_search?filter_path=**.hits
{
"size": 5,
"query": {
"match_all": {}
}
}
上面運(yùn)行的結(jié)果為:
{
"hits": {
"hits": [
{
"_index": "sales",
"_id": "rVjrTooBxPLM4Lwr4CwQ",
"_score": 1,
"_source": {
"name": "The Lord of the Rings: The Fellowship of the Ring",
"authour": "J. R. R. Tolkien",
"tr_timestamp": "2023-09-01T07:06:35.783+03:00",
"timestamp": "2023-09-01T04:06:35.783682Z"
}
},
{
"_index": "sales",
"_id": "rljrTooBxPLM4Lwr4CwQ",
"_score": 1,
"_source": {
"name": "The Lord of the Rings 2: The Two Towers",
"authour": "J. R. R. Tolkien",
"tr_timestamp": "2023-09-01T07:06:35.792+03:00",
"timestamp": "2023-09-01T04:06:35.792130Z"
}
}
]
}
}
在這種情況下,你應(yīng)該密切關(guān)注 tr_timestamp 和 timestamp 數(shù)據(jù)。 tr_timestamp 列中的數(shù)據(jù)末尾有 “+03:00”。
你可以向索引添加更多數(shù)據(jù)。文章來源:http://www.zghlxwxcb.cn/news/detail-718757.html
POST sales/_bulk
{"index":{}}
{"name":"The Lord of the Rings 3: The Return of the King", "authour":"J. R. R. Tolkien"}
{
"_index": "sales",
"_id": "r1j0TooBxPLM4LwreSyl",
"_score": 1,
"_source": {
"name": "The Lord of the Rings 3: The Return of the King",
"authour": "J. R. R. Tolkien",
"tr_timestamp": "2023-09-01T07:15:59.397+03:00",
"timestamp": "2023-09-01T04:15:59.397509Z"
}
}
更多關(guān)于 pipeline 的使用方法,請(qǐng)閱讀文章 “Elasticsearch:ingest pipelines - 使用技巧和竅門”。文章來源地址http://www.zghlxwxcb.cn/news/detail-718757.html
到了這里,關(guān)于Elasticsearch:自動(dòng)使用服務(wù)器時(shí)間設(shè)置日期字段并更新時(shí)區(qū)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!