After building the cluster, you need to create a database, create a table, and set the partition primary key to initialize data!
(1) create a WLB database and set the table shards wlb_orders
D:/mongodb-win32-i386-1.8.0/CMD> Cd D:/mongodb-win32-i386-1.8.0/bin
D:/mongodb-win32-i386-1.8.0/bin> call cmd.exe 127.0.0.1: 50000
MongoDB shell version: 1.8.0
Connecting to: 127.0.0.1: 50000/test
>Use Admin
Switched to DB Admin
>Printshardingstatus ()
--- Sharding status ---
Sharding version: {"_ id": 1, "version": 3}
Shards:
{
"_ Id": "shardseta ",
"Host": "Seta/127.0.0.1: 10000,127.0 .0.1: 10001,127.0 .0.1: 10002"
}
{
"_ Id": "shardsetb ",
"Host": "SETB/127.0.0.1: 20000,127.0 .0.1: 20001,127.0 .0.1: 20002"
}
{
"_ Id": "shardsetc ",
"Host": "SETC/127.0.0.1: 30000,127.0 .0.1: 30001,127.0 .0.1: 30002"
}
Databases:
{"_ Id": "admin", "partitioned": false, "primary": "Config "}
>Use WLB // If the WLB database does not exist, a new database is created.
Switched to DB WLB
>DB. createcollection ('wlb _ orders ') // Create a table wlb_orders
{"OK": 1}
> Use Admin
Switched to DB Admin
>DB. runcommand ({enablesharding: 'wlb '}) // You Can shard the database.
{"OK": 1}
>DB. runcommand ({shardcollection: 'wlb. wlb_orders ', key: {order_id: 1 }})// Set the partition primary key of the table to order_id
{"Collectionsharded": "WLB. wlb_orders", "OK": 1}
>DB. printshardingstatus () // Query database shard Information
--- Sharding status ---
Sharding version: {"_ id": 1, "version": 3}
Shards:
{
"_ Id": "ShardSetA ",
"Host": "setA/127.0.0.1: 10000,127.0 .0.1: 10001,127.0 .0.1: 10002"
}
{
"_ Id": "ShardSetB ",
"Host": "setB/127.0.0.1: 20000,127.0 .0.1: 20001,127.0 .0.1: 20002"
}
{
"_ Id": "ShardSetC ",
"Host": "setC/127.0.0.1: 30000,127.0 .0.1: 30001,127.0 .0.1: 30002"
}
Databases:
{"_ Id": "admin", "partitioned": false, "primary": "Config "}
{"_ Id": "WLB", "partitioned": True, "primary": "shardseta "}
WLB. wlb_orders chunks:
Shardseta 1
{"Order_id" :{$ Minkey: 1 }}-- >>{ "order_id" :{$ maxkey: 1 }}on: shardseta {"T": 1000, "I": 0}
>
(2) complete data initialization with Java code
Package com. zhangzk. MongoDB;
Import java.net. unknownhostexception;
Import java. util. date;
Import com. MongoDB. basicdbobject;
Import com. MongoDB. dB;
Import com. MongoDB. dbcollection;
Import com. MongoDB. Mongo;
Import com. MongoDB. parse exception;
Public class mongodbtest {
/**
* 30 W 288391
*
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Initdata ();
// Query ();
}
Private Static void initdata (){
Long start = new date (). gettime ();
Try {
Mongo DB = new Mongo ("127.0.0.1", 50000 );
DB mydb = dB. getdb ("WLB ");
Dbcollection coll = mydb. getcollection ("wlb_orders ");
For (long I = 1; I <= 300000; I ++ ){
Basicdbobject Doc = new basicdbobject ();
Doc. Put ("order_id", I );
Doc. Put ("company_id", 505 + I );
Doc. Put ("user_id", 180225429 + I );
Doc. Put ("fetcher_id", 59803 + I );
Doc. Put ("fetch_schedule_begin", new date ());
Doc. Put ("fetch_schedule_end", new date ());
Doc. Put ("sender_id", 59803 + I );
Doc. Put ("mail_no", "000000 ");
Doc. Put ("mail_type", "301 ");
Doc. Put ("order_code", "lp100000000003959" + I );
Doc. Put ("order_status", 30 );
Doc. Put ("prev_order_id", 0 );
Doc. Put ("trade_id", 2010012706189794l + I );
Doc. Put ("goods_remark ","");
Doc. Put ("receiver_name", "Caesar ");
Doc. Put ("receiver_wangwang_id", "sanglin01 ");
Doc. Put ("receiver_mobile_phone", "13021525841 ");
Doc. Put ("receiver_zip_code", "650045 ");
Doc. Put ("receiver_teletelephone", "13868117135 ");
Doc. Put ("receiver_county_id", 350102 );
Doc. Put ("receiver_address", "Fujian Province ^ Fuzhou ^ No. 12 of safensavensaine in Gulou District ^ ");
Doc. put ("gmt_create", new Date ());
Doc. put ("gmt_modified", new Date ());
Doc. put ("status_reason ","");
Doc. put ("logis_type", 0 );
Doc. put ("seller_wangwang_id", "tbtest943" + I );
Doc. put ("seller_send_confirm", 0 );
Doc. put ("shipping", 2 );
Doc. put ("company_code ","");
Doc. put ("taobao_trade_id", "2232358300" + I );
Doc. put ("options", 2 );
Doc. put ("shipping2", 0 );
Doc. put ("order_source", 0 );
Doc. put ("status_date", new Date ());
Doc. Put ("timeout_status", 2 );
Doc. Put ("feature", "IP = 127.0.0.1; sfid = ");
Doc. Put ("service_fee", 0 );
Doc. Put ("seller_store_id", "23100 ");
Doc. Put ("items_value", 23100 );
Doc. Put ("pre_status", 0 );
Doc. Put ("ticket_id ","");
Doc. Put ("tfs_url", "t1dobbxctcxxxxxxxx ");
Coll. insert (DOC );
}
DB. Close ();
Long endtime = new date (). gettime ();
System. Out. println (endtime-Start );
System. Out. println (endtime-Start)/10000000 );
} Catch (UnknownHostException e ){
E. printStackTrace ();
} Catch (except exception e ){
E. printStackTrace ();
}
}
}
This is a single-threaded execution. One machine simulates three sets of replica sets, with three shards in each group, three Configsvr, one route node, and 30 million data records inserted, it took a total of 288.349 seconds to insert 1041 data records per second on average, which is quite fast.
(3) view database shard information dB. printshardingstatus ()
> Db. printShardingStatus ()
--- Sharding Status ---
Sharding version: {"_ id": 1, "version": 3}
Shards:
{
"_ Id": "ShardSetA ",
"Host": "setA/127.0.0.1: 10000,127.0 .0.1: 10001,127.0 .0.1: 10002"
}
{
"_ Id": "shardsetb ",
"Host": "SETB/127.0.0.1: 20000,127.0 .0.1: 20001,127.0 .0.1: 20002"
}
{
"_ Id": "shardsetc ",
"Host": "SETC/127.0.0.1: 30000,127.0 .0.1: 30001,127.0 .0.1: 30002"
}
Databases:
{"_ Id": "admin", "partitioned": false, "primary": "Config "}
{"_ Id": "WLB", "partitioned": True, "primary": "shardseta "}
WLB. wlb_orders chunks:
Shardsetb 5
Shardseta 4
Shardsetc 5
{"Order_id" :{$ minKey: 1 }}-- >>{ "order_id": NumberLong (1)} on: ShardSetB {"t": 5000, "I ": 1}
{"Order_id": NumberLong (1)} -- >>{ "order_id": NumberLong (19943)} on: ShardSetA {"t": 8000, "I": 1}
{"Order_id": NumberLong (19943)} -- >>{ "order_id": NumberLong (37015)} on: ShardSetC {"t": 7000, "I": 1}
{"Order_id": numberlong (37015)} -- >>{ "order_id": numberlong (54080)} on: shardsetb {"T": 4000, "I": 2}
{"Order_id": numberlong (54080)} -- >>{ "order_id": numberlong (71148)} on: shardsetb {"T": 4000, "I": 4}
{"Order_id": numberlong (71148)} -- >>{ "order_id": numberlong (88201)} on: shardseta {"T": 5000, "I": 2}
{"Order_id": NumberLong (88201)} -- >>{ "order_id": NumberLong (105259)} on: ShardSetC {"t": 6000, "I": 2}
{"Order_id": NumberLong (105259)} -- >>{ "order_id": NumberLong (122284)} on: ShardSetC {"t": 6000, "I": 4}
{"Order_id": NumberLong (122284)} -- >>{ "order_id": NumberLong (139303)} on: ShardSetC {"t": 6000, "I": 6}
{"Order_id": numberlong (139303)} -- >>{ "order_id": numberlong (173354)} on: shardsetc {"T": 6000, "I": 8}
{"Order_id": numberlong (173354)} -- >>{ "order_id": numberlong (207403)} on: shardseta {"T": 7000, "I": 2}
{"Order_id": numberlong (207403)} -- >>{ "order_id": numberlong (241449)} on: shardseta {"T": 7000, "I": 4}
{"Order_id": numberlong (241449)} -- >>{ "order_id": numberlong (275497)} on: shardsetb {"T": 8000, "I": 2}
{"Order_id": numberlong (275497)} -- >>{ "order_id" :{$ maxkey: 1 }}on: shardsetb {"T": 8000, "I ": 3}
(4) view the database status information dB. Stats ()
> Use WLB
Switched to DB WLB
> DB. Stats ()
{
"Raw ":{
"Seta/127.0.0.1: 10000,127.0 .0.1: 10001,127.0 .0.1: 10002 ":{
"DB": "WLB ",
"Collections": 3,
"Objective": 105096,
"Avgobjsize": 1063.5204765167086,
"Datasize": 111771748,
"StorageSize": 141203712,
"NumExtents": 15,
"Indexes": 2,
"IndexSize": 8282112,
"FileSize": 251658240,
"OK": 1
},
"SETB/127.0.0.1: 20000,127.0 .0.1: 20001,127.0 .0.1: 20002 ":{
"DB": "WLB ",
"Collections": 3,
"Objective": 92691,
"Avgobjsize": 1063.9351824880516,
"Datasize": 98617216,
"Storagesize": 111143936,
"Numextents": 14,
"Indexes": 2,
"Indexsize": 7307264,
"Filesize": 251658240,
"OK": 1
},
"SETC/127.0.0.1: 30000,127.0 .0.1: 30001,127.0 .0.1: 30002 ":{
"DB": "WLB ",
"Collections": 3,
"Objective": 102231,
"Avgobjsize": 1063.9412311334136,
"Datasize": 108767776,
"Storagesize": 111143936,
"NumExtents": 14,
"Indexes": 2,
"IndexSize": 8052736,
"FileSize": 251658240,
"OK": 1
}
},
"Objective": 300018,
"Avgobjsize": 1063.791972481651,
"Datasize": 319156740,
"Storagesize": 363491584,
"Numextents": 43,
"Indexes": 6,
"Indexsize": 23642112,
"Filesize": 754974720,
"OK": 1
}
(5) view server status information dB. serverstatus ()
> DB. serverstatus ()
{
"Host": "zhangzha-283f5f: 50000 ",
"Version": "1.8.0 ",
"Process": "mongos ",
"Uptime": 1880,
"LocalTime": ISODate ("2011-03-24T15: 54: 15.328Z "),
"Mem ":{
"Resident": 5,
"Virtual": 38,
"Supported": true
},
"Connections ":{
"Current": 1,
"Available": 19999
},
"Extra_info ":{
"NOTE": "fields vary by platform"
},
"Opcounters ":{
"Insert": 300000,
"Query": 84,
"Update": 0,
"Delete": 0,
"Getmore": 0,
"Command": 68
},
"Ops ":{
"Sharded ":{
"Insert": 300000,
"Query": 0,
"Update": 0,
"Delete": 0,
"Getmore": 0,
"Command": 0
},
"NotSharded ":{
"Insert": 0,
"Query": 84,
"Update": 0,
"Delete": 0,
"Getmore": 0,
"Command": 68
}
},
"ShardCursorType ":{
},
"Asserts ":{
"Regular": 0,
"Warning": 0,
"Msg": 0,
"User": 0,
"Rolovers": 0
},
"Network ":{
"Bytes": 329380125,
"Bytesout": 42039,
"Numrequests": 300152
},
"OK": 1
}
>
Description of related fields:
Uptime: the server running time (in seconds ).
Localtime: the local time of the server.
Mem: Server Memory information.
Connections: current number of connections.
Opcounters: Operation statistics.