Mongodb叢集JavaAPI插入資料

來源:互聯網
上載者:User

在完成了叢集的搭建工作之後,需要做的就是建立一個資料庫,建立表,設定分區主鍵來初始化資料了!

(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: 操作統計。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.