Elastic: canal数据同步到ES配置常见报错解决(建议收藏,以备不时之需)

1,469 阅读9分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

0. 引言

所有报错均为博主在实操过程中遇到的错误和解决办法,如果有其他报错或者不同的解决办法,请留言告诉我

安装canal过程中遇到问题,先在本文中查询是否有相同报错,将会为你节约大量排错时间

环境

jdk1.8
canal 1.1.5
mysql8.0
es7.13.0

1. Unknown system variable 'query_cache_size'

这是因为mysql驱动包的版本过低导致的,query cache在MySQL5.7.20就已经过时了,而在MySQL8.0之后就已经被移除了

1、只需要将lib中的驱动器替换成mysql-connector-java-8.0.22.jar

2、修改驱动器权限

chmod 777 lib/mysql-connector-java-8.0.22.jar
chmod +st lib/mysql-connector-java-8.0.22.jar

查看权限如图所示

ll lib

在这里插入图片描述

2. Reason: No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]

启动canal-adapter报错:

Failed to bind properties under 'canal.conf.canal-adapters[0].groups[0].outer-adapters[1].properties' to java.util.Map<java.lang.String, java.lang.String>:

    Reason: No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]

解决: 观察报错信息可以得知是配置文件中的outer2(0基,所以outer-adapter[1]实际指的是2)的properties配置有问题,我们观察配置文件,发现是properties下的mode,cluster.name等属性与properties同级了,将其如下图所示后退两字符即可。 配置

3. RuntimeException: java.lang.RuntimeException: No data source found: xxxx

这个因为在conf/es/xxx.yml中配置的dataSourceKey并没有在conf/application.yml中的srcDataSources中维护

如下图所示,es中的dataSourceKey需要在applicaiton.yml中设置,默认是defaultDS 在这里插入图片描述 在这里插入图片描述

4. Reason: Unable to set value for property src-data-sources

Failed to bind properties under 'canal.conf' to com.alibaba.otter.canal.adapter.launcher.config.AdapterCanalConfig:

    Reason: Unable to set value for property src-data-sources

mysql驱动器导致的问题,使用的数据库是8.x。驱动器是5.x的,将mysql驱动替换为8.0.x版本的。如上所示报错1

5. java.sql.SQLException: null, message from server: "Host '172.16.188.2' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'"

同一个ip在短时间内产生太多中断的数据库连接而导致的阻塞

登录对应的mysql,执行如下指令

flush hosts;

6. IllegalStateException: Extension instance(name: es7, class: interface com.alibaba.otter.canal.client.adapter.OuterAdapter) could not be instantiated: class could not be found

一般 could not be instantiated: class could not be found这样的报错是配置文件的问题,如上的报错可以看到是name: es7中的错误,在官方的示例文档中使用的是name: es6 # or es7

在canal1.1.5+版本中设置的是name: es6 # 或者es7

但在1.1.4版本中直接使用name: es即可

7. IllegalArgumentException: Not found the mapping info of index: user

1、这个报错是ES的mapping设置的问题,确保es中有该索引,并且确认是否有部分字段没有在es中设置mapping,这个要对应之前设置的sql,以及es中的mappings来解决

2、使用了elasticsearch 7.x,但adapter1.1.4默认支持es6.x 解决方案: (1)修改adapter源码,将es依赖调整为7.x;参考博客adapter1.1.4修改源码支持es7.x
(2)换成adapter1.1.5

8. IllegalArgumentException: Illegal character in scheme name at index 0: 172.16.188.7:9200

如果连接es使用的是rest方式,那么hosts中的ip前要添加http://,如

hosts: http://172.16.188.7:9200

9. com.alibaba.druid.pool.DruidDataSource cannot be cast to com.alibaba.druid.pool.DruidDataSource

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassCastException: com.alibaba.druid.pool.DruidDataSource cannot be cast to com.alibaba.druid.pool.DruidDataSource
	at com.alibaba.otter.canal.client.adapter.es7x.ES7xAdapter.init(ES7xAdapter.java:54) ~[client-adapter.es7x-1.1.5-jar-with-dependencies.jar:na]
	at com.alibaba.otter.canal.adapter.launcher.loader.CanalAdapterLoader.loadAdapter(CanalAdapterLoader.java:225) [client-adapter.launcher-1.1.5.jar:na]
	at com.alibaba.otter.canal.adapter.launcher.loader.CanalAdapterLoader.init(CanalAdapterLoader.java:56) [client-adapter.launcher-1.1.5.jar:na]
	at com.alibaba.otter.canal.adapter.launcher.loader.CanalAdapterService.init(CanalAdapterService.java:60) [client-adapter.launcher-1.1.5.jar:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_271]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_271]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_271]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_271]

原因: druid 包冲突 解决: 1、修改client-adapter/escore/pom.xml

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <!--add by whx 20220112-->
            <scope>provided</scope>
 </dependency>

2、重新打包 在这里插入图片描述 3、将client-adapter/es7x/target/client-adapter.es7x-1.1.5-jar-with-dependencies.jar上传到服务器,替换adataper/plugin下的同名jar文件 在这里插入图片描述

