本文整理汇总了Java中io.vertx.ext.mongo.MongoClient类的典型用法代码示例。如果您正苦于以下问题:Java MongoClient类的具体用法?Java MongoClient怎么用?Java MongoClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MongoClient类属于io.vertx.ext.mongo包,在下文中一共展示了MongoClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: MongoHandle
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
public MongoHandle(Vertx vertx, JsonObject conf) {
JsonObject opt = new JsonObject();
String h = Config.getSysConf("mongo_host", "localhost", conf);
if (!h.isEmpty()) {
opt.put("host", h);
}
String p = Config.getSysConf("mongo_port", "27017", conf);
if (!p.isEmpty()) {
opt.put("port", Integer.parseInt(p));
}
String dbName = Config.getSysConf("mongo_db_name", "", conf);
if (!dbName.isEmpty()) {
opt.put("db_name", dbName);
}
logger.info("Using mongo backend at " + h + " : " + p + " / " + dbName);
this.cli = MongoClient.createShared(vertx, opt);
}
示例2: example2
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
public void example2(ServiceDiscovery discovery) {
// Get the record
discovery.getRecord(
new JsonObject().put("name", "some-data-source-service"),
ar -> {
if (ar.succeeded() && ar.result() != null) {
// Retrieve the service reference
ServiceReference reference = discovery.getReferenceWithConfiguration(
ar.result(), // The record
new JsonObject().put("username", "clement").put("password", "*****")); // Some additional metadata
// Retrieve the service object
MongoClient client = reference.get();
// ...
// when done
reference.release();
}
});
}
示例3: example3
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
public void example3(ServiceDiscovery discovery) {
MongoDataSource.<JsonObject>getMongoClient(discovery,
new JsonObject().put("name", "some-data-source-service"),
new JsonObject().put("username", "clement").put("password", "*****"), // Some additional metadata
ar -> {
if (ar.succeeded()) {
MongoClient client = ar.result();
// ...
// Dont' forget to release the service
ServiceDiscovery.releaseServiceObject(discovery, client);
}
});
}
示例4: testWithSugar
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
@Test
public void testWithSugar() throws InterruptedException {
Record record = MongoDataSource.createRecord("some-mongo-db",
new JsonObject().put("connection_string", "mongodb://localhost:12345"),
new JsonObject().put("database", "some-raw-data"));
discovery.publish(record, r -> { });
await().until(() -> record.getRegistration() != null);
AtomicBoolean success = new AtomicBoolean();
MongoDataSource.getMongoClient(discovery, new JsonObject().put("name", "some-mongo-db"),
ar -> {
MongoClient client = ar.result();
client.getCollections(coll -> {
client.close();
success.set(coll.succeeded());
});
});
await().untilAtomic(success, is(true));
}
示例5: loadData
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
private static void loadData(MongoClient db) {
db.find("users", new JsonObject(), lookup -> {
// error handling
if (lookup.failed()) {
dropAndCreate(db);
return;
}
if(lookup.result().isEmpty()){
dropAndCreate(db);
}else {
System.out.println("users already exists");
}
});
}
示例6: start
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
@Override
public void start() throws Exception {
int cores = Runtime.getRuntime().availableProcessors();
LOGGER.info("Deploying {0} RouteCalculationVerticles", cores - 1);
DeploymentOptions routeMgrOptions = new DeploymentOptions().setWorker(true).setInstances(cores - 1).setConfig(config());
mongo = MongoClient.createShared(vertx, config().getJsonObject("mongodb", new JsonObject()));
vertx.deployVerticle(RouteCalculationVerticle.class.getName(), routeMgrOptions, w -> {
if (w.failed()) {
LOGGER.error("Deployment of RouteManager failed." + w.cause());
} else {
createDemoSimulation();
indexRoutes();
indexTraffic();
indexTrucks();
createLargeDemoSimulation();
}
});
}
示例7: Mongi
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
/**
*
* @param vertx
*/
public Mongi(Vertx vertx) {
this.vertx = vertx;
/*
* We need a list of classes, These will need to be linked to TypeAdapters
*/
Class[] allow = {
String.class,
Integer.class,
Boolean.class,
Double.class,
Date.class,
Calendar.class,
LocalTime.class
};
mongoClient = MongoClient.createShared(vertx, new JsonObject());
}
示例8: loadData
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
private static void loadData(MongoClient db) {
db.find("users", new JsonObject(), lookup -> {
// error handling
if (lookup.failed()) {
dropAndCreate(db);
return;
}
if (lookup.result().isEmpty()) {
dropAndCreate(db);
} else {
System.out.println("users already exists");
}
});
}
示例9: start
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
@Override
public void start(Future<Void> future) {
mongo = new MongoDAO(MongoClient.createShared(vertx, config.getJsonObject("mongo")));
RedisDAO redis = new RedisDAO(RedisClient.create(vertx, RedisUtils.createRedisOptions(config.getJsonObject("redis"))));
authApi = new AuthenticationApi(mongo);
feedsApi = new FeedsApi(mongo, redis);
server = vertx.createHttpServer(createOptions());
server.requestHandler(createRouter()::accept);
server.listen(result -> {
if (result.succeeded()) {
future.complete();
} else {
future.fail(result.cause());
}
});
}
示例10: example6
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
public void example6(MongoClient mongoClient) {
// Match any documents with title=The Hobbit
JsonObject query = new JsonObject()
.put("title", "The Hobbit");
// Set the author field
JsonObject update = new JsonObject().put("$set", new JsonObject()
.put("author", "J. R. R. Tolkien"));
UpdateOptions options = new UpdateOptions().setMulti(true);
mongoClient.updateCollectionWithOptions("books", query, update, options, res -> {
if (res.succeeded()) {
System.out.println("Book updated !");
} else {
res.cause().printStackTrace();
}
});
}
示例11: example13_0
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
public void example13_0(MongoClient mongoService) {
JsonObject document = new JsonObject()
.put("title", "The Hobbit")
//ISO-8601 date
.put("publicationDate", new JsonObject().put("$date", "1937-09-21T00:00:00+00:00"));
mongoService.save("publishedBooks", document, res -> {
if (res.succeeded()) {
String id = res.result();
mongoService.findOne("publishedBooks", new JsonObject().put("_id", id), null, res2 -> {
if (res2.succeeded()) {
System.out.println("To retrieve ISO-8601 date : "
+ res2.result().getJsonObject("publicationDate").getString("$date"));
} else {
res2.cause().printStackTrace();
}
});
} else {
res.cause().printStackTrace();
}
});
}
示例12: example14_01_dl
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
@Source(translate = false)
public void example14_01_dl(MongoClient mongoService) {
//This could be a serialized object or the contents of a pdf file, etc, in real life
byte[] binaryObject = new byte[40];
JsonObject document = new JsonObject()
.put("name", "Alan Turing")
.put("binaryStuff", new JsonObject().put("$binary", binaryObject));
mongoService.save("smartPeople", document, res -> {
if (res.succeeded()) {
String id = res.result();
mongoService.findOne("smartPeople", new JsonObject().put("_id", id), null, res2 -> {
if (res2.succeeded()) {
byte[] reconstitutedBinaryObject = res2.result().getJsonObject("binaryStuff").getBinary("$binary");
//This could now be de-serialized into an object in real life
} else {
res2.cause().printStackTrace();
}
});
} else {
res.cause().printStackTrace();
}
});
}
示例13: example14_02_dl
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
public void example14_02_dl(MongoClient mongoService) {
//This could be a the byte contents of a pdf file, etc converted to base 64
String base64EncodedString = "a2FpbHVhIGlzIHRoZSAjMSBiZWFjaCBpbiB0aGUgd29ybGQ=";
JsonObject document = new JsonObject()
.put("name", "Alan Turing")
.put("binaryStuff", new JsonObject().put("$binary", base64EncodedString));
mongoService.save("smartPeople", document, res -> {
if (res.succeeded()) {
String id = res.result();
mongoService.findOne("smartPeople", new JsonObject().put("_id", id), null, res2 -> {
if (res2.succeeded()) {
String reconstitutedBase64EncodedString = res2.result().getJsonObject("binaryStuff").getString("$binary");
//This could now converted back to bytes from the base 64 string
} else {
res2.cause().printStackTrace();
}
});
} else {
res.cause().printStackTrace();
}
});
}
示例14: example15_dl
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
public void example15_dl(MongoClient mongoService) {
String individualId = new ObjectId().toHexString();
JsonObject document = new JsonObject()
.put("name", "Stephen Hawking")
.put("individualId", new JsonObject().put("$oid", individualId));
mongoService.save("smartPeople", document, res -> {
if (res.succeeded()) {
String id = res.result();
JsonObject query = new JsonObject().put("_id", id);
mongoService.findOne("smartPeople", query, null, res2 -> {
if (res2.succeeded()) {
String reconstitutedIndividualId = res2.result()
.getJsonObject("individualId").getString("$oid");
} else {
res2.cause().printStackTrace();
}
});
} else {
res.cause().printStackTrace();
}
});
}
示例15: saveWithOptions
import io.vertx.ext.mongo.MongoClient; //导入依赖的package包/类
@Override
public io.vertx.ext.mongo.MongoClient saveWithOptions(String collection, JsonObject document, @Nullable WriteOption writeOption, Handler<AsyncResult<String>> resultHandler) {
requireNonNull(collection, "collection cannot be null");
requireNonNull(document, "document cannot be null");
requireNonNull(resultHandler, "resultHandler cannot be null");
MongoCollection<JsonObject> coll = getCollection(collection, writeOption);
Object id = document.getValue(ID_FIELD);
if (id == null) {
coll.insertOne(document, convertCallback(resultHandler, wr -> useObjectId ? document.getJsonObject(ID_FIELD).getString(JsonObjectCodec.OID_FIELD) : document.getString(ID_FIELD)));
} else {
JsonObject filter = new JsonObject();
JsonObject encodedDocument = encodeKeyWhenUseObjectId(document);
filter.put(ID_FIELD, encodedDocument.getValue(ID_FIELD));
com.mongodb.client.model.UpdateOptions updateOptions = new com.mongodb.client.model.UpdateOptions()
.upsert(true);
coll.replaceOne(wrap(filter), encodedDocument, updateOptions, convertCallback(resultHandler, result -> null));
}
return this;
}