A simple creation and test of a database table after the MongoDB shard cluster environment is built.

Source: Internet
Author: User


1. Enter MongoDB:

./bin/mongo ip:30000
IP varies by setting

2. Create a new database and Shard:

Mongos> use 502switched to DB 502mongos> sh.enablesharding ("502") {"OK": 1}
View table Status:

mongos> sh.status ()---sharding status---...//slightly    {  "_id": "502",  "partitioned": true,  "PRIMARY": " shard0000 "}


3. Create an index:

Mongos> Db.table1.ensureIndex ({"id": 1}) {"Raw": {"219.219.220.180:27017": {"createdcollectionautomatically": True, "Numindexesbefore": 1, "Numindexesafter": 2, "OK": 1}, "OK": 1}


4. Table Shards:

Mongos> sh.shardcollection ("502.table1", {"id": 1}) {"collectionsharded": "502.table1", "OK": 1}
View table Status:
<pre name= "code" class= "plain" >mongos> sh.status ()---sharding status---...///slightly
{"_id": "502", "partitioned": true, "PRIMARY": "shard0000"}502.table1shard key: {"id": 1}chunks:shard0000 1{"id" : {"$minKey": 1}}-->> {"id": {"$maxKey": 1}} on:shard0000 Timestamp (1, 0)

5. Store Data:

Mongos> for (Var i=1;i<=1000000;i++) {...  Db.table1.save ({"id": I, "x": Math.random (), "name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"}), ...  } Writeresult ({"ninserted": 1})

View Status:

mongos> sh.status ()---sharding status---<pre name= "code" class= "plain" ><pre name= "code" class= "plain" >...//slightly

    {  "_id": "502",  "partitioned": true,  "PRIMARY": "shard0000"}    &n bsp;   502.table1            shard key: {"id": 1}   & nbsp;        chunks:                 shard0000    3                shard0001    3                 shard0002    3            {"id": {"$minKey": 1}}-->& Gt {"id": 2} on:shard0001 Timestamp (2, 0)             {"id": 2}-->&gt ; {"id": Ten} on:shard0001 Timestamp (4, 0)             {"id": ten}-- > {"id": 117038} on:shard0002 Timestamp (7, 0)             {"id": 117038}-->> {"id": 242715} on:shard000 0 Timestamp (7, 1)             {"id": 242715}-->> {"id": 359743} o N:shard0002 Timestamp (5, 1)             {"id": 359743}-->> {"id" : 490166} on:shard0002 Timestamp (4, 3)             {"id": 490166}-- > {"id": 607194} on:shard0000 Timestamp (5, 2)             {"id": 607 194}-->> {"id": 767751} on:shard0000 Timestamp (5, 3)              {"id": 767751}-->> {"id": {"$maxKey": 1}} on:shard0001 Timestamp (6, 0)

To view specific data:

Mongos> Db.table1.find () {"_id": ObjectId ("55d45098da50bfb96a039f13"), "id": 1, "x": 0.03590293787419796, "name": " Xubo "," Time ":" 20150819 "," Ops ":" Testinserttimes "} {" _id ": ObjectId (" 55d451a7da50bfb96a07532d ")," id ": 242715," X " : 0.39869119925424457, "name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55D452AFDA50BF  B96a0b19c8 ")," id ": 490166," x ": 0.19621717440895736," name ":" Xubo "," Time ":" 20150819 "," Ops ":" Testinserttimes "} { "_id": ObjectId ("55d45098da50bfb96a039f14"), "id": 2, "x": 0.6352831239346415, "name": "Xubo", "Time": "20150819", " Ops ":" Testinserttimes "} {" _id ": ObjectId (" 55d451a9da50bfb96a07532e ")," id ": 242716," x ": 0.9266659175045788," name " : "Xubo", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d452b0da50bfb96a0b19c9"), "id": 490167, " X ": 0.00011568982154130936," name ":" Xubo "," Time ":" 20150819 "," Ops ":" Testinserttimes "} {" _id ": ObjectId (" 55d45098 Da50bfb96a039f15 ")," id " : 3, "x": 0.5743637685663998, "name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d45 1a9da50bfb96a07532f ")," id ": 242717," x ": 0.49728200025856495," name ":" Xubo "," Time ":" 20150819 "," Ops ":" Testinsert  Times "} {" _id ": ObjectId (" 55d452b0da50bfb96a0b19ca ")," id ": 490168," x ": 0.3094450223725289," name ":" Xubo "," Time ":  "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d45098da50bfb96a039f16"), "id": 4, "x": 0.397825826657936, "Name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d451a9da50bfb96a075330"), "id": 2 42718, "x": 0.11095952079631388, "name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d 452B0DA50BFB96A0B19CB ")," id ": 490169," x ": 0.6007435184437782," name ":" Xubo "," Time ":" 20150819 "," Ops ":" Testinser Ttimes "} {" _id ": ObjectId (" 55d45098da50bfb96a039f17 ")," id ": 5," x ": 0.0571024667005986," name ":" Xubo "," Time ":" 20 150819 "," Ops ":" TestinSerttimes "} {" _id ": ObjectId (" 55d451a9da50bfb96a075331 ")," id ": 242719," x ": 0.46688974485732615," name ":" Xubo "," Ti Me ":" 20150819 "," Ops ":" Testinserttimes "} {" _id ": ObjectId (" 55d452b0da50bfb96a0b19cc ")," id ": 490170," x ": 0.905515 0467902422, "name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d45098da50bfb96a039f18" ), "id": 6, "x": 0.2606754114385694, "name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d451a9da50bfb96a075332"), "id": 242720, "x": 0.024089211830869317, "name": "Xubo", "Time": "20150819", "Ops": "Te Stinserttimes "} {" _id ": ObjectId (" 55D452B0DA50BFB96A0B19CD ")," id ": 490171," x ": 0.7552208981942385," name ":" Xubo ", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d45098da50bfb96a039f19"), "id": 7, "x": 0.77443871  36772275, "name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"} {"_id": ObjectId ("55d451a9da50bfb96a075333"), "id": 242721, "X": 0.6262992226984352, "name": "Xubo", "Time": "20150819", "Ops": "Testinserttimes"}type "it" for more 

