Mongodb集群JavaAPI插入数据

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

(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 }

>
(2)用Java代码完成数据初始化

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.MongoException;

public class MongodbTest {
 
 /**
  * 30W 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", "LP10012700003959" + 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", " 凯撒");
    doc.put("receiver_wangwang_id", "sanglin01");
    doc.put("receiver_mobile_phone", "13021525841");
    doc.put("receiver_zip_code", "650045");
    doc.put("receiver_telephone", "13868117135");
    doc.put("receiver_county_id", 350102);
    doc.put("receiver_address", "福建省^^^福州市^^^鼓楼区^^^的萨芬萨芬萨芬的12号");
    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 (MongoException e) {
   e.printStackTrace();
  }
 }
}  

这里是单线程执行,1台机器模拟3组复制集,每组3个分片,3个Configsvr,1个路由节点,插入30W条数据,总共花费了288.349秒的时间,平均每秒插入1041条数据,相当快的了。

(3)查看数据库分片信息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)查看数据库的状态信息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,
                        "objects" : 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,
                        "objects" : 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,
                        "objects" : 102231,
                        "avgObjSize" : 1063.9412311334136,
                        "dataSize" : 108767776,
                        "storageSize" : 111143936,
                        "numExtents" : 14,
                        "indexes" : 2,
                        "indexSize" : 8052736,
                        "fileSize" : 251658240,
                        "ok" : 1
                }
        },
        "objects" : 300018,
        "avgObjSize" : 1063.791972481651,
        "dataSize" : 319156740,
        "storageSize" : 363491584,
        "numExtents" : 43,
        "indexes" : 6,
        "indexSize" : 23642112,
        "fileSize" : 754974720,
        "ok" : 1
}

(5)查看服务器状态信息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,
                "rollovers" : 0
        },
        "network" : {
                "bytesIn" : 329380125,
                "bytesOut" : 42039,
                "numRequests" : 300152
        },
        "ok" : 1
}
>

相关字段说明:

uptime: 服务器运行时间(秒)。
localTime: 服务器本地时间。
mem: 服务器内存信息。
connections: 当前连接数。
opcounters: 操作统计。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值