使用Kettle實(shí)現(xiàn)從DB至ElasticSearch的數(shù)據(jù)遷移

Kettle 是一款基于Java開(kāi)發(fā)的開(kāi)源(Apache許可)的ETL工具,因其友好的許可来涨、可視化的設(shè)計(jì)器、可擴(kuò)展的插件機(jī)制,而被廣泛使用

Elasticsearch是一款基于Apache Lucene 開(kāi)發(fā)的開(kāi)源(混合型許可Apache許可+ Elastic License)的搜索服務(wù)+存儲(chǔ)引擎扇调,對(duì)文本型大數(shù)據(jù)量的搜索的亮眼性能而被廣泛使用

使用版本
ElasticSearch 6.6.2+xpack 6.6.2(定制版)
Kettle 8.3.0.1 + Elasticsearch bulk insert (版本適配)
DB:mysql 5.6+

為什么選擇Kettle

  • 可視:代碼不僅僅只是是用來(lái)執(zhí)行的,還必須考慮可維護(hù)性抢肛,看著一坨一坨的代碼狼钮,即使跑的很正常、注釋寫(xiě)的再好捡絮,也不一定有人耐下心看熬芜,Kettle插件的天生模塊化,把關(guān)注點(diǎn)進(jìn)行了很好的分離

  • 統(tǒng)一:所有人福稳、所有實(shí)現(xiàn)是一致的涎拉,便于知識(shí)沉淀、便于知識(shí)的流淌

功能實(shí)現(xiàn)

實(shí)現(xiàn)效果

① 創(chuàng)建ES Mapping【可選】

Tips:使用手工創(chuàng)建Mapping的圆,能夠更好的兼容實(shí)際的數(shù)據(jù)鼓拧、定制分詞引擎或通過(guò)指定Keyword等來(lái)降低不必要的索引存儲(chǔ),Elasticsearch bulk insert 可以根據(jù)index名稱進(jìn)行檢測(cè)越妈,如果指定的名稱不存在季俩,則會(huì)創(chuàng)建Index

put http://es-ip:9200/zqcust-ges-party-test

{
  "settings": {
    "index": {
      "number_of_shards": "5",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "RegistCapi": {
          "type": "text"
        },
        "Status": {
          "type": "keyword", "null_value": "NULL"
        },
        "BelongOrg": {
          "type": "text"
        },
        "CreditCode": {
          "type": "text"
        },
        "No": {
          "type": "text"
        },
        "OperName": {
          "type": "text"
        },
        "Address": {
          "type": "text"
        },
        "EconKind": {
          "type": "text"
        },
        "RegistCapiUnit": {
          "type": "text"
        },
        "Province": {
          "type": "text"
        },
        "TermStart": {
          "type": "date"
        },
        "StartDate": {
          "type": "date"
        },
        "TeamEnd": {
          "type": "date"
        },
        "Scope": {
          "type": "text","analyzer": "ik_max_word"
        },
        "company_name": {
          "type": "text"
        },
        "PhoneNumber": {
          "type": "text"
        },
        "CheckDate": {
          "type": "date"
        },
        "ProvinceCode": {
          "type": "text"
        }
      }
    }
  }
}

② 源數(shù)據(jù)獲取

文檔使用“Table input”通過(guò)JDBC從數(shù)據(jù)庫(kù)獲取源數(shù)據(jù)(Kettle支持幾乎能想到的所有數(shù)據(jù)源類(lèi)型)

需加載的數(shù)據(jù)表:


讀-加載數(shù)據(jù)

來(lái)?xiàng)l數(shù)據(jù)預(yù)覽:


數(shù)據(jù)預(yù)覽

③ 字段類(lèi)型處理

通過(guò)設(shè)置字段類(lèi)型可以減少因類(lèi)型轉(zhuǎn)換而導(dǎo)致的異常

字段類(lèi)型

④ Elasticsearch bulk insert配置

配置index相關(guān)信息,如果指定的index不存在梅掠,則會(huì)新建:

Index配置

字段映射:


字段映射

配置ES服務(wù)信息:

配置ES服務(wù)

