Search in sources :

Example 41 with MongoCollection

use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.

the class MongoDbRepository method updateMyPet.

@SuppressWarnings("unchecked")
private boolean updateMyPet(StoredMyPet storedMyPet) {
    MongoCollection petCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
    Document filter = new Document("uuid", storedMyPet.getUUID().toString());
    Document petDocument = (Document) petCollection.find(filter).first();
    if (petDocument == null) {
        return false;
    }
    petDocument.append("uuid", storedMyPet.getUUID().toString());
    petDocument.append("owner_uuid", storedMyPet.getOwner().getInternalUUID().toString());
    petDocument.append("exp", storedMyPet.getExp());
    petDocument.append("health", storedMyPet.getHealth());
    petDocument.append("respawn_time", storedMyPet.getRespawnTime());
    petDocument.append("name", storedMyPet.getPetName());
    petDocument.append("type", storedMyPet.getPetType().name());
    petDocument.append("last_used", storedMyPet.getLastUsed());
    petDocument.append("hunger", storedMyPet.getSaturation());
    petDocument.append("world_group", storedMyPet.getWorldGroup());
    petDocument.append("wants_to_spawn", storedMyPet.wantsToRespawn());
    petDocument.append("skilltree", storedMyPet.getSkilltree() != null ? storedMyPet.getSkilltree().getName() : null);
    try {
        petDocument.append("skills", TagStream.writeTag(storedMyPet.getSkillInfo(), true));
        petDocument.append("info", TagStream.writeTag(storedMyPet.getInfo(), true));
    } catch (IOException e) {
        e.printStackTrace();
    }
    petCollection.replaceOne(filter, petDocument);
    return true;
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) IOException(java.io.IOException) Document(org.bson.Document)

Example 42 with MongoCollection

use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.

the class MongoDbRepository method updateToV3.

private void updateToV3() {
    MongoCollection playerCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
    playerCollection.dropIndex(new BasicDBObject("offline_uuid", 1));
    playerCollection.createIndex(new BasicDBObject("name", 1));
}
Also used : MongoCollection(com.mongodb.client.MongoCollection)

Example 43 with MongoCollection

use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.

the class MongoDbRepository method init.

@Override
@SuppressWarnings("unchecked")
public void init() throws RepositoryInitException {
    connect();
    if (!collectionExists(Configuration.Repository.MongoDB.PREFIX + "info")) {
        initStructure();
    } else {
        MongoCollection infoCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "info");
        Document info = (Document) infoCollection.find().first();
        updateStructure(info.getInteger("version"));
    }
    updateInfo();
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) Document(org.bson.Document)

Example 44 with MongoCollection

use of com.mongodb.client.MongoCollection in project beam by apache.

the class MongoDbReadWriteIT method testProjectPushDown.

@Test
public void testProjectPushDown() {
    final Schema expectedSchema = Schema.builder().addNullableField("c_varchar", STRING).addNullableField("c_boolean", BOOLEAN).addNullableField("c_integer", INT32).build();
    Row testRow = row(expectedSchema, "varchar", true, 2147483647);
    String createTableStatement = "CREATE EXTERNAL TABLE TEST( \n" + "   c_bigint BIGINT, \n " + "   c_tinyint TINYINT, \n" + "   c_smallint SMALLINT, \n" + "   c_integer INTEGER, \n" + "   c_float FLOAT, \n" + "   c_double DOUBLE, \n" + "   c_boolean BOOLEAN, \n" + "   c_varchar VARCHAR, \n " + "   c_arr ARRAY<VARCHAR> \n" + ") \n" + "TYPE 'mongodb' \n" + "LOCATION '" + mongoSqlUrl + "'";
    sqlEnv.executeDdl(createTableStatement);
    String insertStatement = "INSERT INTO TEST VALUES (" + "9223372036854775807, " + "127, " + "32767, " + "2147483647, " + "1.0, " + "1.0, " + "TRUE, " + "'varchar', " + "ARRAY['123', '456']" + ")";
    BeamRelNode insertRelNode = sqlEnv.parseQuery(insertStatement);
    BeamSqlRelUtils.toPCollection(writePipeline, insertRelNode);
    writePipeline.run().waitUntilFinish();
    BeamRelNode node = sqlEnv.parseQuery("select c_varchar, c_boolean, c_integer from TEST");
    // Calc should be dropped, since MongoDb supports project push-down and field reordering.
    assertThat(node, instanceOf(BeamPushDownIOSourceRel.class));
    // Only selected fields are projected.
    assertThat(node.getRowType().getFieldNames(), containsInAnyOrder("c_varchar", "c_boolean", "c_integer"));
    PCollection<Row> output = BeamSqlRelUtils.toPCollection(readPipeline, node);
    assertThat(output.getSchema(), equalTo(expectedSchema));
    PAssert.that(output).containsInAnyOrder(testRow);
    readPipeline.run().waitUntilFinish();
    MongoDatabase db = client.getDatabase(database);
    MongoCollection coll = db.getCollection("system.profile");
    // Find the last executed query.
    Object query = coll.find().filter(Filters.eq("op", "query")).sort(new BasicDBObject().append("ts", -1)).iterator().next();
    // Retrieve a projection parameters.
    assertThat(query, instanceOf(Document.class));
    Object command = ((Document) query).get("command");
    assertThat(command, instanceOf(Document.class));
    Object projection = ((Document) command).get("projection");
    assertThat(projection, instanceOf(Document.class));
    // Validate projected fields.
    assertThat(((Document) projection).keySet(), containsInAnyOrder("c_varchar", "c_boolean", "c_integer"));
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) BeamPushDownIOSourceRel(org.apache.beam.sdk.extensions.sql.impl.rel.BeamPushDownIOSourceRel) BasicDBObject(com.mongodb.BasicDBObject) MongoCollection(com.mongodb.client.MongoCollection) Schema(org.apache.beam.sdk.schemas.Schema) BasicDBObject(com.mongodb.BasicDBObject) Row(org.apache.beam.sdk.values.Row) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 45 with MongoCollection

