Mongodb集群JavaAPI插入数据

原创 2011年03月24日 13:26:00

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

(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: 操作统计。

 

Spring+mongodb集群集成(吐血教程)

前一篇文章中介绍了在window系统下部署mongodb集群,按照教程一步步实现的话应该都可以在本机部署成功了,上篇文章末尾也提到了,想集成到SSM框架中去,网上资料基本都是单个mongodb数据库的...
  • qq_16497617
  • qq_16497617
  • 2016年10月15日 08:26
  • 7339

mongodb集群分片+副本模式操作+javaCode

mongodb集群分片+副本模式操作 本人规划的三个节点分别为192.168.46.133(mongo1) 192.168.46.134(mongo2) 192.168.46.135(m...
  • u011191463
  • u011191463
  • 2017年04月11日 17:36
  • 1009

mongo-java-driver连接mongo集群

此处mongo-java-driver版本为2.13 import java.net.UnknownHostException; import java.util.Arrays; import c...
  • Truong
  • Truong
  • 2017年07月06日 10:14
  • 841

mongoDB中的数据类型

mongoDB中的数据类型:Date、ObjectId、NumberLong、NumberInt Date mongo shell中提供各式各样的返回日期类型的方法,例如字符串类型或者Date对象类型...
  • user_longling
  • user_longling
  • 2016年09月01日 12:50
  • 3508

Mongodb数据库查询NumberLong数据类型的方式

这种数据类型必须要在NumberLong对象里面加上双引号,才能执行正常查询动作。db.t_element_access_reason.find( { "sessionId" : NumberLong...
  • u014344668
  • u014344668
  • 2017年05月18日 15:54
  • 3197

将Mongodb中NumberLong类型转String类型方法

将Mongodb中Numberlong类型转String类型方法 本文介绍将.CSV文件数据导入到Mongodb数据库后,Numberlong类型数据如何转换为String类型数据。 电脑配置:...
  • u010430471
  • u010430471
  • 2016年12月12日 19:14
  • 4879

Spring+mongodb集群集成(吐血教程)

前一篇文章中介绍了在window系统下部署mongodb集群,按照教程一步步实现的话应该都可以在本机部署成功了,上篇文章末尾也提到了,想集成到SSM框架中去,网上资料基本都是单个mongodb数据库的...
  • qq_16497617
  • qq_16497617
  • 2016年10月15日 08:26
  • 7339

Mongodb3.0.5副本集搭建及spring和java连接副本集配置

这是去年写的一篇文档,最近突然发现并没有发不出来,因此现在补上,希望能对某些朋友有所帮助。因为当时记录时没有截图,因此这里看起来可能就比较单调。一、基本环境:mongdb3.0.5数据库spring-...
  • tuzongxun
  • tuzongxun
  • 2016年06月20日 21:05
  • 17650

mongodb java api常用方法的使用以及和spring的集成使用

本篇文章主要介绍了mongodb对应java的常用增删改查的api,以及和spring集成后mongoTemplate的常用方法使用,废话不多说,直接上代码: 1.首先上需要用到的两个实体类Us...
  • u014033756
  • u014033756
  • 2016年10月14日 15:30
  • 2519

redis集群javaApi

pom.xml需要这2个包 redis.clients jedis 2.8.0 net.sourceforge.cobertu...
  • baidu_19473529
  • baidu_19473529
  • 2017年01月11日 11:08
  • 657
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Mongodb集群JavaAPI插入数据
举报原因:
原因补充:

(最多只允许输入30个字)