3

I am attempting to programmatically "enable sharding" and set the "shard key" using java/scala API particularly casbah

Our config

scala 2.10
casbah 2.6 - "org.mongodb" % "casbah_2.10" % "2.6.0",
MongoDB 2.4.4

Also what is the casbah driver version for mongo 2.4.4 (with scala 2.10)

Our use case is such that, collections + indexes are created programmatically using the casbah scala API dbConnection.getCollection(....) and collection.ensureIndex(DBObject("orgId" -> 1), DBObject("background" -> true, "name" -> "org_idx", "unique" -> false))

Is there an equivalent casbah API to programmatically, enableSharding and choose shardKey as well as we are currently sharding our mongo cluster to scale out. Our database + collection names are not known ahead time and a dynamically created using API, so a enabling sharding using mongo shell is simply not an option.

Is there a better way to do this ? ANy recommendations?

4lex1v
  • 21,367
  • 6
  • 52
  • 86
conikeec
  • 209
  • 2
  • 14
  • since you are creating databases dynamically they will be round robin'ed around the shards - why do you need to shard any collections? wouldn't balancing load by putting different DBs on different shards be sufficient? – Asya Kamsky Jun 10 '13 at 03:57
  • How will a database be sharded without an enableShard command or choosing a shard key on the collection ? There are certain admin commands that have to be issued to do that – conikeec Jun 10 '13 at 04:37
  • when you have sharded cluster and you add a new database it will automatically be placed on a new(er) shard. databases will not be partitioned but they will all keep being placed on a next shard. The only time you need enableShard and shardCollection is when you want to partition a collection - I don't see why you would need to partition all these collections that you are generating programmatically. – Asya Kamsky Jun 10 '13 at 04:38
  • Thanks for the insight, Asya .. I will get it a shot ... – conikeec Jun 10 '13 at 05:10

2 Answers2

1

Here's what worked for me, building on @Ross's answer, which didn't quite work for me:

import com.mongodb.casbah.Imports._

// Connect to MongoDB
val conn = MongoClient()
val adminDB = conn("admin")

// Enable sharding on the DB
adminDB.command(MongoDBObject("enableSharding" -> <database>))

// Create the index for our shard key
val shardKey = MongoDBObject("_id" -> "hashed")
conn(<database>)(<collection>).ensureIndex(shardKey)

// Enable sharding on the collection
adminDB.command(MongoDBObject("shardCollection" -> "<database>.<collection>", 
    "key" -> shardkey))
Kyrstellaine
  • 441
  • 5
  • 16
0

For completeness and to help others - enableSharding is a command (see the enableSharding docs) and you can run any command from casbah by using db.command.

import com.mongodb.casbah.Imports._

// Connect to MongoDB
val conn = MongoClient()
val adminDB = conn("admin")


// Enable sharding
adminDB.command(MongoDBObject("shardCollection" -> "<database>.<collection>", "key" -> <shardkey>))

The part should be a MongoDBObject defining the shardkey.

As Asya mentions this might not be the right solution for your use case but its certainly possible to do pragmatically using casbah.

Ross
  • 17,861
  • 2
  • 55
  • 73