---"The table needs to be divided into databases according to
a certain type
---"Machine:
* mysql1
operating system version: centos6.5 x64
database version: mysql5.1.73
database name instance: db1
hostname: hadoop1
* mysql2
operating system version: centos6 .5 x64
database version: mysql5.1.73
database name instance: db2
hostname: hadoop2
* mycat
OS version: win7
mycat version: 1.5 release
--- "configure
* server.xml
<?xml version="1.0" encoding="UTF -8"?>
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://org.opencloudb/">
<system>
<property name="defaultSqlParser">druidparser< /property>
</system>
<!-- Logical username, password, instance-->
<user name="test">
<property name="password">test</property>
<property name="schemas">test</property>
</user>
</mycat:server>
* schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://org.opencloudb/" >
<schema name="test" checkSQLschema="false" sqlMaxLimit="100">
<table name="employee" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" />
</schema>
<!-- db1 db2 为实际存在的实例-->
<dataNode name="dn1" dataHost="hadoop1" database="db1" />
<dataNode name="dn2" dataHost="hadoop2" database="db2" />
<dataHost name="hadoop1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM1" url="hadoop1:3306" user="root"
password="root">
</writeHost>
</dataHost>
<dataHost name="hadoop2" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="hostM2"url="hadoop2:3306" user="root"
password="root">
</writeHost>
</dataHost>
</mycat:schema>
* rule.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mycat:rule SYSTEM "rule.dtd">
<mycat:rule xmlns:mycat="http://org.opencloudb/">
<tableRule name="sharding-by-intfile">
<rule>
<columns>sharding_id</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
<function name="hash-int"
class="org.opencloudb.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
</function>
</mycat:rule>
* partition-hash-int.txt and mysql connection are similar, but the port number needs to be changed to 8066 (mycat default port) * Client connection such as sqlyog, navicat connection mycat --- "Client test 10010=1
10000=0
ip: 127.0.0.1
Port: 8066
Library: test
user\password: test\test (configured in server)
* Create table
CREATE TABLE employee (id INT NOT NULL PRIMARY KEY,NAME VARCHAR(100),sharding_id INT NOT NULL);
* Insert data
INSERT INTO employee(id,NAME,sharding_id) VALUES(1,'leader us',10000)
INSERT INTO employee(id,NAME,sharding_id) VALUES(2, 'me',10010);
INSERT INTO employee(id ,NAME,sharding_id) VALUES(3, 'mycat',10000);
INSERT INTO employee(id,NAME,sharding_id) VALUES(4, 'mydog',10010);
* query test
explain select * from employee ;
explain select * from employee where sharding_id=10000 ;
---"Jdbc test
* program
@Test
public void testUpdatePoint3() throws SQLException {
String url = "jdbc:mysql://127.0.0.1:8066/test?user=test&password=test";
Connection con = DriverManager.getConnection(url);
String sql = "select id,NAME,sharding_id from employee";
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println("============================");
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + "\t");
if ((i == 2) && (rs.getString(i).length() <
) {
System.out.print("\t");
}
}
System.out.println("");
}
System.out.println("================== =========");
pstmt.close();
con.close();
}
*result http://ynp.iteye.com
============= ==============
3 mycat 10000
1 leader us 10000
4 mydog 10010
2 me 10010
======================== ======
---" Compared with Spring's multiple data sources
, mycat can no longer maintain multiple data sources, and the code modification amount is small;
mycat introductory example
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326748268&siteId=291194637
Recommended
Ranking