使用從Kafka connect實現從oracle到kafka的數據同步

參考https://blog.csdn.net/ismr_m/article/details/79813838

https://mp.weixin.qq.com/s?src=11&timestamp=1579153063&ver=2099&signature=6tatHFHkl*dhNeA2L533wHUkaTxXUAgAhlflX5MqEMjIraCIfnOMN7e*JkFCBo0ZVv9RQMwTI42lS5d0ys2CwKW7aksHCGkv5Qyi6XSAeMZo6N7iyct8rq2Ct4txQoaN&new=1

confluent的包鏈接:https://pan.baidu.com/s/1JfCxRMd2eGdSGnWbrcd0Hg 提取碼:uqx4

以上工具來源:https://blog.csdn.net/ismr_m/article/details/79813838(十分感謝)

一、創建oracle的數據

SQL> create table test_user(id number(19) not null primary key, username varchar2(100),password varchar2(100),modified timestamp(0) default SYSTIMESTAMP not null);

 創建自增序列,使主鍵自增:

 

SQL> create sequence test_user_seq start with 1 increment by 1;

創建觸發器: 

 

SQL> create or replace trigger test_user_seq_tr

  2    before insert or update on test_user for each row

  3  begin

  4    if inserting then

  5    select test_user_seq.NEXTVAL into :new.id from dual;

  6    end if;

  7  END;

  8  /

爲時間列創建一個索引:

 

SQL> create index test_modified_index on test_user (modified);

插入數據:

 

SQL> insert into test_user(username,password) values('tom','111');

1 row created.

SQL> insert into test_user(username,password) values('bob','222');

1 row created.

SQL> insert into test_user(username,password) values('jhon','333');

1 row created.

SQL> insert into test_user(username,password) values('rose','444');

1 row created.

SQL> insert into test_user (username,password) values('amy','555');

1 row created.

SQL> commit;

 

這裏要注意insert之後要commit一下,否則topic中讀不到這條記錄。

二、啓動kafka

三、導入oracle驅動

將ojdbc的jar放入kafka安裝包下的lib目錄以及confluent工具下的share/java/kafka下。注意ojdbc.jar的版本要和jdk以及oracle的版本對應。我這裏使用的是jdk1.8+oracle11g。

4.在kafka安裝包下的config中的connect-standalone.properties文件中修改plugin.path:

最下面的地址指向你的share/java/kafka下

5.在config下創建一個connector的配置文件:

 

[root@localhost config]# vi wyh-oracle-connector.properties

添加如下內容:

 

name=test-oracle-connector

connector.class=io.confluent.connect.jdbc.JdbcSourceConnector

tasks.max=1

connection.password=wyhpwd

connection.url=jdbc:oracle:thin:@192.168.184.129:1522:orcl

connection.user=wyh

table.whitelist=TEST_USER

mode=incrementing

incrementing.column.name=ID

topic.prefix=test-oracle-

這裏一定要注意在oracle內部表名和列名都是大寫,所以配置中table.whitelist和incrementing.column.name都要大寫,否則報錯。此處的name是連接器名稱,是唯一的,不能重複。connector.class是連接器的類名。在連接oracel時,username和password最好和url分開寫,因爲分開寫會進行隱藏密碼,否則直接寫在url中會明文顯示密碼。table.whitelist是表示允許複製的表。mode表示增量查詢的模式,也就是根據哪種模式來跟蹤數據的更新。incrementing.column.name是具體以哪個列名來作爲mode 下的跟蹤。topic.prefix是在表很多的情況下可以根據topic的前綴對每一個表都有一個不同的topic,這裏我們只有一個表。輸出數據的topic就是topic.prefix加上表名。這個topic會自動創建。topic.prefix是必須指定的。

6.啓動connector:

 

[root@localhost kafka_2.12-2.1.0]# bin/connect-standalone.sh config/connect-standalone.properties config/wyh-oracle-connector.properties

7.啓動consumer:

 

[root@localhost kafka_2.12-2.1.0]# bin/kafka-console-consumer.sh --bootstrap-server 192.168.184.128:9092 --from-beginning --topic test-oracle-TEST_USER

當然,上面的192.168.184.128:9092應該換成各自的bootstrap.servers

圖中payload中的數據就是數據庫中的每條數據。

 

這裏先暫存一個疑問:爲什麼Oracle中的id是1,2,3,4,5,但是topic中讀取消息的id是字母(如:AQ==)。

 

針對於mode爲incrementing的connector,只適用於insert類型的數據變化,是通過檢測新增的ID大於之前讀取的最大的ID來確定是否是要更新的數據。對於update和delete的數據在這種模式下無法檢測更新。

 

這裏我們再插入一條數據:

 

SQL> insert into test_user(username,password) values('bill','666');

再看topic:

這樣也就實現了oracle中的數據在Insert時與kafka同步。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章