Dear Thomas,
the answers to your queries are:
Could you connect to the mongos through the shell directly (mongo --port
30000) and try to access the document there? Are you able to retrieve
it?
When I connect directly to mongos shell and query the data, i get nothing. Below is the shell commands:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
D:\Users\tecnex>cd D:\mongodb\bin
D:\mongodb\bin>mongo localhost:30000/admin
MongoDB shell version: 2.0.6
connecting to: localhost:30000/admin
mongos> db.testShard.find();
mongos> show collections
mongos>
In your Java code, can you connect to the shard directly that contains
the document, instead of the mongos? Are you able to retrieve it?
Sorry, i do not understand your question. Therefore i am putting my whole code here:
//--------------------------------------------------------------------------------------------
package main.com.deftlabs.examples.mongo;
import com.mongodb.*;
import java.security.MessageDigest;
import java.util.Random;
// JUnit
import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.*;
// Java
import java.util.List;
import java.util.Random;
import java.util.ArrayList;
import java.security.MessageDigest;
import org.bson.types.Binary;
/**
*
* @author tecnex
*/
public class ShardingTest {
static DBCollection coll,coll2,coll3;
static DB db;
private static void insert1() throws Exception {
final Mongo mongo2 = new Mongo(new DBAddress("127.0.0.1", 30000,
"ExampleShardingData"));
db = mongo2.getDB("ExampleShardingD
ata");
coll = db.getCollection("testShard");
BasicDBObject view1 = new BasicDBObject();
view1.put("x",20110114);
DBCursor cur = coll.find(view1);
Double id;
Binary b;
if (cur.hasNext()) {
DBObject o = cur.next();
id = (Double) (o.get("x"));
b = (Binary) (o.get("hash"));
System.out.println(id);
System.out.println(b);
}
}
public ShardingTest() {
}
private static final int [] _shardPorts = { 10049, 10057 };
@Before
public static void setupCluster() throws Exception {
// Connect to mongos
final Mongo mongo = new Mongo(new DBAddress("127.0.0.1", 30000, "admin"));
// Add the shards
/* for (final int shardPort : _shardPorts) {
final CommandResult result
= mongo.getDB("admin").command(new BasicDBObject("addshard", ("localhost:" + shardPort)));
System.out.println(result);
}
// Sleep for a bit to wait for all the nodes to be intialized.
Thread.sleep(5000);
// Enable sharding on a collection.
CommandResult result
= mongo.getDB("admin").command(new BasicDBObject("enablesharding", "ExampleShardingData"));
System.out.println(result);*/
final BasicDBObject shardKey = new BasicDBObject("x", 1);
shardKey.put("hash", 1);
final BasicDBObject cmd = new BasicDBObject("shardcollection", "ExampleShardingData.testShard");
cmd.put("key", shardKey);
CommandResult result4 = mongo.getDB("admin").command(cmd);
System.out.println(result4);
final BasicDBObject re = new BasicDBObject("split", "ExampleShardingData.testShard");
final BasicDBObject mid = new BasicDBObject("x", 100);
re.put("middle", mid);
CommandResult result3 = mongo.getDB("admin").command(re);
System.out.println(result3);
final BasicDBObject mov = new BasicDBObject("moveChunk", "ExampleShardingData.testShard");
final BasicDBObject mo2 = new BasicDBObject("x", 10);
mov.put("find", mo2);
mov.put("to", "shard0001");
CommandResult result2 = mongo.getDB("admin").command(mov);
System.out.println(result2);
}
@Test
public static void testShards() throws Exception {
final Mongo mongo = new Mongo(new DBAddress("127.0.0.1", 27017, "ExampleShardingData"));
final DBCollection shardCollection = mongo.getDB("ExampleShardingData").getCollection("testShard");
final Random random = new Random(System.currentTimeMillis());
// Write some data
for (int idx=0; idx < 1000; idx++) {
final BasicDBObject entry
= new BasicDBObject("x", ("201101" + String.format("%02d", random.nextInt(30))));
entry.put("hash", md5(("this is a value to hash-" + idx)));
shardCollection.insert(entry);
}
}
private static byte [] md5(final String pValue) throws Exception
{ return MessageDigest.getInstance("MD5").digest(pValue.getBytes("UTF-8")); }
public static void main(String[] args) {
// TODO code application logic here
try {
testShards();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
setupCluster();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
insert1();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
//-------------------------------------------------------------------------------------
yes i have Java Tutorials and also implemented them. They ware running accurately. What i want is to make shards through java driver and then query the data in those shards.
Regards,
Rahat Masood.