配置cluster.name:

服務(wù)名稱配置

使用證書(shū)驗(yàn)證的配置:

使用證書(shū)驗(yàn)證

性能調(diào)整

JDBC性能參數(shù)

fetchSize 參數(shù)調(diào)整酌住,參數(shù)效用參考附錄

讀-性能參數(shù)

對(duì)于大批量的數(shù)據(jù)讀店归,使用fetchSize、defaultRowPrefetch 會(huì)帶來(lái)比較好的性能赂韵,關(guān)于參數(shù)詳細(xì)說(shuō)明娱节,可參考附錄內(nèi)容。

ES批量插入?yún)?shù)

使用Kettle本身的線程配置機(jī)制祭示,右鍵修改插件運(yùn)行線程數(shù):

多線程配置

運(yùn)行

直接在Spoon中運(yùn)行

在Spoon的GUI窗口直接執(zhí)行肄满,一般在開(kāi)發(fā)階段

圖像窗口運(yùn)行

命令行執(zhí)行

在Linux上將命令存為.sh文件,并設(shè)置文件可執(zhí)行:

/soft/data-integration-8.3.0.1/pan.sh  -file="/soft/kettle_run_file/zq-ges-party-oracle-to-es.ktr"  -level=Minimal >> /soft/kettle_run_file/run_to_es.log

在windows將命令存亡.cmd文件质涛,執(zhí)行文件:

C:\data-integration-8.3.0.1\data-integration\Pan.bat  /file:C:\kettle_App\zq-ges-party-oracle-to-es.ktr >> C:\kettle_App\log.txt

使用Pentaho Server執(zhí)行

將Transformations(轉(zhuǎn)換)發(fā)布到Pentaho Server上執(zhí)行

其他事項(xiàng)

依賴包放到Kettle的classpath下

將依賴的數(shù)據(jù)庫(kù)驅(qū)動(dòng)放置 ${Kettle_home}/lib目錄下稠歉,重啟Kettle加載

Kettle本地版本編譯

從github下載源碼進(jìn)行本地編譯,注意 Maven的遠(yuǎn)程倉(cāng)庫(kù)一定要參考官方的 setting.xml進(jìn)行配置:

官方提供地址:https://raw.githubusercontent.com/pentaho/maven-parent-poms/master/maven-support-files/settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <!--本地倉(cāng)庫(kù)地址-->
  <localRepository>D:\dev_tools\maven-repository</localRepository>
  <!-- This is the recommended settings.xml for development of Hitachi Vantara projects. -->
  
  <!--
  If your wish to mirror everything through pentaho-public's repo uncomment bellow. Not recommended  for external developers.
  -->
  <!--
  <mirrors>
    <mirror>
      <id>pentaho-public</id>
      <url>http://nexus.pentaho.org/content/groups/omni</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  -->

  <!--
  You might want to tweak the 'updatePolicy' configuration to fit your need on having updated snapshots and releases. Our recommendation is to set it to 'never' and run maven with the '-U' flag when needed.
  -->
  <profiles>
    <profile>
      <id>pentaho</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <repositories>
        <repository>
          <id>pentaho-public</id>
          <name>Pentaho Public</name>
          <url>http://nexus.pentaho.org/content/groups/omni</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>pentaho-public</id>
          <name>Pentaho Public</name>
          <url>http://nexus.pentaho.org/content/groups/omni</url>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

  <!-- this lets you call plugins from these groups in their short form -->
  <pluginGroups>
    <pluginGroup>org.pentaho.maven.plugins</pluginGroup>
    <pluginGroup>com.pentaho.maven.plugins</pluginGroup>
    <pluginGroup>com.github.spotbugs</pluginGroup>
  </pluginGroups>
</settings>

執(zhí)行構(gòu)建:

mvn clean install -DskipTests

插件依賴的ES版本調(diào)整

