将 JSON 文件导入 mongos

Import JSON file to mongos

我正在 MongoDB 中模拟分片。 sharded cluster已经配置好了,我在三台不同的物理机上通过网络连接了三个shard。

分片 1、分片 2、分片 3

我连接到 mongos 并创建了一个数据库、一个集合并在创建的数据库上启用了分片。

现在我需要插入一个大数据集,以便我可以对集合进行分片。 我正在使用 mongoimport 如下。

我的问题是这个数据集正在导入到主机上的本地数据库中。例如。如果我将 mongo shell 连接到 mongod 我可以看到新数据库有 XX.GB 但我连接到带有 mongo 的配置服务器s 但是当我在 mongos 上 运行 show dbs 时,我在创建的数据库中看到 00.GB。

您必须通过 --host 参数针对 mongos 实例发出 mongoimport,以便路由服务可以适当地将数据分发到分片

想象一下您的分片环境如下所示。有 3 个分片,每个分片有 3 个服务器。

   shards:
            {  "_id" : "s0",  "host" : "s0/localhost:37017,localhost:37018,localhost:37019" }
            {  "_id" : "s1",  "host" : "s1/localhost:47018,localhost:47019" }
            {  "_id" : "s2",  "host" : "s2/localhost:57017,localhost:57018,localhost:57019" }
      active mongoses:
            "3.2.5" : 1

现在按照以下步骤将 json 导入分片环境。

  1. 通过 mongos 连接到任何分片实例。 mongo --port 47018,这会让您进入 mongos> 提示符。

  2. 给出以下命令。

   mongos> sh.enableSharding("sharddb");
    { "ok" : 1 }

此命令告诉 mongodb 数据库 "sharddb" 已启用分片。

  1. 现在指定需要分片的集合和key。
> mongos> sh.shardCollection("sharddb.neighbourhoods",{"_id":1},true) 
> {"collectionsharded" : "sharddb.neighbourhoods", "ok" : 1 }

指定所有详细信息后,通过命令提示符执行以下 mongo导入命令

> C:\Users\yc03ak1>mongoimport --db sharddb --collection
> "neighbourhoods" --drop --type json  --host "localhost:47018"  --file
> "c:\Users\yc03ak1\Desktop\neighborhoods.json"

> 2016-08-14T15:32:03.087-0700    connected to: localhost:47018
> 2016-08-14T15:32:03.091-0700    dropping: sharddb.neighbourhoods
> 2016-08-14T15:32:04.743-0700    imported 195 documents

这将在 sharddb 的集合社区中创建文档作为分片集合。

您可以通过

检查分片集合
mongos> sh.status();
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("57a8f3d77ce8ef0f68a210c9")
}
  shards:
        {  "_id" : "s0",  "host" : "s0/localhost:37017,localhost:37018,localhost:37019" }
        {  "_id" : "s1",  "host" : "s1/localhost:47018,localhost:47019" }
        {  "_id" : "s2",  "host" : "s2/localhost:57017,localhost:57018,localhost:57019" }
  active mongoses:
        "3.2.5" : 1
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  5
        Last reported error:  HostUnreachable
        Time of Reported error:  Thu Aug 11 2016 18:02:14 GMT-0700 (Pacific Standard Time)
        Migration Results for the last 24 hours:
                No recent migrations
  databases:
        {  "_id" : "projects",  "primary" : "s1",  "partitioned" : true }
                projects.students
                        shard key: { "student_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                s0      1
                                s1      1
                                s2      1
                        { "student_id" : { "$minKey" : 1 } } -->> { "student_id" : 1 } on : s2 Timestamp(3, 0)
                        { "student_id" : 1 } -->> { "student_id" : 25 } on : s1 Timestamp(3, 1)
                        { "student_id" : 25 } -->> { "student_id" : { "$maxKey" : 1 } } on : s0 Timestamp(2, 0)
        {  "_id" : "test",  "primary" : "s2",  "partitioned" : true }
                test.zipcodes
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                s2      1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : s2 Timestamp(1, 0)
        {  "_id" : "sharddb",  "primary" : "s2",  "partitioned" : true }
                sharddb.neighbourhoods
                        shard key: { "_id" : 1 }
                        unique: true
                        balancing: true
                        chunks:
                                s2      1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : s2 Timestamp(1, 0)

mongos>

HTH..