use of com.mongodb.MongoClient in project mongo-hadoop by mongodb.
the class SensorDataGenerator method run.
public void run() throws UnknownHostException {
final List<Integer> models = new ArrayList<Integer>();
final List<String> owners = new ArrayList<String>();
final MongoClient client = new MongoClient();
DB db = client.getDB("mongo_hadoop");
DBCollection devices = db.getCollection("devices");
DBCollection logs = db.getCollection("logs");
if ("true".equals(System.getenv("SENSOR_DROP"))) {
LOG.info("Dropping sensor data");
devices.drop();
logs.drop();
devices.createIndex(new BasicDBObject("devices", 1));
}
db.getCollection("logs_aggregate").createIndex(new BasicDBObject("devices", 1));
if (logs.count() == 0) {
for (int i = 0; i < 10; i++) {
owners.add(getRandomString(10));
}
for (int i = 0; i < 10; i++) {
models.add(getRandomInt(10, 20));
}
List<ObjectId> deviceIds = new ArrayList<ObjectId>();
for (int i = 0; i < NUM_DEVICES; i++) {
DBObject device = new BasicDBObject("_id", new ObjectId()).append("name", getRandomString(5) + getRandomInt(3, 5)).append("type", choose(TYPES)).append("owner", choose(owners)).append("model", choose(models)).append("created_at", randomDate(new Date(2000, 1, 1, 16, 49, 29), new Date()));
deviceIds.add((ObjectId) device.get("_id"));
devices.insert(device);
}
for (int i = 0; i < NUM_LOGS; i++) {
if (i % 50000 == 0) {
LOG.info(format("Creating %d sensor log data entries: %d%n", NUM_LOGS, i));
}
BasicDBList location = new BasicDBList();
location.add(getRandomInRange(-180, 180, 3));
location.add(getRandomInRange(-90, 90, 3));
DBObject log = new BasicDBObject("_id", new ObjectId()).append("d_id", choose(deviceIds)).append("v", getRandomInt(0, 10000)).append("timestamp", randomDate(new Date(2013, 1, 1, 16, 49, 29), new Date())).append("loc", location);
logs.insert(log);
}
}
}
use of com.mongodb.MongoClient in project mongo-hadoop by mongodb.
the class PrepareShakespeare method run.
@Override
public int run(final String[] args) throws Exception {
if (args.length < 2) {
printUsage();
return 1;
}
String inputFilePath = args[0];
String mongoURI = args[1];
MongoClientURI uri = new MongoClientURI(mongoURI);
MongoClient client = new MongoClient(uri);
DB gridfsDB = client.getDB(uri.getDatabase());
GridFS gridFS = new GridFS(gridfsDB);
Scanner scanner = new Scanner(new File(inputFilePath));
// Each work is dated with a year.
Pattern delimiter = Pattern.compile("^\\d{4}", Pattern.MULTILINE);
scanner.useDelimiter(delimiter);
int numWorks = 0;
// Drop database before uploading anything.
gridfsDB.dropDatabase();
try {
for (; scanner.hasNext(); ++numWorks) {
String nextWork = scanner.next();
// Skip legal notice/intro.
if (0 == numWorks) {
continue;
}
Scanner titleScanner = new Scanner(nextWork);
String workTitle = null;
while (titleScanner.hasNextLine()) {
String line = titleScanner.nextLine();
if (!line.isEmpty()) {
// Work title is first non-blank line.
workTitle = line;
break;
}
}
if (null == workTitle) {
throw new IOException("Could not find a title!");
}
GridFSInputFile file = gridFS.createFile(workTitle);
// Set chunk size low enough that we get multiple chunks.
file.setChunkSize(1024 * 10);
OutputStream os = file.getOutputStream();
os.write(nextWork.getBytes());
os.close();
}
} finally {
scanner.close();
client.close();
}
System.out.printf("Wrote %d works to GridFS.\n", numWorks);
return 0;
}
use of com.mongodb.MongoClient in project mongo-java-driver by mongodb.
the class MongoClientFactory method getObjectInstance.
/**
* This implementation will create instances of {@link MongoClient} based on a connection string conforming to the format specified in
* {@link MongoClientURI}.
* <p>The connection string is specified in one of two ways:</p>
* <ul>
* <li>As the {@code String} value of a property in the {@code environment} parameter with a key of {@code "connectionString"}</li>
* <li>As the {@code String} value of a {@link RefAddr} with type {@code "connectionString"} in an {@code obj} parameter
* of type {@link Reference}</li>
* </ul>
*
* Specification of the connection string in the {@code environment} parameter takes precedence over specification in the {@code obj}
* parameter. The {@code name} and {@code nameCtx} parameters are ignored.
*
* If a non-empty connection string is not specified in either of these two ways, a {@link MongoException} is thrown.
* @return an instance of {@link MongoClient} based on the specified connection string
* @throws MongoException
*
* Note: Not all options that can be specified via {@link com.mongodb.MongoClientOptions} can be specified via the connection string.
*/
@Override
public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment) throws Exception {
// Some app servers, e.g. Wildfly, use the environment to pass location information to an ObjectFactory
String connectionString = null;
if (environment.get(CONNECTION_STRING) instanceof String) {
connectionString = (String) environment.get(CONNECTION_STRING);
}
if (connectionString == null || connectionString.isEmpty()) {
LOGGER.debug(format("No '%s' property in environment. Casting 'obj' to java.naming.Reference to look for a " + "javax.naming.RefAddr with type equal to '%s'", CONNECTION_STRING, CONNECTION_STRING));
// javax.naming.RefAddr
if (obj instanceof Reference) {
Enumeration<RefAddr> props = ((Reference) obj).getAll();
while (props.hasMoreElements()) {
RefAddr addr = props.nextElement();
if (addr != null) {
if (CONNECTION_STRING.equals(addr.getType())) {
if (addr.getContent() instanceof String) {
connectionString = (String) addr.getContent();
break;
}
}
}
}
}
}
if (connectionString == null || connectionString.isEmpty()) {
throw new MongoException(format("Could not locate '%s' in either environment or obj", CONNECTION_STRING));
}
MongoClientURI uri = new MongoClientURI(connectionString);
return new MongoClient(uri);
}
use of com.mongodb.MongoClient in project mongo-java-driver by mongodb.
the class DatabaseAcceptanceTest method shouldBeAbleToListAllTheDatabasesAvailable.
@Test
public void shouldBeAbleToListAllTheDatabasesAvailable() {
MongoClient mongoClient = getMongoClient();
MongoDatabase firstDatabase = mongoClient.getDatabase("FirstNewDatabase");
MongoDatabase secondDatabase = mongoClient.getDatabase("SecondNewDatabase");
MongoDatabase otherDatabase = mongoClient.getDatabase("DatabaseThatDoesNotExistYet");
try {
// given
firstDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
secondDatabase.getCollection("coll").insertOne(new Document("aDoc", "to force database creation"));
//when
List<String> databaseNames = mongoClient.listDatabaseNames().into(new ArrayList<String>());
//then
assertThat(databaseNames, hasItems(firstDatabase.getName(), secondDatabase.getName()));
assertThat(databaseNames, not(hasItem(otherDatabase.getName())));
} finally {
//tear down
firstDatabase.drop();
secondDatabase.drop();
}
}
use of com.mongodb.MongoClient in project spring-boot by spring-projects.
the class EmbeddedMongoAutoConfigurationTests method specifyPortToZeroAllocateRandomPort.
@Test
public void specifyPortToZeroAllocateRandomPort() {
load("spring.data.mongodb.port=0");
assertThat(this.context.getBeansOfType(MongoClient.class)).hasSize(1);
MongoClient client = this.context.getBean(MongoClient.class);
Integer mongoPort = Integer.valueOf(this.context.getEnvironment().getProperty("local.mongo.port"));
assertThat(client.getAddress().getPort()).isEqualTo(mongoPort);
}
Aggregations