use of com.mongodb.client.MongoCollection in project beam by apache.

the class MongoDbReadWriteIT method testPredicatePushDown.

@Test
public void testPredicatePushDown() {
    final Document expectedFilter = new Document().append("$or", ImmutableList.of(new Document("c_varchar", "varchar"), new Document("c_varchar", new Document("$not", new Document("$eq", "fakeString")))).asList()).append("c_boolean", true).append("c_integer", 2147483647);
    final Schema expectedSchema = Schema.builder().addNullableField("c_varchar", STRING).addNullableField("c_boolean", BOOLEAN).addNullableField("c_integer", INT32).build();
    Row testRow = row(expectedSchema, "varchar", true, 2147483647);
    String createTableStatement = "CREATE EXTERNAL TABLE TEST( \n" + "   c_bigint BIGINT, \n " + "   c_tinyint TINYINT, \n" + "   c_smallint SMALLINT, \n" + "   c_integer INTEGER, \n" + "   c_float FLOAT, \n" + "   c_double DOUBLE, \n" + "   c_boolean BOOLEAN, \n" + "   c_varchar VARCHAR, \n " + "   c_arr ARRAY<VARCHAR> \n" + ") \n" + "TYPE 'mongodb' \n" + "LOCATION '" + mongoSqlUrl + "'";
    sqlEnv.executeDdl(createTableStatement);
    String insertStatement = "INSERT INTO TEST VALUES (" + "9223372036854775807, " + "127, " + "32767, " + "2147483647, " + "1.0, " + "1.0, " + "TRUE, " + "'varchar', " + "ARRAY['123', '456']" + ")";
    BeamRelNode insertRelNode = sqlEnv.parseQuery(insertStatement);
    BeamSqlRelUtils.toPCollection(writePipeline, insertRelNode);
    writePipeline.run().waitUntilFinish();
    BeamRelNode node = sqlEnv.parseQuery("select c_varchar, c_boolean, c_integer from TEST" + " where (c_varchar='varchar' or c_varchar<>'fakeString') and c_boolean and c_integer=2147483647");
    // Calc should be dropped, since MongoDb can push-down all predicate operations from a query
    // above.
    assertThat(node, instanceOf(BeamPushDownIOSourceRel.class));
    // Only selected fields are projected.
    assertThat(node.getRowType().getFieldNames(), containsInAnyOrder("c_varchar", "c_boolean", "c_integer"));
    PCollection<Row> output = BeamSqlRelUtils.toPCollection(readPipeline, node);
    assertThat(output.getSchema(), equalTo(expectedSchema));
    PAssert.that(output).containsInAnyOrder(testRow);
    readPipeline.run().waitUntilFinish();
    MongoDatabase db = client.getDatabase(database);
    MongoCollection coll = db.getCollection("system.profile");
    // Find the last executed query.
    Object query = coll.find().filter(Filters.eq("op", "query")).sort(new BasicDBObject().append("ts", -1)).iterator().next();
    // Retrieve a projection parameters.
    assertThat(query, instanceOf(Document.class));
    Object command = ((Document) query).get("command");
    assertThat(command, instanceOf(Document.class));
    Object filter = ((Document) command).get("filter");
    assertThat(filter, instanceOf(Document.class));
    Object projection = ((Document) command).get("projection");
    assertThat(projection, instanceOf(Document.class));
    // Validate projected fields.
    assertThat(((Document) projection).keySet(), containsInAnyOrder("c_varchar", "c_boolean", "c_integer"));
    // Validate filtered fields.
    assertThat(((Document) filter), equalTo(expectedFilter));
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) BeamPushDownIOSourceRel(org.apache.beam.sdk.extensions.sql.impl.rel.BeamPushDownIOSourceRel) BasicDBObject(com.mongodb.BasicDBObject) MongoCollection(com.mongodb.client.MongoCollection) Schema(org.apache.beam.sdk.schemas.Schema) BasicDBObject(com.mongodb.BasicDBObject) Row(org.apache.beam.sdk.values.Row) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Aggregations

MongoCollection (com.mongodb.client.MongoCollection)53 Document (org.bson.Document)39 MongoDatabase (com.mongodb.client.MongoDatabase)21 List (java.util.List)16 FindIterable (com.mongodb.client.FindIterable)14 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)11 BasicDBObject (com.mongodb.BasicDBObject)10 Set (java.util.Set)9 ObjectId (org.bson.types.ObjectId)9 MongoCursor (com.mongodb.client.MongoCursor)7 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Bson (org.bson.conversions.Bson)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 BsonDocument (org.bson.BsonDocument)6 Block (com.mongodb.Block)5 UpdateOptions (com.mongodb.client.model.UpdateOptions)5