Search in sources :

Example 1 with BSONDecoder

use of org.bson.BSONDecoder in project mongo-hadoop by mongodb.

the class BSONWritable method readFields.

/**
     * {@inheritDoc}
     *
     * @see Writable#readFields(DataInput)
     */
public void readFields(final DataInput in) throws IOException {
    BSONDecoder dec = new BasicBSONDecoder();
    BSONCallback cb = new BasicBSONCallback();
    // Read the BSON length from the start of the record
    byte[] l = new byte[4];
    try {
        in.readFully(l);
        int dataLen = Bits.readInt(l);
        if (LOG.isDebugEnabled()) {
            LOG.debug("*** Expected DataLen: " + dataLen);
        }
        byte[] data = new byte[dataLen + 4];
        System.arraycopy(l, 0, data, 0, 4);
        in.readFully(data, 4, dataLen - 4);
        dec.decode(data, cb);
        doc = (BSONObject) cb.get();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Decoded a BSON Object: " + doc);
        }
    } catch (Exception e) {
        /* If we can't read another length it's not an error, just return quietly. */
        // TODO - Figure out how to gracefully mark this as an empty
        LOG.info("No Length Header available." + e);
        doc = new BasicDBObject();
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONCallback(org.bson.BasicBSONCallback) BasicBSONDecoder(org.bson.BasicBSONDecoder) BSONDecoder(org.bson.BSONDecoder) BSONCallback(org.bson.BSONCallback) BasicBSONCallback(org.bson.BasicBSONCallback) BasicBSONDecoder(org.bson.BasicBSONDecoder) IOException(java.io.IOException)

Example 2 with BSONDecoder

use of org.bson.BSONDecoder in project mongo-hadoop by mongodb.

the class BSONSplitterTest method testReadSplitsForFile.

@Test
public void testReadSplitsForFile() throws IOException {
    Configuration readSplitsConfig = new Configuration(conf);
    SPLITTER.setConf(readSplitsConfig);
    // Only one split if reading splits is disabled.
    MongoConfigUtil.setBSONReadSplits(readSplitsConfig, false);
    SPLITTER.readSplitsForFile(file);
    List<BSONFileSplit> splitsList = SPLITTER.getAllSplits();
    assertEquals(1, splitsList.size());
    BSONFileSplit theSplit = splitsList.get(0);
    assertOneSplit(theSplit);
    // Actually compute splits.
    MongoConfigUtil.setBSONReadSplits(readSplitsConfig, true);
    // Set split size to be really small so we get a lot of them.
    readSplitsConfig.set("mapreduce.input.fileinputformat.split.maxsize", "5000");
    SPLITTER.readSplitsForFile(file);
    splitsList = SPLITTER.getAllSplits();
    // Value found through manual inspection.
    assertEquals(40, splitsList.size());
    // Make sure that all splits start on document boundaries.
    FSDataInputStream stream = fs.open(file.getPath());
    BSONDecoder decoder = new BasicBSONDecoder();
    BSONCallback callback = new BasicBSONCallback();
    for (BSONFileSplit split : splitsList) {
        stream.seek(split.getStart());
        decoder.decode(stream, callback);
        BSONObject doc = (BSONObject) callback.get();
        assertTrue(doc.containsField("_id"));
    }
}
Also used : BasicBSONCallback(org.bson.BasicBSONCallback) BasicBSONDecoder(org.bson.BasicBSONDecoder) BSONDecoder(org.bson.BSONDecoder) Configuration(org.apache.hadoop.conf.Configuration) BSONFileSplit(com.mongodb.hadoop.input.BSONFileSplit) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) BSONCallback(org.bson.BSONCallback) BasicBSONCallback(org.bson.BasicBSONCallback) BasicBSONDecoder(org.bson.BasicBSONDecoder) Test(org.junit.Test)

Example 3 with BSONDecoder

use of org.bson.BSONDecoder in project mongo-hadoop by mongodb.

the class MongoUpdateWritable method readFields.

/**
     * {@inheritDoc}
     *
     * @see Writable#readFields(DataInput)
     */
public void readFields(final DataInput in) throws IOException {
    BSONDecoder dec = new BasicBSONDecoder();
    BSONCallback cb = new BasicBSONCallback();
    // Read the BSON length from the start of the record
    byte[] l = new byte[4];
    try {
        in.readFully(l);
        int dataLen = Bits.readInt(l);
        byte[] data = new byte[dataLen + 4];
        System.arraycopy(l, 0, data, 0, 4);
        in.readFully(data, 4, dataLen - 4);
        dec.decode(data, cb);
        query = (BasicBSONObject) cb.get();
        in.readFully(l);
        dataLen = Bits.readInt(l);
        data = new byte[dataLen + 4];
        System.arraycopy(l, 0, data, 0, 4);
        in.readFully(data, 4, dataLen - 4);
        dec.decode(data, cb);
        modifiers = (BasicBSONObject) cb.get();
        upsert = in.readBoolean();
        multiUpdate = in.readBoolean();
        replace = in.readBoolean();
    } catch (Exception e) {
        /* If we can't read another length it's not an error, just return quietly. */
        // TODO - Figure out how to gracefully mark this as an empty
        LOG.info("No Length Header available." + e);
        query = new BasicDBObject();
        modifiers = new BasicDBObject();
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONCallback(org.bson.BasicBSONCallback) BasicBSONDecoder(org.bson.BasicBSONDecoder) BSONDecoder(org.bson.BSONDecoder) BSONCallback(org.bson.BSONCallback) BasicBSONCallback(org.bson.BasicBSONCallback) BasicBSONDecoder(org.bson.BasicBSONDecoder) IOException(java.io.IOException)

Example 4 with BSONDecoder

use of org.bson.BSONDecoder in project graylog2-server by Graylog2.

the class BsonReader method readBsonFile.

protected List<DBObject> readBsonFile(String filename) {
    Path filePath = Paths.get(filename);
    List<DBObject> dataset = new ArrayList<>();
    try {
        ByteArrayInputStream fileBytes = new ByteArrayInputStream(Files.readAllBytes(filePath));
        BSONDecoder decoder = new BasicBSONDecoder();
        BSONObject obj;
        while ((obj = decoder.readObject(fileBytes)) != null) {
            final DBObject mongoDocument = new BasicDBObject(obj.toMap());
            dataset.add(mongoDocument);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw new RuntimeException("Can not open BSON input file.", e);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        throw new RuntimeException("Can not parse BSON data.", e);
    } catch (IOException e) {
    //EOF
    }
    return dataset;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) BSONObject(org.bson.BSONObject) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicBSONDecoder(org.bson.BasicBSONDecoder) BSONDecoder(org.bson.BSONDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BasicBSONDecoder(org.bson.BasicBSONDecoder)

Aggregations

BSONDecoder (org.bson.BSONDecoder)4 BasicBSONDecoder (org.bson.BasicBSONDecoder)4 BasicDBObject (com.mongodb.BasicDBObject)3 IOException (java.io.IOException)3 BSONCallback (org.bson.BSONCallback)3 BasicBSONCallback (org.bson.BasicBSONCallback)3 BSONObject (org.bson.BSONObject)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DBObject (com.mongodb.DBObject)1 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Configuration (org.apache.hadoop.conf.Configuration)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1 BasicBSONObject (org.bson.BasicBSONObject)1 Test (org.junit.Test)1