gitweixin
  • 首页
  • 小程序代码
    • 资讯读书
    • 工具类
    • O2O
    • 地图定位
    • 社交
    • 行业软件
    • 电商类
    • 互联网类
    • 企业类
    • UI控件
  • 大数据开发
    • Hadoop
    • Spark
    • Hbase
    • Elasticsearch
    • Kafka
    • Flink
    • 数据仓库
    • 数据挖掘
    • flume
    • Kafka
    • Hive
    • shardingsphere
    • solr
  • 开发博客
    • Android
    • php
    • python
    • 运维
    • 技术架构
    • 数据库
  • 程序员网赚
  • bug清单
  • 量化投资
  • 在线查询工具
    • 去行号
    • 在线时间戳转换工具
    • 免费图片批量修改尺寸在线工具
    • SVG转JPG在线工具

分类归档shardingsphere

精品微信小程序开发门户,代码全部亲测可用

  • 首页   /  大数据开发
  • 分类归档: "shardingsphere"
shardingsphere, 大数据开发 11月 6,2022

DataX、Sqoop抽取Sharding-Proxy数据遇到问题和解决方案

公司有几十T的数据库数据,采用sharding-proxy来分库分表存取。采用sharding-proxy,可以让客户端像普通单库一样调用,但用datax、sqoop抽取数据却发现那么简单。

原先是用 sharding-proxy 4.1.0 版本,springboot采用druid来读取数据或用navicat高版本来读没问题,所以一直没有升级。

不幸的是,用sqoop 1.4.7来抽取数据时,发生报错“Failed to switch schema, please terminate current transaction”。网上相关的资料很少,后来查到是 sharding-proxy 报的错,在github讨论区有说这个问题,
https://github.com/apache/shardingsphere/issues/5531

刚开始以为是sqoop读取表结构时遇到的问题,改用dataX来抽取数据,还是同样的问题。后来升级 sharding-proxy 到最新版本,终于没报这个问题,但用sqoop抽取时,由于sqoop 1.4.7要先读取mysql的表结构,但是获取
sharding-proxy 的表结构发生错误,这个可以通过修改sqoop代码或别的方式来避开这个问题。

但DataX抽取数据时没这个问题,能很顺利抽取到
sharding-proxy 上面的数据。

作者 east
shardingsphere 12月 10,2021

Shardingsphere 4.0.0-RC1使用遇到的坑

1、使用pagehelper后跨表查询奇慢无比

shardingsphere后面的版本还没试过,在目前版本时,结合mybatis + pagehelper组件是有存在问题的。在
Shardingsphere 4.0.0-RC1和pagehelper分页时报错解决 一文解决了冲突,没想到后来又发现新的问题,在进行分表时,每个表数据几百万,查询跨几个表时,发现要等几分钟才出来结果。这样比单库查询加索引查还慢,一时很崩溃,本来分库分表为了提高速度,现在反而速度更慢了。后来在查询前把

PageHelper.startPage(pageNum, pageSize);

注释掉,刷刷一下子出来结果了。看来如果要实现分页还是自己实现。

2、查询语句太复杂,不走分片规则,进行全库查询

原来sql语句片段如下:

