org.bson.UuidRepresentation Java Examples

The following examples show how to use org.bson.UuidRepresentation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JacksonRepoTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  final MongoDatabase database = context.database();
  this.collection = database.getCollection("jackson").withDocumentClass(BsonDocument.class);

  SimpleModule module = new SimpleModule(); // for our local serializers of Date and ObjectId
  module.addDeserializer(Date.class, new DateDeserializer());
  module.addSerializer(new DateSerializer());
  module.addDeserializer(ObjectId.class, new ObjectIdDeserializer());
  module.addSerializer(new ObjectIdSerializer());
  module.addDeserializer(UUID.class, new UUIDDeserializer(UuidRepresentation.JAVA_LEGACY));

  ObjectMapper mapper = new ObjectMapper()
      // to support bson types like: Document, BsonValue etc.
      .registerModule(JacksonCodecs.module(MongoClient.getDefaultCodecRegistry()))
      .registerModule(new GuavaModule())
      .registerModule(module);

  RepositorySetup setup = RepositorySetup.builder()
          .database(database)
          .codecRegistry(JacksonCodecs.registryFromMapper(mapper))
          .executor(MoreExecutors.newDirectExecutorService())
          .build();

  this.repository = new JacksonRepository(setup);
}
 
Example #2
Source File: UuidStrategy.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public BsonValue generateId(final SinkDocument doc, final SinkRecord orig) {
  UUID uuid = UUID.randomUUID();
  if (outputFormat.equals(UuidBsonFormat.STRING)) {
    return new BsonString(uuid.toString());
  }

  return new BsonBinary(uuid, UuidRepresentation.STANDARD);
}
 
Example #3
Source File: UuidProvidedStrategy.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public BsonValue generateId(final SinkDocument doc, final SinkRecord orig) {
  BsonValue id = super.generateId(doc, orig);

  if (id.isBinary() && BsonBinarySubType.isUuid(id.asBinary().getType())) {
    return id;
  } else if (id.isString()) {
    return new BsonBinary(
        constructUuidObjectFromString(id.asString().getValue()), UuidRepresentation.STANDARD);
  }

  throw new DataException(format("UUID cannot be constructed from provided value: `%s`", id));
}
 
Example #4
Source File: TestMongoClient.java    From jframe with Apache License 2.0 4 votes vote down vote up
public void testDriverStatus() {
		CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
				CodecRegistries.fromCodecs(new UuidCodec(
						UuidRepresentation.STANDARD)), MongoClient
						.getDefaultCodecRegistry());

		mongoClient.getDatabase("lech_rent").drop();
		MongoDatabase rent = mongoClient.getDatabase("lech_rent")
				.withCodecRegistry(codecRegistry);
		// rent.createCollection("driver_status", new CreateCollectionOptions()
		// .capped(true).sizeInBytes(0x100000));
		MongoCollection<Document> status = rent.getCollection("driver_status");
		status.deleteMany(Filters.eq("mobile", "18616020610"));
		if (status.count() == 0) {

		}
		status.createIndex(new Document("mobile", "text"));
		// status.createIndex(new Document("no", "text"));
		for (final Document index : status.listIndexes()) {
			System.out.println(index.toJson());
		}

		Document doc = new Document("loc",
				new Document("type", "Point").append("coordinates",
						Arrays.asList(-73.97, 40.77))).append("no", "dno")
				.append("usrImg", "/usr/driver.png")
				.append("mobile", "18616020610").append("status", 7)
				.append("car", new Document("no", "A00001"));
		status.insertOne(doc);
		// status.createIndex(keys);
		doc = status.find(Filters.eq("mobile", "18616020610")).first();

		System.out.println(doc.get("loc", Document.class).get("coordinates"));
		System.out.println(doc.get("loc", Document.class).get("coordinates",
				ArrayList.class));
		System.out.println(doc.get("car", Document.class));
		// System.out.println(doc.get("loc", Document.class));

//		UpdateResult updateResult = status.updateOne(Filters.eq("mobile",
//				"18616020610"), new Document("$set", new Document("car",
//				new Document("no", "A00002"))));
		doc = status.find(Filters.eq("mobile", "18616020610")).first();
		System.out.println(doc.get("car", Document.class));

		// updateResult = status.updateMany(Filters.lt("i", 100), new Document(
		// "$inc", new Document("i", 100)));
		// System.out.println(updateResult.getModifiedCount());
		// DeleteResult deleteResult = status.deleteOne(Filters.eq("i", 110));
		// System.out.println(deleteResult.getDeletedCount());

		// 2. Ordered bulk operation - order is guarenteed
		// status.bulkWrite(Arrays.asList(new InsertOneModel<>(new
		// Document("_id",
		// 4)), new InsertOneModel<>(new Document("_id", 5)),
		// new InsertOneModel<>(new Document("_id", 6)),
		// new UpdateOneModel<>(new Document("_id", 1), new Document(
		// "$set", new Document("x", 2))), new DeleteOneModel<>(
		// new Document("_id", 2)),
		// new ReplaceOneModel<>(new Document("_id", 3), new Document(
		// "_id", 3).append("x", 4))));

		// 2. Unordered bulk operation - no guarantee of order of operation
		// status.bulkWrite(Arrays.asList(new InsertOneModel<>(new
		// Document("_id",
		// 4)), new InsertOneModel<>(new Document("_id", 5)),
		// new InsertOneModel<>(new Document("_id", 6)),
		// new UpdateOneModel<>(new Document("_id", 1), new Document(
		// "$set", new Document("x", 2))), new DeleteOneModel<>(
		// new Document("_id", 2)),
		// new ReplaceOneModel<>(new Document("_id", 3), new Document(
		// "_id", 3).append("x", 4))), new BulkWriteOptions()
		// .ordered(false));

	}
 
Example #5
Source File: JacksonRepoTest.java    From immutables with Apache License 2.0 4 votes vote down vote up
private UUIDDeserializer(UuidRepresentation uuidRepresentation) {
  super(UUID.class);
  uuidCodec = new UuidCodec(uuidRepresentation);
}
 
Example #6
Source File: MapperOptions.java    From morphia with Apache License 2.0 4 votes vote down vote up
/**
 * @return the UUID representation to use in the driver
 */
public UuidRepresentation getUuidRepresentation() {
    return uuidRepresentation;
}