canal初探
我計劃以後將單位的搜尋專案,慢慢過渡到ES叢集,然後實現準實時的搜尋.
canal可以感知資料庫的變化,作為一個mysql的偽slave,我可以透過canal獲取資料庫變化,然後批次重新整理到ES叢集.
canal是阿里開源的一款產品..
雖然感覺國產開源不靠譜.但是沒辦法..洋人沒做,自己又不會做.
主頁:
下載地址:
/releases
開發API:
/wiki/ClientAPI
下載canal,解壓然後修改配置檔案.
canal.properties
canal.zkServers=192.168.16.105:2181,192.168.16.106:2181,192.168.16.108:2181,192.168.16.109:2181,192.168.16.110:2181
canal.destinations= songod
canal.instance.global.spring.xml = classpath:spring/default-instance.xml
songod/instance.properties
canal.instance.master.address = 192.168.16.98:3306
canal.instance.master.journal.name =
canal.instance.master.position =
canal.instance.master.timestamp =
# username/password
canal.instance.dbUsername = canal
canal.instance.dbPassword = canal
canal.instance.defaultDatabaseName = songod
canal.instance.connectionCharset = UTF-8
# table regex
canal.instance.filter.regex = .*\\..*
# table black regex
canal.instance.filter.black.regex =
然後執行 startup.sh
執行如下程式,接收資料變更.
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>1.0.21</version>
</dependency>
參考:
http://blog.csdn.net/bbirdsky/article/details/41479479
canal可以感知資料庫的變化,作為一個mysql的偽slave,我可以透過canal獲取資料庫變化,然後批次重新整理到ES叢集.
canal是阿里開源的一款產品..
雖然感覺國產開源不靠譜.但是沒辦法..洋人沒做,自己又不會做.
主頁:
下載地址:
/releases
開發API:
/wiki/ClientAPI
下載canal,解壓然後修改配置檔案.
canal.properties
canal.zkServers=192.168.16.105:2181,192.168.16.106:2181,192.168.16.108:2181,192.168.16.109:2181,192.168.16.110:2181
canal.destinations= songod
canal.instance.global.spring.xml = classpath:spring/default-instance.xml
songod/instance.properties
canal.instance.master.address = 192.168.16.98:3306
canal.instance.master.journal.name =
canal.instance.master.position =
canal.instance.master.timestamp =
# username/password
canal.instance.dbUsername = canal
canal.instance.dbPassword = canal
canal.instance.defaultDatabaseName = songod
canal.instance.connectionCharset = UTF-8
# table regex
canal.instance.filter.regex = .*\\..*
# table black regex
canal.instance.filter.black.regex =
然後執行 startup.sh
執行如下程式,接收資料變更.
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- import com.alibaba.otter.canal.client.CanalConnector;
- import com.alibaba.otter.canal.client.CanalConnectors;
- import com.alibaba.otter.canal.protocol.CanalEntry.Column;
- import com.alibaba.otter.canal.protocol.CanalEntry.Entry;
- import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
- import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
- import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;
- import com.alibaba.otter.canal.protocol.CanalEntry.RowData;
- import com.alibaba.otter.canal.protocol.Message;
- public class T {
- public static void main(String args[]) {
- // 建立連結
- CanalConnector connector = CanalConnectors.newClusterConnector("192.168.16.105:2181,192.168.16.108:2181",
- "songod", "", "");
- int batchSize = 100;
- connector.connect();
- connector.subscribe(".*\\..*");
- while (true) {
- long batchId = -1;
- try {
- Message message = connector.getWithoutAck(batchSize, new Long(5), TimeUnit.SECONDS); // 獲取指定數量的資料
- batchId = message.getId();
- int size = message.getEntries().size();
- System.out.println("batchSize:" + size);
- printEntry(message.getEntries());
- connector.ack(batchId); // 提交確認
- } catch (Exception e) {
- connector.rollback(batchId); // 處理失敗, 回滾資料
- connector.disconnect();
- }
- }
- }
- private static void printEntry(List<Entry> entrys) {
- for (Entry entry : entrys) {
- if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN
- || entry.getEntryType() == EntryType.TRANSACTIONEND) {
- continue;
- }
- RowChange rowChage = null;
- try {
- rowChage = RowChange.parseFrom(entry.getStoreValue());
- } catch (Exception e) {
- throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:" + entry.toString(),
- e);
- }
- EventType eventType = rowChage.getEventType();
- System.out.println(String.format("================> binlog[%s:%s] , name[%s,%s] , eventType : %s",
- entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(),
- entry.getHeader().getSchemaName(), entry.getHeader().getTableName(), eventType));
- for (RowData rowData : rowChage.getRowDatasList()) {
- Map<String, Object> map = null;
- if (eventType == EventType.DELETE) {
- map = printColumn(rowData);
- } else if (eventType == EventType.INSERT) {
- map = printColumn(rowData);
- } else if (eventType == EventType.UPDATE) {
- map = printColumn(rowData);
- }
- System.out.print(eventType + ":");
- System.out.println(map);
- }
- }
- }
- private static Map<String, Object> printColumn(RowData rowData) {
- Map<String, Object> map = new HashMap<String, Object>();
- for (Column column : rowData.getBeforeColumnsList()) {
- map.put(column.getName(), column.getValue());
- }
- return map;
- }
- }
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<version>1.0.21</version>
</dependency>
參考:
http://blog.csdn.net/bbirdsky/article/details/41479479
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2102706/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 「從零單排canal 03」 canal原始碼分析大綱原始碼
- 聊聊canal的BooleanMutexBooleanMutex
- canal資料同步
- canal安裝部署
- 資料同步利器 - canal
- 聊聊canal的CanalAdapterServiceAPT
- 聊聊canal的CanalAdapterWorkerAPT
- Canal 原始碼走讀原始碼
- Canal PHP[實戰系列]PHP
- Canal原始碼分析----MetaManager原始碼
- 阿里canal部署和應用阿里
- Canal高可用架構部署架構
- 大資料技術 - Canal大資料
- docker canal 踩坑歷程Docker
- 「從零單排canal 01」 canal 10分鐘入門(基於1.1.4版本)
- Canal adapter 同步 ElasticSearch 記錄APTElasticsearch
- 全量同步Elasticsearch方案之CanalElasticsearch
- 全量同步 Elasticsearch 方案之 CanalElasticsearch
- canal mysql select許可權粒度MySql
- docker中搭建canal監聽mysql例子DockerMySql
- 中介軟體---Binlog傳輸同步---Canal
- 使用canal.adapter同步資料到MySQLAPTMySql
- 認識資料實時同步神器—Canal
- 【Canal】資料同步的終極解決方案,阿里巴巴開源的Canal框架當之無愧!!阿里框架
- Deno 初探
- ELK初探
- Nginx 初探Nginx
- Mobx 初探
- jQuery初探jQuery
- 初探IndexedDBIndex
- 初探PWA
- Puppeteer 初探
- Redis初探Redis
- 初探Firewalld
- Serverless初探Server
- Promise初探Promise
- Quantum 初探
- 初探TCPTCP