因Kettle默認(rèn)版本所使用的ES客戶端版本為 6.6.2并不支持xpack-transport的連接汇陆,需要對(duì)依賴庫(kù)進(jìn)行添加:

    <dependency>
       <groupId>org.elasticsearch.client</groupId>
       <artifactId>x-pack-transport</artifactId>
       <version>6.6.2</version>
    </dependency>
        <dependency>
       <groupId>org.elasticsearch.plugin</groupId>
       <artifactId>x-pack-core</artifactId>
       <version>6.6.2</version>
    </dependency>
    <dependency>
       <groupId>com.unboundid</groupId>
       <artifactId>unboundid-ldapsdk</artifactId>
       <version>4.0.8</version>
    </dependency>

將原代碼中使用的TransportClient進(jìn)行替換:

ElasticSearchBulk.java的481行:

    //PreBuiltTransportClient tClient = new PreBuiltTransportClient( settingsBuilder.build() );
    PreBuiltXPackTransportClient tClient = new PreBuiltXPackTransportClient( settingsBuilder.build() );

ElasticSearchBulkDialog.java的907行:

 try ( PreBuiltXPackTransportClient client = new PreBuiltXPackTransportClient( settingsBuilder.build() ) ){
 ……
 }

注意:不同版本的插件有可能會(huì)有差異怒炸,當(dāng)前使用的是8.3.0.1源碼;

插件的遠(yuǎn)程調(diào)試

首先配置啟動(dòng)參數(shù)毡代,使用遠(yuǎn)程調(diào)試阅羹,將參數(shù)添加到 spoon.sh或spoon.bat中:

"-Xdebug" "-Xnoagent" "-Djava.compiler=NONE" "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8080"

在Eclipse中配置遠(yuǎn)程調(diào)試:

遠(yuǎn)程調(diào)試

打上斷點(diǎn),可以進(jìn)行代碼調(diào)試了

附錄:
JDBC fetchsize 參數(shù)說(shuō)明:https://blog.csdn.net/bisal/article/details/82735614
JDBC fetchsize 對(duì)比:http://makejavafaster.blogspot.com/2015/06/jdbc-fetch-size-performance.html
Kettle插件開(kāi)發(fā)說(shuō)明:https://help.pentaho.com/Documentation/8.2/Developer_Center/PDI/Extend
阿里巴巴 數(shù)據(jù)遷移中間件: https://github.com/alibaba/canal

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末教寂,一起剝皮案震驚了整個(gè)濱河市捏鱼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌酪耕,老刑警劉巖导梆,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異迂烁,居然都是意外死亡看尼,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)盟步,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)藏斩,“玉大人,你說(shuō)我怎么就攤上這事却盘≡肿拢” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵谷炸,是天一觀的道長(zhǎng)北专。 經(jīng)常有香客問(wèn)我,道長(zhǎng)旬陡,這世上最難降的妖魔是什么拓颓? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮描孟,結(jié)果婚禮上驶睦,老公的妹妹穿的比我還像新娘砰左。我一直安慰自己,他們只是感情好场航,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布缠导。 她就那樣靜靜地躺著,像睡著了一般溉痢。 火紅的嫁衣襯著肌膚如雪僻造。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,578評(píng)論 1 305
  • 那天孩饼,我揣著相機(jī)與錄音髓削,去河邊找鬼。 笑死镀娶,一個(gè)胖子當(dāng)著我的面吹牛立膛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播梯码,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼宝泵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了轩娶?” 一聲冷哼從身側(cè)響起儿奶,我...
    開(kāi)封第一講書(shū)人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎罢坝,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體搅窿,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡嘁酿,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了男应。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片闹司。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖沐飘,靈堂內(nèi)的尸體忽然破棺而出游桩,到底是詐尸還是另有隱情,我是刑警寧澤耐朴,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布借卧,位于F島的核電站,受9級(jí)特大地震影響筛峭,放射性物質(zhì)發(fā)生泄漏铐刘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一影晓、第九天 我趴在偏房一處隱蔽的房頂上張望镰吵。 院中可真熱鬧檩禾,春花似錦、人聲如沸疤祭。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)勺馆。三九已至戏售,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間谓传,已是汗流浹背蜈项。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留续挟,地道東北人紧卒。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像诗祸,于是被迫代替她去往敵國(guó)和親跑芳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容