To view table storage:

Mongos> db.table1.stats () {"sharded": True, "Paddingfactornote": "Paddingfactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only. "," userflags ": 1," capped ": false," ns ":" 502.table1 "," Count ": 10000 XX, "numextents":, "size": 112000000, "storagesize": 182550528, "totalindexsize": 66119312, "indexsizes": {"_id_": 355 32896, "Id_1": 30586416}, "Avgobjsize": 2, "nindexes": 9, "Nchunks": {"shards": {"ns": "shard0000", "C Ount ": 403262," size ": 45165344," avgobjsize ":" Numextents ": 27869184," storagesize ": 86310912, ' lastextentsize ' , "Paddingfactor": 1, "Paddingfactornote": "Paddingfactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only. "," userflags ": 1," capped ": false," nindexes ": 2," Totalindexsize ": 2 4380832, "indexsizes": {"_id_": 13114304, "Id_1": 11266528}, "OK": 1}, "shard0001": {"ns": "502.table1", "Count": 232259 , "size": 26013008, "avgobjsize": "Numextents": 8, "sToragesize ": 37797888," lastextentsize ": 15290368," Paddingfactor ": 1," Paddingfactornote ":" Paddingfactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only. "," userflags ": 1," capped ": false," nindexes ": 2," totalindexsize ": 1 4038192, "indexsizes": {"_id_": 7546448, "Id_1": 6491744}, "OK": 1}, "shard0002": {"ns": "502.table1", "Count": 364479, " Size ": 40821648," avgobjsize ":" Numextents ": 9," storagesize ": 58441728," lastextentsize ": 20643840," Paddingfactor ": 1," Paddingfactornote ":" Paddingfactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only. "," userflags ": 1," capped ": false," nindexes ": 2," Totalindexsize ": 2 7700288, "indexsizes": {"_id_": 14872144, "Id_1": 12828144}, "OK": 1}}, "OK": 1}
Displays a total of 000000 documents, distributed as:

"shard0000" is "count": 403262;

"Shard0001" is "count": 232259;

"Shard0002" is "count": 364479;

The Shard effect is achieved, but not very evenly.



Resources:

[1] Chodorow K. mongodb:the definitive Guide[m]. "O ' Reilly Media, Inc.", 2013. Chinese Version







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A simple creation and test of a database table after the MongoDB shard cluster environment is built.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.