Code Monkey home page Code Monkey logo

sharding-jdbc's Introduction

##Sharding-JDBC - A JDBC driver for shard databases and tables

Sharding-JDBC是当当应用框架ddframe中,关系型数据库模块dd-rdb中分离出来的数据库水平扩展框架,即透明化数据库分库分表访问。

Sharding-JDBCdubboxelastic-job之后,是ddframe系列开源的第三个产品。

Release Notes

  • sharding-jdbc           Maven Status
  • sharding-jdbc-core  Maven Status

License

Hex.pm

Build Status

Build Status Coverage Status

主要贡献者

**讨论QQ群:**532576663(不限于Sharding-JDBC,包括分布式,数据库相关以及其他互联网技术交流)

简介

Sharding-JDBC直接封装JDBC API,可以理解为增强版的JDBC驱动,旧代码迁移成本几乎为零:

  • 可适用于任何基于javaORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC
  • 可基于任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, Druid等。
  • 理论上可支持任意实现JDBC规范的数据库。虽然目前仅支持MySQL,但已有支持OracleSQLServerDB2等数据库的计划。

Sharding-JDBC定位为轻量级java框架,使用客户端直连数据库,以jar包形式提供服务,未使用中间层,无需额外部署,无其他依赖,DBA也无需改变原有的运维方式。SQL解析使用Druid解析器,是目前性能最高的SQL解析器。

Sharding-JDBC功能灵活且全面:

  • 分片策略灵活,可支持=BETWEENIN等多维度分片,也可支持多分片键共用。
  • SQL解析功能完善,支持聚合,分组,排序,LimitOR等查询,并且支持Binding Table以及笛卡尔积的表查询。

以下是常见的分库分表产品和Sharding-JDBC的对比:

功能 Cobar Cobar-client TDDL Sharding-JDBC
分库 未开源
分表 未开源
中间层
ORM支持 任意 仅MyBatis 任意 任意
数据库支持 仅MySQL 任意 任意 任意
异构语言 仅Java 仅Java 仅Java
外部依赖 Diamond

整体架构图

整体架构图

相关文档

使用指南

详细功能列表

架构图

目录结构说明

阅读源码编译问题说明

使用限制

压力测试报告

未来线路规划

事务支持说明

InfoQ新闻

Quick Start

引入maven依赖

<!-- 引入sharding-jdbc核心模块 -->
<dependency>
    <groupId>com.dangdang</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
    <version>1.0.0</version>
</dependency>

规则配置

Sharding-JDBC的分库分表通过规则配置描述,请简单浏览配置全貌:

 ShardingRule shardingRule = new ShardingRule(
                dataSourceRule, 
                Arrays.asList(tableRule), 
                new DatabaseShardingStrategy("sharding_column_1", new XXXShardingAlgorithm()),
                new TableShardingStrategy("sharding_column_2", new XXXShardingAlgorithm()));

规则配置包括数据源配置、表规则配置、分库策略和分表策略组成。这只是最简单的配置方式,实际使用可更加灵活,如:多分片键,分片策略直接和tableRule绑定等。

详细的规则配置请参考用户指南

使用基于ShardingDataSource的JDBC接口

通过规则配置对象获取ShardingDataSourceShardingDataSource实现自JDBC的标准接口DataSource。然后可通过DataSource选择使用原生JDBC开发,或者使用JPA, MyBatisORM工具。 以JDBC原生实现为例:

DataSource dataSource = new ShardingDataSource(shardingRule);
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
try (
        Connection conn = dataSource.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql)) {
    pstmt.setInt(1, 10);
    pstmt.setInt(2, 1001);
    try (ResultSet rs = pstmt.executeQuery()) {
        while(rs.next()) {
            System.out.println(rs.getInt(1));
            System.out.println(rs.getInt(2));
            System.out.println(rs.getInt(3));
        }
    }
}

sharding-jdbc's People

Contributors

terrymanu avatar hanahmily avatar haocao avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.