将JSON文件导入到mongos

3
我正在模拟MongoDB的分片。已经配置了分片集群,我在三台不同的物理机器上连接了三个分片。
分片1、分片2、分片3。
我连接到mongos并创建了一个数据库、一个集合,并在所创建的数据库上启用了分片。
现在我需要插入一个大型数据集,以便可以对该集合进行分片。 我使用以下命令行导入mongoimport。 command line 我的问题是这个数据集被导入到主机上的本地数据库中。例如,如果我连接一个mongo shell到mongod,我可以看到新的数据库有XX.GB大小,但是我连接到一个mongos的配置服务器时,当我在mongos上运行show dbs时,我看到创建的数据库只有00.GB。
2个回答

4

您需要通过--host参数针对mongos实例来发布mongoimport,以便路由服务可以适当地将数据分发到分片上。


因为我已经阅读过,mongoimport不打算在连接到mongo或mongos时运行。 - user2307236
您提到您有一个分片集群,因此必须运行一个mongos实例,该实例从配置实例中读取信息以了解如何对您的集合进行分片,并且您必须通过--host和适当的IP / DNS名称和端口将您的mongoimport命令行指向此mongos实例。 - DAXaholic
@user2307236,我已经详细地给出了完整的示例...如果您还有任何问题,请告诉我。 - user641887

4
想象一下你的分片环境类似于下面这样,有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. 现在指定需要进行分片的集合和键。
> mongos> sh.shardCollection("sharddb.neighbourhoods",{"_id":1},true) 
> {"collectionsharded" : "sharddb.neighbourhoods", "ok" : 1 }

一旦您指定了所有细节,请通过命令提示符执行以下mongoimport命令

> 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

这将在分片数据库中创建名为neighborhoods的集合,作为分片集合。

您可以通过以下方式检查分片集合:

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


我能解答您的疑问吗?@user2307236 - user641887

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接