MongoDb unable to insert documents using Java Driver in sharded env -
when try connect mongo db sharded @ key "state" via jdbc, , insert document, says missing shardkey
my shards (2 no, part of individual replica sets) , config servers (1 no, part of replica set)are , running, , can insert document using mongos terminal, cant insert documents using java driver. mongodb version 2.6.5.
package dao; import util.instancefactory; import com.mongodb.db; import com.mongodb.mongoclient; import org.apache.log4j.logger; public class mongoconnector { private static mongoconnector mongoconnectorinstance; private db dbconnection; public static db getconnection(string dbname) { mongoconnector.mongoconnectorinstance = instancefactory.instantiateassingleton(mongoconnector.class); if (mongoconnector.mongoconnectorinstance.dbconnection == null) { synchronized (mongoconnector.mongoconnectorinstance) { if (mongoconnector.mongoconnectorinstance.dbconnection == null) { mongoconnector.mongoconnectorinstance.dbconnection = mongoconnector.mongoconnectorinstance .getdbconnection(dbname); } } } return mongoconnector.mongoconnectorinstance.dbconnection; } private db getdbconnection(string dbname) { db db = null; try { mongoclient mongoclient = new mongoclient("some ip address of mongos", 27017); // connect databases db = mongoclient.getdb(dbname); system.out.println("connect database successfully"); } catch (exception e) { system.err.println(e.getclass().getname() + ": " + e.getmessage()); } return db; } }
now when try insert document using other class below function shard key not present code 61
public static boolean insert(dbobject object, string table) { boolean status = false; if (object != null) { try { db db = mongoconnector.getconnection("somedb"); dbcollection collection = db.getcollection(table); //system.out.println(collection); if (collection == null) { db.createcollection(table, null); } collection.insert(object); status = true; } catch (exception e) { mongodao.logger.error("could not insert in database" + e); } } return status; }
my object inserted looks
dbobject o=new basicdbobject("somecolumn":"somevalue") .append("shardkey":"up"); mongodao.insert(o,"sometable");
am missing configuartion connect shards?
if shard key state
, problem construction of document:
dbobject o = new basicdbobject("somecolumn" : "somevalue") .append("shardkey" : "up");
it should like
dbobject o = new basicdbobject("somecolumn" : "somevalue") .append("state" : "up");
it looks did not set shard key correctly.
also, couple of other notes sharding setup:
you said have 1 config server. use 3. not use 1 config server, ever, except small test deployments don't care @ all. use 3 config servers.
you said shard key has 36 possible values. few. 36 possible values, can have @ 36 possible chunks distributed between shards. small number of potential chunks make hard or impossible mongodb balance amount of data between shards. need reconsider shard key. choosing shard key critical part of scaling mongodb deployment , making right choice requires lot of knowledge system's use case; can start some advice on choosing shard keys manual.
Comments
Post a Comment