大數據

【最佳實踐】Solr集群數據遷移至Elasticsearch

Elasticsearch是一款非常強大的搜索引擎,可以讓你在海量的數據中快速找到想要的內容。例如,代碼搜索:可以幫助您找到相應的代碼倉庫,還可以實現代碼級的搜索及高亮顯示;網上shopping:可以幫助您推薦商品。

除了大數據搜索,Elasticsearch結合Logstash和Beats,還被廣泛應用在大數據實時分析領域,例如日誌分析、指標監控、信息安全等。幫助您探索海量的結構化和非結構化的數據,按需創建可視化報表、對監控數據設置報警閾值,甚至使用機器學習技術,自動識別異常狀況、及時發出警報。

在DBRanking 2019年5月的數據庫評測中,Elasticsearch在搜索引擎中排名第一,國內也有大量的公司在使用Elasticsearch。並且Elasticsearch在全球的下載量已經超過了3.5億次。如果您需要將Solr集群中的數據遷移至Elasticsearch,請跟著本文一步步來操作吧。

本文以阿里雲Elasticsearch為例,為您演示通過第三方社區提供的solr-to-es工具,將Solr節點中的文檔遷移到阿里雲Elasticsearch(簡稱ES)中的方法。

阿里雲Elasticsearch兼容開源Elasticsearch的功能,以及Security、Machine Learning、Graph、APM等商業功能,致力於數據分析、數據搜索等場景服務。支持5.5.3、6.3.2、6.7.0、6.8.0和7.4.0等版本,並提供了商業插件X-Pack服務。在開源Elasticsearch的基礎上提供企業級權限管控、安全監控告警、自動報表生成等功能。阿里雲Elasticsearch為您提供1個月的免費試用活動,單擊此處即可免費試用。

環境準備

  1. 創建阿里雲ES實例,要求版本為6.x,本文使用6.3.2 版本,詳情請參見創建阿里雲Elasticsearch實例

    **說明:** 本文使用的solr-to-es遷移工具僅支持阿里雲ES 6.x版本,其他版本需自行測試。
    
  2. 開啟目標阿里雲ES的自動創建索引功能,詳情請參見開啟自動創建索引
  3. 創建阿里雲ECS實例,本文使用CentOS 7.3版本,詳情請參見步驟一:創建ECS實例

    **說明:** ECS實例需要與阿里雲ES實例在同一區域和可用區,以及同一專有網絡VPC(Virtual Private Cloud)下。
    
  4. 在ECS上安裝Solr,本文使用5.0.0版本的Solr,詳情請參見Solr官方文檔
  5. 在ECS上安裝Python,要求3.0及以上版本,本文使用Python 3.6.2。
  6. 在ECS上安裝PySolr,要求3.3.3及以上,4.0以下版本。

安裝solr-to-es工具

  1. 連接ECS服務器,下載solr-to-es工具
  2. 進入setup.py所在的目錄,運行python setup.py install命令,安裝solr-to-es工具。
  3. 安裝成功後,參考以下命令進行文檔遷移。

    python __main__.py <solr_url>:8983/solr/<my_core>/select http://<username>:<password>@<elasticsearch_url>:9200 <elasticsearch_index> <doc_type>
|參數|說明|
|--|--|
|`<solr_url>`|Solr集群的完整訪問地址。例如,http://116.62.\*\*.\*\*。|
|`<my_core>`|遷移文檔對應的SolrCore的名稱。|
|`<username>`|阿里雲ES的訪問用戶名,默認為elastic。|
|`<password>`|阿里雲ES的訪問密碼,在創建實例時設定。|
|`<elasticsearch_url>`|阿里雲ES實例的內網或外網訪問地址。可在實例的基本信息頁面獲取,詳情請參見[查看實例的基本信息](https://help.aliyun.com/document_detail/67804.html?spm=a2c4g.11186623.2.19.18a87461Jp4xKZ#task-2449896)。|
|`<elasticsearch_index>`|待寫入的Solr文檔對應的索引名稱。|
|`<doc_type>`|對應索引的類型名稱。|

**說明:** 如果您使用的是其他版本的solr-to-es工具,也可以嘗試使用如下命令進行文檔遷移,參數詳情請參見[solr-to-es](https://github.com/o19s/solr-to-es)。
solr-to-es [-h] [--solr-query SOLR_QUERY] [--solr-fields COMMA_SEP_FIELDS]
                 [--rows-per-page ROWS_PER_PAGE] [--es-timeout ES_TIMEOUT]
                 solr_url elasticsearch_url elasticsearch_index doc_type
本案例使用以上命令會輸出`-bash: solr-to-es.py: command not found`的錯誤。

操作示例

通過以下命令,查詢名稱為my_core的SolrCore的所有文檔,寫入到阿里雲ES實例中。對應的索引為elasticsearch_index,索引類型為doc_type

  1. 在Solr環境中,進入solr-to-es-master/solr_to_es文件夾下。
  2. 執行以下命令。

    python __main__.py 'http://116.62.**.**:8983/solr/my_core/select?q=*%3A*&wt=json&indent=true' 'http://elastic:替換密碼@es-cn-so4lwf40ubsrf****.public.elasticsearch.aliyuncs.com:9200' elasticsearch_index doc_type
|參數|說明|
|--|--|
|`q`|Solr的查詢語法,必選,可以使用運算符。`*%3A*`表示查詢所有文檔。|
|`wt`|返回的數據類型,支持JSON、XML、Python、Ruby、CSV等格式。|
|`indent`|返回結果是否需要格式化展示,默認為`false`。|

其他參數說明請參見[表 1](https://help.aliyun.com/document_detail/143003.html?spm=a2c4g.11174283.6.832.4d097958P4tOeH#table-lnc-t0w-8ud)。
  1. 登錄目標阿里雲ES實例的Kibana控制檯。

    登錄控制檯的具體操作步驟請參見[登錄Kibana控制檯](https://help.aliyun.com/document_detail/122668.html?spm=a2c4g.11186623.2.22.18a87461Jp4xKZ#task-761873)。
    
  2. 單擊左側導航欄的Dev Tools(開發工具),在Console中執行以下命令,查看阿里雲ES集群服務中是否已成功創建elasticsearch_index索引。

    GET _cat/indices?v
  3. 執行以下命令,查看遷移成功的文檔詳情。

    GET /elasticsearch_index/doc_type/_search
查詢成功後,返回如下結果。
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "elasticsearch_index",
        "_type" : "doc_type",
        "_id" : "Tz8WNW4BwRjcQciJ****",
        "_score" : 1.0,
        "_source" : {
          "id" : "2",
          "title" : [
            "test"
          ],
          "_version_" : 1648195017403006976
        }
      },
      {
        "_index" : "elasticsearch_index",
        "_type" : "doc_type",
        "_id" : "Tj8WNW4BwRjcQciJ****",
        "_score" : 1.0,
        "_source" : {
          "id" : "1",
          "title" : [
            "change.me"
          ],
          "_version_" : 1648195007391203328
        }
      }
    ]
  }
}

相關活動

更多折扣活動,請訪問阿里雲 Elasticsearch 官網

阿里雲 Elasticsearch 商業通用版,1核2G ,SSD 20G首月免費
阿里雲 Logstash 2核4G首月免費

image.png
image.png

Leave a Reply

Your email address will not be published. Required fields are marked *