欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

MongoDB集群JavaAPI插入数据

程序员文章站 2024-01-26 16:00:04
...

在完成了MongoDB集群的搭建工作之后,需要做的就是建立一个数据库,建立表,设置分片主键来初始化数据了!

在完成了MongoDB集群的搭建工作之后(见 ),需要做的就是建立一个数据库,建立表,设置分片主键来初始化数据了!

(1)建立WLB数据库,设置分表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 mongo.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 //在wlb数据库不存在的情况下,,就会新建一个数据库
switched to db wlb
> db.createCollection('wlb_orders') //创建一个表wlb_orders
{ "ok" : 1 }
> use admin
switched to db admin
> db.runCommand({enablesharding:'wlb'}) //设置数据库可以分片
{ "ok" : 1 }
> db.runCommand({shardcollection:'wlb.wlb_orders',key:{order_id:1}}) //设置表的分区主键为order_id
{ "collectionsharded" : "wlb.wlb_orders", "ok" : 1 }
> 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:
ShardSetA 1
{ "order_id" : { $minKey : 1 } } -->> { "order_id" : { $maxKey : 1 } } on : ShardSetA { "t" : 1000, "i" : 0 }

>

MongoDB集群JavaAPI插入数据