Search in sources :

Example 6 with JsonReader

use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.

the class MessageHelper method encodeJson.

private static ByteBuf encodeJson(final String json) {
    OutputBuffer outputBuffer = new BasicOutputBuffer();
    JsonReader jsonReader = new JsonReader(json);
    BsonDocumentCodec codec = new BsonDocumentCodec();
    BsonDocument document = codec.decode(jsonReader, DecoderContext.builder().build());
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    codec.encode(writer, document, EncoderContext.builder().build());
    ByteBuffer documentByteBuffer = ByteBuffer.allocate(outputBuffer.size());
    documentByteBuffer.put(outputBuffer.toByteArray());
    return new ByteBufNIO(documentByteBuffer);
}
Also used : BsonDocument(org.bson.BsonDocument) JsonReader(org.bson.json.JsonReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) ByteBuffer(java.nio.ByteBuffer) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 7 with JsonReader

use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.

the class Document method parse.

/**
 * Parses a string in MongoDB Extended JSON format to a {@code Document}
 *
 * @param json the JSON string
 * @param decoder the {@code Decoder} to use to parse the JSON string into a {@code Document}
 * @return a corresponding {@code Document} object
 * @see org.bson.json.JsonReader
 * @mongodb.driver.manual reference/mongodb-extended-json/ MongoDB Extended JSON
 */
public static Document parse(final String json, final Decoder<Document> decoder) {
    notNull("codec", decoder);
    JsonReader bsonReader = new JsonReader(json);
    return decoder.decode(bsonReader, DecoderContext.builder().build());
}
Also used : JsonReader(org.bson.json.JsonReader)

Example 8 with JsonReader

use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.

the class MultiFileImportBenchmark method importJsonFile.

private Runnable importJsonFile(final CountDownLatch latch, final int fileId) {
    final RawBsonDocumentCodec codec = new RawBsonDocumentCodec();
    return new Runnable() {

        @Override
        public void run() {
            try {
                String resourcePath = "parallel/ldjson_multi/ldjson" + String.format("%03d", fileId) + ".txt";
                BufferedReader reader = new BufferedReader(readFromRelativePath(resourcePath), 1024 * 64);
                try {
                    String json;
                    List<RawBsonDocument> documents = new ArrayList<RawBsonDocument>(1000);
                    while ((json = reader.readLine()) != null) {
                        RawBsonDocument document = codec.decode(new JsonReader(json), DecoderContext.builder().build());
                        documents.add(document);
                        if (documents.size() == 1000) {
                            final List<RawBsonDocument> documentsToInsert = documents;
                            documentWritingService.submit(new Runnable() {

                                @Override
                                public void run() {
                                    collection.insertMany(documentsToInsert, new InsertManyOptions().ordered(false));
                                    latch.countDown();
                                }
                            });
                            documents = new ArrayList<RawBsonDocument>(1000);
                        }
                    }
                    if (!documents.isEmpty()) {
                        throw new IllegalStateException("Document count not a multiple of 1000");
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    reader.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}
Also used : RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) ArrayList(java.util.ArrayList) RawBsonDocument(org.bson.RawBsonDocument) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) JsonReader(org.bson.json.JsonReader) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 9 with JsonReader

use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.

the class MultiFileExportBenchmark method importJsonFiles.

private void importJsonFiles() throws InterruptedException {
    ExecutorService importService = Executors.newFixedThreadPool(FILE_READING_THREAD_POOL_SIZE);
    final CountDownLatch latch = new CountDownLatch(100);
    for (int i = 0; i < 100; i++) {
        final int fileId = i;
        importService.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    String resourcePath = "parallel/ldjson_multi/ldjson" + String.format("%03d", fileId) + ".txt";
                    BufferedReader reader = new BufferedReader(readFromRelativePath(resourcePath), 1024 * 64);
                    try {
                        String json;
                        List<BsonDocument> documents = new ArrayList<BsonDocument>(1000);
                        while ((json = reader.readLine()) != null) {
                            BsonDocument document = new BsonDocumentCodec().decode(new JsonReader(json), DecoderContext.builder().build());
                            document.put("fileId", new BsonInt32(fileId));
                            documents.add(document);
                        }
                        database.getCollection(COLLECTION_NAME, BsonDocument.class).insertMany(documents, new InsertManyOptions().ordered(false));
                        latch.countDown();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        reader.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    latch.await(1, TimeUnit.MINUTES);
    collection.createIndex(new BsonDocument("fileId", new BsonInt32(1)));
    importService.shutdown();
    importService.awaitTermination(1, TimeUnit.MINUTES);
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) RawBsonDocument(org.bson.RawBsonDocument) ExecutorService(java.util.concurrent.ExecutorService) BufferedReader(java.io.BufferedReader) JsonReader(org.bson.json.JsonReader) ArrayList(java.util.ArrayList) List(java.util.List) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 10 with JsonReader

use of org.bson.json.JsonReader in project mongo-java-driver by mongodb.

the class AbstractFindBenchmark method setUp.

public void setUp() throws Exception {
    super.setUp();
    collection = client.getDatabase(DATABASE_NAME).getCollection(COLLECTION_NAME, clazz);
    byte[] bytes = readAllBytesFromRelativePath(resourcePath);
    fileLength = bytes.length;
    MongoDatabase setUpDatabase = client.getDatabase(DATABASE_NAME);
    setUpDatabase.drop();
    insertCopiesOfDocument(setUpDatabase.getCollection(COLLECTION_NAME, BsonDocument.class), new BsonDocumentCodec().decode(new JsonReader(new String(bytes, StandardCharsets.UTF_8)), DecoderContext.builder().build()));
}
Also used : BsonDocument(org.bson.BsonDocument) JsonReader(org.bson.json.JsonReader) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

JsonReader (org.bson.json.JsonReader)18 BsonDocument (org.bson.BsonDocument)5 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)5 BsonReader (org.bson.BsonReader)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 MongoDatabase (com.mongodb.client.MongoDatabase)2 InsertManyOptions (com.mongodb.client.model.InsertManyOptions)2 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Converter (org.apache.camel.Converter)2 IOConverter (org.apache.camel.converter.IOConverter)2 BsonArray (org.bson.BsonArray)2 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 ByteBufNIO (org.bson.ByteBufNIO)2 Document (org.bson.Document)2 RawBsonDocument (org.bson.RawBsonDocument)2 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)2 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)2