(VALUE1 LIKE concat(#(value,jdbcType=VARCHAR},%) AND TYPE = 1) OR  
(VALUE2 LIKE concat(#(value,jdbcType=VARCHAR},%) AND TYPE = 2) OR
(VALUE2 LIKE concat(#(value,jdbcType=VARCHAR},%) AND TYPE = 3)

没有调用到分片规则,后来把sql改简单后就正常了。例如下面的

TYPE != 4 AND VALUE1 LIKE concat(#(value,jdbcType=VARCHAR},%) OR VALUE2 LIKE concat(#(value,jdbcType=VARCHAR},%)

作者 east
shardingsphere 11月 28,2021

Shardingsphere 4.0.0-RC1和pagehelper分页时报错解决

项目架构使用Shardingsphere和pagehelper架构,对一个复杂sql语句,运行后报错“Must have sharding column with subquery. "

原来 PageHelper里面有个机制是,当解析的sql比较复杂的时候,会加上别名,而Sharding-jdbc执行这个带有别名的sql会报错。如果不用pageHelper,自己来分页是可以避免这个问题。但这样做比较麻烦。

解决办法是在另加一个XXX_COUNT的sql,不要让PageHelper给原始sql加上别名。 官网的做法解释如下:

增加 countSuffix count 查询后缀配置参数,该参数是针对 PageInterceptor 配置的,默认值为 _COUNT。

分页插件会优先通过当前查询的 msId + countSuffix 查找手写的分页查询。

如果存在就使用手写的 count 查询,如果不存在,仍然使用之前的方式自动创建 count 查询。

例如,如果存在下面两个查询:

<select id="selectLeftjoin" resultType="com.github.pagehelper.model.User">     select a.id,b.name,a.py from user a     left join user b on a.id = b.id     order by a.id
</select>

<select id="selectLeftjoin_COUNT" resultType="Long">
select count(distinct a.id) from user a left join user b on a.id = b.id
</select>

上面的 countSuffix 使用的默认值 _COUNT,分页插件会自动获取到 selectLeftjoin_COUNT 查询,这个查询需要自己保证结果数正确。

返回值的类型必须是resultType="Long",入参使用的和 selectLeftjoin 查询相同的参数,所以在 SQL 中要按照 selectLeftjoin 的入参来使用。

因为 selectLeftjoin_COUNT 方法是自动调用的,所以不需要在接口提供相应的方法,如果需要单独调用,也可以提供。

作者 east
bug清单, shardingsphere 2月 13,2021

解决DATE_FORMAT(data_create_time, ‘%Y-%m-%d’)>= DATE_FORMAT(?, ‘%Y-%m-%d’)报错

使用 shardingsphere 4.1.0版本,代码如下:

 PreparedStatement ps = conn.prepareStatement("select * from test where DATE_FORMAT(create_date, '%Y-%m-%d')>= DATE_FORMAT(?, '%Y-%m-%d') limit ?,?");
    ps.setString(1,"2020-02-01");
    ps.setInt(2,1);
    ps.setInt(3,10)

报以下的错:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.PaginationContext.getValue(PaginationContext.java:57) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.PaginationContext.(PaginationContext.java:50) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.engine.LimitPaginationContextEngine.createPaginationContext(LimitPaginationContextEngine.java:38) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.engine.PaginationContextEngine.createPaginationContext(PaginationContextEngine.java:48) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext.(SelectStatementContext.java:100) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory.getDMLStatementContext(SQLStatementContextFactory.java:103) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory.newInstance(SQLStatementContextFactory.java:87) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.underlying.route.DataNodeRouter.createRouteContext(DataNodeRouter.java:99) ~[shardingsphere-route-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.underlying.route.DataNodeRouter.executeRoute(DataNodeRouter.java:89) ~[shardingsphere-route-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.underlying.route.DataNodeRouter.route(DataNodeRouter.java:76) ~[shardingsphere-route-4.1.0.jar:4.1.0]

经过测试,传递的参数如果为string类型就报下面的错误,如果把类型修正为Date类型则不会报如下错误。配置:

spring:
  shardingsphere:
    encrypt:
      encryptors:
        aesEncryptor:
          type: aes
          props:
            aes.key.value: 123456
      tables:
        linkman:
          columns:
            mobile:
              plainColumn: mobile
              cipherColumn: mobile_encrypted
              encryptor: aesEncryptor
    datasource:
      names: ds
      ds:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://localhost:3306/shardingsphere_test?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true
        username: root
        password: root
        connection-timeout: 30000
        maximum-pool-size: 20
        minimum-idle: 5
        auto-commit: true
        idle-timeout: 600000
        pool-name: crm-hikari
        max-lifetime: 1800000
        connection-test-query: SELECT 1
    props:
      query.with.cipher.column: false
<select id="selectByTime" resultMap="BaseResultMap"  parameterType="string">
        SELECT  <include refid="Base_Column_List"/>
         FROM linkman
         WHERE DATE_FORMAT(create_time, '%Y-%m-%d') =  DATE_FORMAT(#{createTime},'%Y-%m-%d')
    </select>

    List<Linkman> selectByTime(@Param("createTime") String createTime);
//如果把createTime的类型修改为Date类型则不会报错.
作者 east
bug清单, shardingsphere 2月 8,2021

解决shardingsphere shardingsphere4.0使用like模糊搜索出现的问题

使用shardingsphere,运行时出现红色报错“no viable alternative at input “,下面还出现”SQLErrorCodes loaded:,[DB2,Derby,H2,HSQL,Informix,MS-SQL,Oracle,PostgreSQL,Sybase]。

怀疑是sql语句在 shardingsphere 解析后出现问题。一查果然是。

出问题的语句是这样写: select * from t_car like #{car_no}”%”。

改写成下面这样就没问题:

select * from t_car like concat(#{car_no}, ‘%’)

作者 east
shardingsphere 2月 6,2021

shardingsphere4.0+Springboot+Mybatis+druid动态多数据源

首先Springboot+Mybatis+druid动态多数据源的配置是这样的

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan(basePackages = "com.xxx.xxx.mapper")
@Import({DynamicDataSourceConfig.class})
public class BootApplication {

	public static void main(String[] args) {
		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
		SpringApplication.run(BootApplication.class, args);
	}

}

application.pro的关键配置如下:

spring:

datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 配置监控服务器:http://ip:端口/druid/index.html
stat-view-servlet:
login-username: admin
login-password: kisen@123
reset-enable: false
url-pattern: /druid/*

  master: # 主数据源
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://167.1.6.163:53306/master?serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true

  slave: # 从数据源
    #        driverClassName: org.postgresql.Driver
    #        username: postgres
    #        password: postgres
    #        url: jdbc:postgresql://192.168.30.22:5432/slave_db
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://157.1.6.134:53

动态切换的配置文件如下:


import java.lang.annotation.*;

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
    String name() default "";
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class DataSourceAspect {

    @Pointcut("@annotation(DataSource)")
    public void dataSourcePointCut() {

    }


    @Around("dataSourcePointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();


        DataSource dataSource = method.getAnnotation(DataSource.class);
        if(dataSource == null){
            DynamicDataSource.setDataSource("master");
        }else {
            System.out.println("dataSource.name()===="+dataSource.name());
            DynamicDataSource.setDataSource(dataSource.name());
        }

        try {
            return point.proceed();
        } finally {
            DynamicDataSource.clearDataSource();
        }
    }
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource  extends AbstractRoutingDataSource {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    private static Logger log = LoggerFactory.getLogger(DynamicDataSource.class);

    @Override
    protected Object determineCurrentLookupKey() {
     //   log.info("getDataSource()===================="+getDataSource());
        return getDataSource();
    }

    public static void setDataSource(String dataSource) {
        contextHolder.set(dataSource);
        log.info("切换到{"+dataSource+"}数据源");
        log.info("切换到{"+dataSource+"}数据源");
    }

    public static String getDataSource() {
        return contextHolder.get();
    }

    public static void clearDataSource() {
        contextHolder.remove();
    }
}

下面是最关键的地方,要对普通的druid多数据源动态切换做如下修改


import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DynamicDataSourceConfig {


    @Bean(name = "master")
    @ConfigurationProperties("spring.datasource.druid.master")
    public DataSource  masterDataSource(){
        return DruidDataSourceBuilder.create().build();
    }



    @Bean(name = "slave")
    @ConfigurationProperties("spring.datasource.druid.slave")
    public DataSource  anjianSlaveDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

@Bean(name = "sharding")
public DataSource getShardingDataSource() throws SQLException {
		ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
 
		//  分库分表逻辑,在这里不做代码展示
 
		return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig,new ConcurrentHashMap(), properties);
	}

 public  Map<String, DataSource> createDataSourceMap() {
	   Map<String, DataSource> dataSourceMap = new HashMap<String, DataSource>(4);
	   // 配置第一个数据源
       DruidDataSource data1 = new DruidDataSource();
       data1.setDriverClassName("数据源驱动");
       data1.setUrl("数据库链接1");
       data1.setUsername("用户名");
       data1.setPassword("密码");
       dataSourceMap.put("data1 ", data1 );
 
       // 配置第二个数据源
       DruidDataSource data2 = new DruidDataSource();
       data2.setDriverClassName("数据源驱动");
       data2.setUrl("数据库链接2");
       data2.setUsername("用户名");
       data2.setPassword("密码");
       dataSourceMap.put("data2 ", data2 );
 
       // 配置第三个数据源
       DruidDataSource data3 = new DruidDataSource();
       data3.setDriverClassName("数据源驱动");
       data3.setUrl("数据库链接3");
       data3.setUsername("用户名");
       data3.setPassword("密码");
       dataSourceMap.put("data3", data3);
       
       // 配置第四个数据源
       DruidDataSource data4 = new DruidDataSource();
       data4.setDriverClassName("数据源驱动");
       data4.setUrl("j数据库链接4");
       data4.setUsername("用户名");
       data4.setPassword("密码");
       dataSourceMap.put("data4", data4);
       
       return dataSourceMap;
   }
 
}



    @Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap();
        dsMap.put("master", masterDataSource());
        dsMap.put("slave-anjian", anjianSlaveDataSource());

        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }

    /**
     * 配置@Transactional注解事物
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }

}
作者 east

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。回复”chatgpt”获取免注册可用chatgpt。回复“大数据”获取多本大数据电子书

标签

AIGC AI创作 bert chatgpt github GPT-3 gpt3 GTP-3 hive mysql O2O tensorflow UI控件 不含后台 交流 共享经济 出行 图像 地图定位 外卖 多媒体 娱乐 小程序 布局 带后台完整项目 开源项目 搜索 支付 效率 教育 日历 机器学习 深度学习 物流 用户系统 电商 画图 画布(canvas) 社交 签到 联网 读书 资讯 阅读 预订

官方QQ群

小程序开发群:74052405

大数据开发群: 952493060

近期文章

  • 详解Python当中的pip常用命令
  • AUTOSAR如何在多个供应商交付的配置中避免ARXML不兼容?
  • C++thread pool(线程池)设计应关注哪些扩展性问题?
  • 各类MCAL(Microcontroller Abstraction Layer)如何与AUTOSAR工具链解耦?
  • 如何设计AUTOSAR中的“域控制器”以支持未来扩展?
  • C++ 中避免悬挂引用的企业策略有哪些?
  • 嵌入式电机:如何在低速和高负载状态下保持FOC(Field-Oriented Control)算法的电流控制稳定?
  • C++如何在插件式架构中使用反射实现模块隔离?
  • C++如何追踪内存泄漏(valgrind/ASan等)并定位到业务代码?
  • C++大型系统中如何组织头文件和依赖树?

文章归档

  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年1月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年7月
  • 2018年6月

分类目录

  • Android (73)
  • bug清单 (79)
  • C++ (34)
  • Fuchsia (15)
  • php (4)
  • python (43)
  • sklearn (1)
  • 云计算 (20)
  • 人工智能 (61)
    • chatgpt (21)
      • 提示词 (6)
    • Keras (1)
    • Tensorflow (3)
    • 大模型 (1)
    • 智能体 (4)
    • 深度学习 (14)
  • 储能 (44)
  • 前端 (4)
  • 大数据开发 (488)
    • CDH (6)
    • datax (4)
    • doris (30)
    • Elasticsearch (15)
    • Flink (78)
    • flume (7)
    • Hadoop (19)
    • Hbase (23)
    • Hive (40)
    • Impala (2)
    • Java (71)
    • Kafka (10)
    • neo4j (5)
    • shardingsphere (6)
    • solr (5)
    • Spark (99)
    • spring (11)
    • 数据仓库 (9)
    • 数据挖掘 (7)
    • 海豚调度器 (10)
    • 运维 (34)
      • Docker (3)
  • 小游戏代码 (1)
  • 小程序代码 (139)
    • O2O (16)
    • UI控件 (5)
    • 互联网类 (23)
    • 企业类 (6)
    • 地图定位 (9)
    • 多媒体 (6)
    • 工具类 (25)
    • 电商类 (22)
    • 社交 (7)
    • 行业软件 (7)
    • 资讯读书 (11)
  • 嵌入式 (70)
    • autosar (63)
    • RTOS (1)
    • 总线 (1)
  • 开发博客 (16)
    • Harmony (9)
  • 技术架构 (6)
  • 数据库 (32)
    • mongodb (1)
    • mysql (13)
    • pgsql (2)
    • redis (1)
    • tdengine (4)
  • 未分类 (6)
  • 程序员网赚 (20)
    • 广告联盟 (3)
    • 私域流量 (5)
    • 自媒体 (5)
  • 量化投资 (4)
  • 面试 (14)

功能

  • 登录
  • 文章RSS
  • 评论RSS
  • WordPress.org

All Rights Reserved by Gitweixin.本站收集网友上传代码, 如有侵犯版权,请发邮件联系yiyuyos@gmail.com删除.