proxy-5.0.0 使用Groovy运算报错

proxy-5.0.0 分库分表插入数据报错

使用环境:

5.7.28-log-ShardingSphere-Proxy 5.0.0

场景、问题:

字符串截取
algorithm-expression: "${'2021-10-01'[0..3]}"
报错 [C1997][1997] Runtime exception: [Cannot invoke method getAt() on null object]

字符串转数字
algorithm-expression: "${'4'.toInteger() % 2}"
报错 [C1997][1997] Runtime exception: [Cannot invoke method toInteger() on null object]

现状:

当前配置

schemaName: sharding_db

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  ds_1:
    url: jdbc:mysql://127.0.0.1:3306/test_2?serverTimezone=UTC&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50

rules:
- !SHARDING
  tables:
    od_orderdetails: 
      actualDataNodes: ds_0.od_orderdetails_${[2021]}, ds_1.od_orderdetails_${[2020,2022,2023]}
      databaseStrategy:
        standard:
          shardingColumn: CreateTime
          shardingAlgorithmName: od_orderdetails_database
      tableStrategy:
        standard:
          shardingColumn: CreateTime
          shardingAlgorithmName: od_orderdetails_table

  # 默认数据库分片策略
  defaultDatabaseStrategy:
    hint:
      shardingAlgorithmName: default_database
  # 默认表分片策略
  defaultTableStrategy:
    none:
  # 默认键生成策略 
  defaultKeyGenerateStrategy:
    column: KeyID
    keyGeneratorName: uuid

  # 分片算法配置
  shardingAlgorithms:
    default_database:
      type: HINT_INLINE
      props:
        algorithm-expression: "ds_0"
    od_orderdetails_database:
      type: INLINE
      props:
        algorithm-expression: "ds_${CreateTime[0..3] == '2021' ? '0' : '1'}"
    od_orderdetails_table:
      type: INLINE
      props:
        algorithm-expression: "od_orderdetails_${CreateTime[0..3]}"
  
  # 分布式序列算法配置
  keyGenerators:
    uuid:
      type: UUID

Cannot invoke method getAt() on null object

请检查下 inline 表达式对象是否有为 null 的场景。

莫得,同样的配置在proxy-4.1.1能用 :nauseated_face:

可以在 InlineShardingAlgorithm.java:70 这个位置 debug 看下参数是否为空。

试了好久才发现是proxy-5.0.0 不支持部分Groovy运算
比如:

algorithm-expression: "${'2021-10-01'[0..3]}"
字符串截取 报错 [C1997][1997] Runtime exception: [Cannot invoke method getAt() on null object]

algorithm-expression: "${'4'.toInteger() % 2}"
字符串转数字 报错 [C1997][1997] Runtime exception: [Cannot invoke method toInteger() on null object]

Try this way.
ds_${createtime[0..3] == '2021' ? '0' : '1'}
This bug come from the groovy expression’s column is not same as sharding column.
We’ll fix it later.

1 个赞

ok,谢谢。
这样写可以用,只是sql用切分列作条件时必须写小写。

嗯嗯,好的,感谢反馈,我们后面会修复这个问题

京ICP备2021015875号