IBM Cloud Docs
將檔案上傳至 Elasticsearch

將檔案上傳至 Elasticsearch

少數 Elasticsearch 特性容許索引從檔案系統上的檔案讀取,因此 IBM Cloud® Databases for Elasticsearch 可讓您將檔案上傳至部署。 這些檔案儲存在已知位置,且 Elasticsearch 已配置成容許它從該位置讀取檔案。

上傳至部署的檔案會使用索引及檔案系統上的磁碟資源。 在上傳檔案之前,請確定您 調整部署

基本程序

  1. 您在用戶端 base64 編碼檔案。
  2. base64 字串會儲存為 Elasticsearch 部署中名為 ibm_file_sync 的索引中的文件。
  3. 您可以從 Cloud Databases API 觸發檔案同步。
  4. Elasticsearch 叢集中的所有節點都會從索引下載檔案內容,解碼 base64,並還原部署磁碟上 /data/ibm_file_sync/current 目錄中的檔案。
  5. 定期且在重新啟動時,會重新同步化檔案,以確保它們存在於所有節點上。
  6. 會刪除位於磁碟上但不在索引中的檔案。 您可以從索引中移除檔案,以從磁碟中刪除檔案。

Elasticsearch 中的索引是 ibm_file_sync
磁碟上檔案的位置是 /data/ibm_file_sync/current

在 Elasticsearch 7 中,API 會移除文件類型。 如需詳細資料,請參閱 Elasticsearch 說明文件。 從舊版更新時,請記住這一點特別好。

將檔案上傳至索引

索引中文件的結構如下: name 是檔案的檔名,blob 是 base64-encoded 檔案內容,md5 是檔案內容的選用雜湊值。 根據版本來分割索引的建議對映。

若為 Elasticsearch 6:

curl -X PUT "https://user:password@host:port/ibm_file_sync" -H 'Content-Type: application/json' -d'
{
    "mappings": {
        "files": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "blob": {
                    "type": "binary"
                },
                "md5": {
                    "type": "text"
                }
            }
        }
    }
}'

若為 Elasticsearch 7 (請注意移除 files 小節):

curl -X PUT "https://user:password@host:port/ibm_file_sync" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "blob": {
        "type": "binary"
      },
      "md5": {
        "type": "text"
      }
    }
  }
}'

URL 是來自部署的 https 連線字串

若要使用索引,請將檔案內容編碼為 base64。 若要將 Bash 中的範例檔案 README.md 編碼, ENC=$(base64 -w 0 README.md)。 然後,對內容 HASH=$(md5sum README.md) 建置總和檢查。

下載函數會比較每次同步執行時的雜湊值,如果值自前次同步以來未變更,則不會嘗試新的下載。 如果索引中有任何文件沒有 md5 值,則會重新嘗試所有下載。

接下來,將文件上傳至索引。 請注意,URL 中也提供檔名。

若為 Elasticsearch 6:

curl -X PUT "https://user:password@host:port/ibm_file_sync/files/README1.md" -H 'Content-Type: application/json' -d'
{
    "name": "README1.md",
    "blob": '"\"$ENC\""',
    "md5": '"\"$HASH\""'
}'

若為 Elasticsearch 7 (請注意,只會變更 URL):

curl -X PUT "https://user:password@host:port/ibm_file_sync/_doc/README1.md" -H 'Content-Type: application/json' -d'
{
    "name": "README1.md",
    "blob": '"\"$ENC\""',
    "md5": '"\"$HASH\""'
}'

您可以驗證上傳的資料。

若為 Elasticsearch 6:

curl https://user:password@host:port/ibm_file_sync/files/README.md?pretty

若為 Elasticsearch 7:

curl https://user:password@host:port/ibm_file_sync/_doc/README.md?pretty

如果一切順利進行,則傳回的資料看起來會像這個 (縮短) 範例。 "md5" 欄位可以包含檔名及雜湊。

若為 Elasticsearch 6:

{
  "_index" : "ibm_file_sync",
  "_type" : "files",
  "_id" : "README1.md",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "README1.md",
    "blob" : "IyBF ... KWBgCg==",
    "md5" : "270f60e62d3d37add3702ced7f6969a1  README.md"
  }
}

若為 Elasticsearch 7:

{
  "_index" : "ibm_file_sync",
  "_id" : "README1.md",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "README1.md",
    "blob" : "IyBF ... KWBgCg==",
    "md5" : "270f60e62d3d37add3702ced7f6969a1  README.md"
  }
}

正在將檔案同步至磁碟

將檔案上傳至索引之後,它們可以同步至磁碟。 從 Cloud Databases API 呼叫 /elasticsearch/file_syncs 端點。

curl -X POST \
https://api.{region}.databases.cloud.ibm.com/v4/ibm/deployments/{id}/elasticsearch/file_syncs \
-H 'authorization: Bearer <token>'

region 是部署所在的地區,且 URL 的 id (CRN) 部分需要進行 URL 編碼。 如需相關資訊,請參閱 API 參考資料

呼叫會啟動並傳回 作業,以便您可以監視其進度。 在傳回的作業完成之後,索引中的內容會呈現在叢集中的所有節點上。

可以上傳及同步任何數目的檔案。 未驗證檔案的內容。 請確保 Elasticsearch可以處理它們。

使用檔案

Elasticsearch 特性使用檔案系統上的檔案,其作法是在定義索引時接受檔案路徑。 上傳的檔案 example.txt 位於 /data/ibm_file_sync/current/example.txt。 此清單包含範例,並非詳盡無遺。