MongoDB Fragment Test _mongodb

Source: Internet
Author: User
Tags mongoclient mongodb

Fragmentation is a way of mongodb expansion. The fragment splits a collection and stores the different parts on different machines. When the collections phase of a database is too large for the current space, you need to add a new machine. Fragmentation automatically distributes collection data to new servers.

1. Connect to MONGOs to view system-related information

Configsvr> Show dbs configsvr> use config configsvr> show collections onfigsvr> Db.mongos.find () {"_id": "Ra cdb:28885 "," ping ": Isodate (" 2016-03-21t09:23:05.106z ")," Up ": Numberlong (1436)," Waiting ": true," mongoversion ":" 3.2.3 "} {" _id ":" host8.localdomain:28885 "," ping ": Isodate (" 2016-03-21t09:23:07.960z ")," Up ": Numberlong (1427)," Waiting ": true," mongoversion ":" 3.2.3 "} {" _id ":" host9.localdomain:28885 "," ping ": isodate (" 2016-03-21t09:23:03.521z ")," Up ": Numberlong (1407)," Waiting ": true," mongoversion ":" 3.2.3 "} configsvr> Db.shards.find () {" _id ":" Shard1 ", "Host": "shard1/host8:28017,racdb:28017"} {"_id": "Shard2", "host": "shard2/host8:28018,racdb:28018"} configsvr> D B.databases.find () {"_id": "Im_offline_msg", "PRIMARY": "Shard1", "Partitioned": true} {"_id": "TestDB", "PRIMARY": "  Shard2 "," Partitioned ": true} {" _id ":" Test "," PRIMARY ":" Shard1 "," Partitioned ": true} {" _id ":" Blogdb "," PRIMARY " : "Shard2", "partitioned": false}

2. Enable fragmentation of the database

2.1 Currently can connect to MONGOs view database or collection fragmentation (no fragmentation):

Mongos> db.stats ()
mongos> db.tab.stats ()

2.2 To the database to activate the fragmentation function:

# MONGO racdb:28885
mongos>sh.enablesharding ("test")
#或者
# MONGO racdb:28885
mongos> Use admin
mongos> Db.runcommand ({enablesharding: "Blogdb"})

2.3 View the database partition situation at this time, partitioned to "true".

configsvr> use config
switched to db config
configsvr> db.databases.find ()
{"_id": "Im_offline_ Msg "," PRIMARY ":" Shard1 "," Partitioned ": true}
{" _id ":" TestDB "," PRIMARY ":" Shard2 "," partitioned ": True}
   {"_id": "Test", "PRIMARY": "Shard1", "Partitioned": true}
{"_id": "Blogdb", "PRIMARY": "Shard2", "partitioned" ": true}

Enabling database fragmentation does not separate the data and also requires fragmentation of the collection.

3. Enable fragmentation of the collection

There are several issues to consider before you enable:

Select which key column to use as Shard key. (More references: Considerations for selecting Shard Keys)

If the data already exists in the collection, the key column selected as Shard key must be indexed, and if the collection is empty, MongoDB will create the index when the collection fragment (sh.shardcollection) is activated.

Set piecewise function Sh.shardcollection,

Sh.shardcollection (".", Shard-key-pattern)

Mongos>sh.shardcollection ("Test.tab", {"_id": "Hashed"})

Test Insert Data:

--Using the python command
#创建python文件
$ vi batch_insert.py
#-*-coding:utf-8-*-
import pymongo
client = Pymongo. Mongoclient ("racdb", 28885)
db = Client.testdb
#查看testdb数据库中集合信息
print (Db.collection_names ())
# Connect to My_collection collection
print (db.my_collection)
#清空my_collection集合文档信息
db.my_collection.remove ()
#显示my_collection集合中文档数目
Print (Db.my_collection.find (). Count ())
#插入10000条文档信息 for the
I in range ( 10000):
Db.my_collection.insert ({"id": I, "name": "Licz"})
#显示my_collection集合中文档数目
print (' Insert completed, Current number of documents: ')
print (Db.my_collection.find (). Count ())
#执行插入
[mongod@racdb ~]$ Python2.7.3batch_ insert.py
[u ' system.indexes ', U ' table1 ', U ' my_collection ']
collection (Database (mongoclient racdb:28885 '],document_class=dict, Tz_aware=false, connect=true), U ' TestDB '), U ' my_collection ')
0

Insert complete, number of current documents:

10000
#或是用mongo Shell Insert test data
for (var i=1 i<=100000; i++) {
Db.cc.insert ({"id": I, "myname": "CC" +i, " MyDate ": New Date ()});
}

Enable Collection Fragmentation

Mongos> Show Collections
mongos> db.cc.find ()
mongos> Db.cc.createIndex ({"id": "hashed"})
Mongos> db.cc.getIndexes ()
mongos>sh.shardcollection ("testdb.cc", {"id": "hashed"})
mongos> Db.stats ()
mongos> db.cc.stats ()
--View sharding status
mongos> db.printshardingstatus ();

The above content is small series to introduce the MongoDB fragmentation test, I hope to help you!

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.