README.md 5.8 KB
Newer Older
T
terrymanu 已提交
1 2 3
##Sharding-JDBC - A JDBC driver for shard databases and tables 

# [中文主页](README_cn.md)
H
haocao 已提交
4
[![Build Status](https://secure.travis-ci.org/dangdangdotcom/sharding-jdbc.png?branch=master)](https://travis-ci.org/dangdangdotcom/sharding-jdbc)
C
Chris Cao 已提交
5
[![Maven Status](https://maven-badges.herokuapp.com/maven-central/com.dangdang/sharding-jdbc/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.dangdang/sharding-jdbc)
H
haocao 已提交
6
[![Coverage Status](https://coveralls.io/repos/dangdangdotcom/sharding-jdbc/badge.svg?branch=master&service=github)](https://coveralls.io/github/dangdangdotcom/sharding-jdbc?branch=master)
C
Chris Cao 已提交
7 8
[![GitHub release](https://img.shields.io/github/release/dangdangdotcom/sharding-jdbc.svg)](https://github.com/dangdangdotcom/sharding-jdbc/releases)
[![Hex.pm](http://dangdangdotcom.github.io/sharding-jdbc/img/license.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
C
Chris Cao 已提交
9

T
terrymanu 已提交
10
# Overview
T
terrymanu 已提交
11

T
terrymanu 已提交
12
Sharding-JDBC is a JDBC extension, provides distributed features such as sharding, read/write splitting and BASE transaction.
T
terrymanu 已提交
13

T
terrymanu 已提交
14
# Features
T
terrymanu 已提交
15

T
terrymanu 已提交
16 17 18 19 20 21
## 1. Sharding
* Aggregation functions, group by, order by and limit SQL supported in distributed database.
* Join (inner/outer) query supported.
* Sharding operator `=`, `BETWEEN` and `IN` supported.
* Sharding algorithm customization supported.
* Hint supported.
T
terrymanu 已提交
22

T
terrymanu 已提交
23 24 25
## 2. Read/Write Splitting
* Same transaction data concurrency guarantee.
* Hint supported.
T
terrymanu 已提交
26

T
terrymanu 已提交
27
## 3. BASE Transaction
T
terrymanu 已提交
28 29
* Best efforts delivery transaction.
* Try confirm cancel transaction (TBD).
T
terrymanu 已提交
30

T
terrymanu 已提交
31 32 33 34
## 4. Compatibility
* ORM self-adapting. JPA, Hibernate, Mybatis, Spring JDBC Template or JDBC supported.
* Connection-pool self-adapting. DBCP, C3P0, BoneCP, Druid supported.
* Any Database supported theoretically. MySQL support only, will support Oracle, SQLServer, DB2 and PostgreSQL in future.
T
terrymanu 已提交
35

T
terrymanu 已提交
36 37 38 39 40
## 5. Configuration
* Java config
* Spring namespace
* YAML
* Inline expression
H
haocao 已提交
41

G
gaohongtao 已提交
42 43 44
## 6. ID Generation
* Distributed Unique Time-Sequence Generation

T
terrymanu 已提交
45 46
***

T
terrymanu 已提交
47
# Architecture
T
terrymanu 已提交
48

T
terrymanu 已提交
49
![Architecture](http://dangdangdotcom.github.io/sharding-jdbc/img/architecture_en.png)
T
terrymanu 已提交
50

T
terrymanu 已提交
51
# [Roadmap](ROADMAP.md)
T
terrymanu 已提交
52

T
terrymanu 已提交
53 54
# Quick Start

T
terrymanu 已提交
55
## Add maven dependency
T
terrymanu 已提交
56

T
terrymanu 已提交
57
```xml
T
terrymanu 已提交
58
<!-- import sharding-jdbc core -->
T
terrymanu 已提交
59 60 61
<dependency>
    <groupId>com.dangdang</groupId>
    <artifactId>sharding-jdbc-core</artifactId>
H
haocao 已提交
62
    <version>${latest.release.version}</version>
T
terrymanu 已提交
63 64
</dependency>

T
terrymanu 已提交
65
<!-- import other module if need -->
T
terrymanu 已提交
66
```
T
terrymanu 已提交
67 68 69

## Rule configuration

T
terrymanu 已提交
70
```java
71
ShardingRule shardingRule = ShardingRule.builder()
T
fix #78  
terrymanu 已提交
72
        .dataSourceRule(dataSourceRule)
73 74 75
        .tableRules(tableRuleList)
        .databaseShardingStrategy(new DatabaseShardingStrategy("sharding_column", new XXXShardingAlgorithm()))
        .tableShardingStrategy(new TableShardingStrategy("sharding_column", new XXXShardingAlgorithm())))
T
fix #78  
terrymanu 已提交
76
        .build();
T
terrymanu 已提交
77
```
T
terrymanu 已提交
78

T
terrymanu 已提交
79
## Use raw JDBC API
T
terrymanu 已提交
80

T
terrymanu 已提交
81
```java
T
fix #60  
terrymanu 已提交
82
DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
T
terrymanu 已提交
83 84 85
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();
T
terrymanu 已提交
86 87 88 89
        PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
    preparedStatement.setInt(1, 10);
    preparedStatement.setInt(2, 1001);
    try (ResultSet rs = preparedStatement.executeQuery()) {
T
terrymanu 已提交
90 91 92 93 94 95
        while(rs.next()) {
            System.out.println(rs.getInt(1));
            System.out.println(rs.getInt(2));
        }
    }
}
T
terrymanu 已提交
96
```
H
haocao 已提交
97

T
terrymanu 已提交
98 99
## Use spring namespace

T
terrymanu 已提交
100
```xml
H
haocao 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.xsd 
                        http://www.dangdang.com/schema/ddframe/rdb 
                        http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd 
                        ">
    <context:property-placeholder location="classpath:conf/rdb/conf.properties" ignore-unresolvable="true"/>
    
    <bean id="dbtbl_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    <bean id="dbtbl_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>

C
Chris Cao 已提交
128 129
    <rdb:strategy id="orderTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_${order_id.longValue() % 4}"/>
    <rdb:strategy id="orderItemTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_item_${order_id.longValue() % 4}"/>
H
haocao 已提交
130 131 132 133 134 135
    <rdb:data-source id="shardingDataSource">
        <rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1">
            <rdb:table-rules>
                <rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}" table-strategy="orderTableStrategy"/>
                <rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" table-strategy="orderItemTableStrategy"/>
            </rdb:table-rules>
C
Chris Cao 已提交
136
            <rdb:default-database-strategy sharding-columns="user_id" algorithm-expression="dbtbl_${user_id.longValue() % 2 + 1}"/>
H
haocao 已提交
137 138 139
        </rdb:sharding-rule>
    </rdb:data-source>
</beans>
T
terrymanu 已提交
140
```