MongoDB Fragment Test

Source: Internet
Author: User
Tags mongoclient mongodb

Replicaset+sharding Deployment: http://blog.csdn.net/lichangzai/article/details/50927588

MongoDB fragment Test 1. Connect to MONGOs to view system-related information

Configsvr> Show DBS

configsvr> use Config

Configsvr> Show Collections

Onfigsvr> Db.mongos.find ()

{"_id": "racdb: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> 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": false}

2. Enable fragmentation of the database

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

[Plain] View plain copy on the code to see a piece of coding derived to my Code slice

Mongos> Db.stats ()

Mongos> Db.tab.stats ()

2.2 To the database to activate the fragmentation function:

[Plain] View plain copy on the code to see a piece of coding derived to my Code slice

# 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 ("<database>.<collection>", 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 ()

# Displays the number of documents in the My_collection collection

print (Db.my_collection.find (). Count ())

#插入10000条文档信息 for I in  range ( 10000):

    Db.my_collection.insert ({"id": I, "name": "Licz"})

 

#显示my_collection集合中文档数目

print  (' Insert complete, current document number: ')

print (Db.my_collection.find (). Count ())

#执行插入

[Mongod@racdb ~]$ python2.7.3batch_insert.py

[u ' system.indexes ', U ' table1 ', U ' my_collection ']

Collection (mongoclient (host=[' racdb:28885 '],document_class=dict, Tz_aware=false, connect=true), U ' TestDB '), U ' my_collection ')

0

Insert complete, number of current documents:

10000

Inserting test data #或是用mongo shell

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 ();

Reference: http://blog.csdn.net/kk185800961/article/details/45932747

Http://www.cnblogs.com/magialmoon/archive/2013/04/11/3015394.html

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.