scp client-adapter.es7x-1.1.5-jar-with-dependencies.jar root@172.16.188.2:/var/local

4、给该文件赋权

chmod 777 /var/local/client-adapter.es7x-1.1.5-jar-with-dependencies.jar 

5、重启服务

10 CanalParseException: java.io.IOException: EOF encountered

将lib目录下的mysql驱动器替换为mysql8.0,并附权。参考上述

11. CanalClientException: java.io.IOException: Broken pipe Error sync but ACK

服务连接断开了,将deployer和adapter都关闭,先启动deployer再启动adapter

12. DocumentMissingException[[_doc][1413298413755211778]: document missing]

1、es集群出现问题,导致doc无法分配。常见的是分片数的问题,可能是副本分片过多,导致集群报黄 解决: 因为我的是es单节点,所以将主分片数设置为1,副本分片设置为0。不申明的话默认创建副本分片数为1

PUT user
{
  "mappings": {
    "properties": {
      "code": {
        "type": "keyword"
      },
      "email": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "realName": {
        "type": "text",
        "analyzer": "ik_smart",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "roleId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "postId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "deptId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  },
  "settings": {
    "number_of_replicas": 0,
    "number_of_shards": 1
  }
}

2、修改的mysql数据库数据,在es中不存在。先进行全量同步,再进行增量同步

在conf/example/instance.properties中修改

# 全量同步
canal.instance.master.journal.name=mysql-bin.000001
canal.instance.master.position=0
#2019-01-01 00:00:00 上一次更新的时间
canal.instance.master.timestamp=1546272000000

13. ERROR c.a.otter.canal.server.netty.handler.SessionHandler - something goes wrong with channel:[id: 0x23d9cad9, /127.0.0.1:46472 :> /127.0.0.1:11111], exception=java.nio.channels.ClosedChannelException

这是由于deployer中的conf/example/meta.dat与instance.properties文件中的journalName,position,timestamp不一致导致的

# 查询bin log位置
show master status;
# 如果显示的binlog不为000001可以执行以下语句重置binlog(轻易别操作,最好让专业的运维人员操作)
# 该操作会重新生成binlog,之前的binlog就会清空,之前的数据就不会再同步
reset master;
# 刷新log日志,自此刻开始产生一个新编号的binlog日志文件
flush logs;

在这里插入图片描述 将meta.dat删除或者修改一致即可。删除后将会按照instance.properties中设置的起点同步,生产环境考虑好需要后再删除。

如果想要将之前的数据也同步的话,可以将数据库先导出,再重新导入一遍,即可重新生成binlog,实现数据的全量同步

14. Received error packet: errno = 1236, sqlstate = HY000 errmsg = Could not find first log file name in binary log index file

mysql bin log数据不同步,刷新一下即可

flush logs;

15. binlog也设置为000001了,timestamp也设置了,但就是无法实现全量同步

1、删除conf/example/meta.dat 2、调整conf/example/instance.properties

canal.instance.master.journal.name=mysql-bin.000001
canal.instance.master.position=0
#2019-01-01 00:00:00 上一次更新的时间
canal.instance.master.timestamp=1546272000000

3、重启deployer

另外需要注意的是如果bin log是只会记录增量操作的,也就是说开启bin log之前的历史数据是不会记录的,如果需要同步者之前的数据,解决这个问题有三个办法: (1)通过logstash-input-jdbc来实现 (2)通过业务代码来实现(后续会详细讲解这两种方式,可以关注我后续的博客) (3)复制原数据库数据到开启了binlog的从数据库,然后从从数据库同步

16. adapter启动报错:something goes wrong when starting up the canal client adapters: java.lang.NullPointerException: null

这个报错是空指针报错,很明显是哪里获取为空的,这种错误没有固定的原因,但大概率上可以锁定配置文件的问题

1、adapter的配置文件中是有包含了mysql、es、mq、zk等配置,如果不需要的配置项,就将其注释掉,不要打开

比如我这里的报错原因就是因为打开了zookeeperHosts,但是没有配置具体值,所以导致了空指针,因为我不需要zk,将其注释掉即可

 # flatMessage: true
 # zookeeperHosts:

2、某些必要的配置没有设置,快速排查的方式就是根据官方文档中给出的配置文件对比排错 可以参考如下配置文件

3、配置文件中配置项排版错位

server:
  port: 8081
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    default-property-inclusion: non_null

canal.conf:
  mode: tcp #tcp kafka rocketMQ rabbitMQ
  flatMessage: true
  zookeeperHosts:
  syncBatchSize: 1000
  retries: 0
  timeout:
  accessKey:
  secretKey:
  consumerProperties:
    # canal tcp consumer
    canal.tcp.server.host: 127.0.0.1:11111
    canal.tcp.zookeeper.hosts:
    canal.tcp.batch.size: 500
    canal.tcp.username:
    canal.tcp.password:
    # kafka consumer
    kafka.bootstrap.servers: 127.0.0.1:9092
    kafka.enable.auto.commit: false
    kafka.auto.commit.interval.ms: 1000
    kafka.auto.offset.reset: latest
    kafka.request.timeout.ms: 40000
    kafka.session.timeout.ms: 30000
    kafka.isolation.level: read_committed
    kafka.max.poll.records: 1000
    # rocketMQ consumer
    rocketmq.namespace:
    rocketmq.namesrv.addr: 127.0.0.1:9876
    rocketmq.batch.size: 1000
    rocketmq.enable.message.trace: false
    rocketmq.customized.trace.topic:
    rocketmq.access.channel:
    rocketmq.subscribe.filter:
    # rabbitMQ consumer
    rabbitmq.host:
    rabbitmq.virtual.host:
    rabbitmq.username:
    rabbitmq.password:
    rabbitmq.resource.ownerId:
  srcDataSources:
    defaultDS:
      url: jdbc:mysql://172.16.188.1:3306/bladex?useUnicode=true
      #driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: 123456
  canalAdapters:
  - instance: example # canal instance Name or mq topic name
    groups:
    - groupId: g1
      outerAdapters:
      - name: logger
      - 
        key: esKey
        name: es7 # es6 or es7
        hosts: http://172.16.188.7:9200 # 集群地址,逗号隔开. 127.0.0.1:9200 for rest mode or 127.0.0.1:9300 for transport mode
        properties:
          mode: rest #  rest or transport 
          # security.auth: test:123456 #  only used for rest mode
          cluster.name: cluster1

17 Field error in object 'target' on field 'esMapping': rejected value [];

Field error in object 'target' on field 'esMapping': rejected value []; codes 
[typeMismatch.target.esMapping,typeMismatch.esMapping,typeMismatch.com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig$ESMapping,typeMismatch]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.esMapping,esMapping]; arguments []; default message [esMapping]]; default message [Failed to convert property value of type 'java.lang.String' to required type 
'com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig$ESMapping' for property 'esMapping'; nested 
exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type
 'com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig$ESMapping' for property 'esMapping': no 
 matching editors or conversion strategy found]

这是配置文件问题,检查es下的配置yml文件,特别是sql语句的语法是否有问题

18 java.util.NoSuchElementException

没有找到对应字段导致

检查下canal配置文件中的字段是否在es mapping中有对应的,大小写是否一致,是否有遗漏

因为我的操作是mysql同步至es,所以这里说明几项容易出错的地方: 1、canal配置文件中的sql中是否大小写一致,canal是区分大小写的 2、sql中设置的别名是否与es mappings中的名称一致,允许es中的部分字段为空,但是不允许sql中查询出来的字段在es mappings中找不到对应的字段 3、canal配置文件中的dataSourceKey是否正确,其对应到canal application.yml配置文件中的数据库是否正确

dataSourceKey: aaa

application.yml

srcDataSources:
    aaa: # 与之对应
      url: jdbc:mysql://192.168.244.1:3306/aaa?useUnicode=true
      #driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: 123456
    xxx:
      url: jdbc:mysql://192.168.244.1:3306/xxx?useUnicode=true
      username: root
      password: 123456
    yyy:
      url: jdbc:mysql://192.168.244.1:3306/yyy?useUnicode=true
      username: root
      password: 123456

4、canal配置文件中的排版是否正确,特别注意_index,_type等属性要放在esMappings下

dataSourceKey: aaa # 这里的key与上述application.yml中配置的数据源保持一致
outerAdapterKey: esKey # 与上述application.yml中配置的outerAdapters.key一直
destination: example # 默认为example,与application.yml中配置的instance保持一致
groupId:
esMapping:
  _index: dept 
  _type: _doc
  _id: _id
  sql: "select
        t.id,
        t.id as _id,
        t.dept_name as deptName,
        t.dept_category as deptCategory,
        t.parent_id as parentId,
        t.ancestors as ancestors,
        t.third_party_id as thirdPartyId,
        t.phone as phone,
        t.address as address,
        t.is_deleted as isDeleted
      from
         dept t"
  #etlCondition: "where t.update_time>='{0}'"
  commitBatch: 3000

5、sql查询出来的字段类型与es mappings中的字段数据类型是否一致

6、多表同步到同一个索引时,如果都有同一个常量列且值不同时会报错。 如下所示的两个配置文件都有userSource常量列用于区分不同的数据来源,但是如下的配置会报错。 xxx.yml

dataSourceKey: aaa # 这里的key与上述application.yml中配置的数据源保持一致
outerAdapterKey: esKey # 与上述application.yml中配置的outerAdapters.key一直
destination: example # 默认为example,与application.yml中配置的instance保持一致
groupId:
esMapping:
  _index: user
  _type: _doc
  _id: _id
  sql: "select
        t.id, 
        0 as userSource, 
      from
         user t" 
  commitBatch: 3000

yyy.yml

dataSourceKey: aaa # 这里的key与上述application.yml中配置的数据源保持一致
outerAdapterKey: esKey # 与上述application.yml中配置的outerAdapters.key一直
destination: example # 默认为example,与application.yml中配置的instance保持一致
groupId:
esMapping:
  _index: user
  _type: _doc
  _id: _id
  sql: "select
        t.id, 
        1 as userSource, 
      from
         user_wx t" 
  commitBatch: 3000