Search in sources :

Example 1 with BasicBSONDecoder

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

the class BSONFileRecordReader method init.

public void init(final InputSplit inputSplit, final Configuration configuration) throws IOException, InterruptedException {
    this.configuration = configuration;
    fileSplit = (FileSplit) inputSplit;
    if (LOG.isDebugEnabled()) {
        LOG.debug("reading split " + fileSplit);
    }
    Path file = fileSplit.getPath();
    FileSystem fs = file.getFileSystem(configuration);
    CompressionCodec codec = new CompressionCodecFactory(configuration).getCodec(fileSplit.getPath());
    inRaw = fs.open(file, 16 * 1024 * 1024);
    inRaw.seek(startingPosition == BSON_RR_POSITION_NOT_GIVEN ? fileSplit.getStart() : startingPosition);
    if (codec != null) {
        decompressor = CodecPool.getDecompressor(codec);
        in = codec.createInputStream(inRaw, decompressor);
    } else {
        in = inRaw;
    }
    if (MongoConfigUtil.getLazyBSON(configuration)) {
        callback = new LazyBSONCallback();
        decoder = new LazyBSONDecoder();
    } else {
        callback = new BasicBSONCallback();
        decoder = new BasicBSONDecoder();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) BasicBSONCallback(org.bson.BasicBSONCallback) CompressionCodecFactory(org.apache.hadoop.io.compress.CompressionCodecFactory) FileSystem(org.apache.hadoop.fs.FileSystem) LazyBSONCallback(org.bson.LazyBSONCallback) CompressionCodec(org.apache.hadoop.io.compress.CompressionCodec) LazyBSONDecoder(org.bson.LazyBSONDecoder) BasicBSONDecoder(org.bson.BasicBSONDecoder)

Example 2 with BasicBSONDecoder

use of org.bson.BasicBSONDecoder 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 3 with BasicBSONDecoder

use of org.bson.BasicBSONDecoder in project immutables by immutables.

the class BsonEncoding method unwrapBsonable.

/**
   * Although it may seem that re-parsing is bizarre, but it is one [of not so many] ways to do
   * proper marshaling. This kind of inefficiency will only hit query constraints that have many
   * object with custom marshaling, which considered to be a rare case.
   * @param adapted adapted value that know how to write itself to {@link JsonWriter}
   * @return object converted to MongoDB driver's {@link BSONObject}.
   */
public static Object unwrapBsonable(Support.Adapted<?> adapted) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BsonGenerator generator = BSON_FACTORY.createGenerator(outputStream);
        BsonWriter writer = new BsonWriter(generator);
        writer.beginObject().name(PREENCODED_VALUE_WRAPPER_FIELD_NAME);
        adapted.write(writer);
        writer.endObject();
        writer.close();
        BSONObject object = new BasicBSONDecoder().readObject(outputStream.toByteArray());
        return object.get(PREENCODED_VALUE_WRAPPER_FIELD_NAME);
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}
Also used : BSONObject(org.bson.BSONObject) BsonGenerator(de.undercouch.bson4jackson.BsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BasicBSONDecoder(org.bson.BasicBSONDecoder)

Example 4 with BasicBSONDecoder

use of org.bson.BasicBSONDecoder in project camel by apache.

the class MongoDbBasicConverters method fromInputStreamToDBObject.

@Converter
public static BasicDBObject fromInputStreamToDBObject(InputStream is, Exchange exchange) {
    BasicDBObject answer = null;
    try {
        byte[] input = IOConverter.toBytes(is);
        if (isBson(input)) {
            BSONCallback callback = new JSONCallback();
            new BasicBSONDecoder().decode(input, callback);
            answer = (BasicDBObject) callback.get();
        } else {
            answer = (BasicDBObject) JSON.parse(IOConverter.toString(input, exchange));
        }
    } catch (Exception e) {
        LOG.warn("String -> DBObject conversion selected, but the following exception occurred. Returning null.", e);
    } finally {
        // we need to make sure to close the input stream
        IOHelper.close(is, "InputStream", LOG);
    }
    return answer;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) JSONCallback(com.mongodb.util.JSONCallback) BSONCallback(org.bson.BSONCallback) BasicBSONDecoder(org.bson.BasicBSONDecoder) FileNotFoundException(java.io.FileNotFoundException) Converter(org.apache.camel.Converter) IOConverter(org.apache.camel.converter.IOConverter)

Example 5 with BasicBSONDecoder

use of org.bson.BasicBSONDecoder 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)

Aggregations

BasicBSONDecoder (org.bson.BasicBSONDecoder)7 BasicDBObject (com.mongodb.BasicDBObject)4 IOException (java.io.IOException)4 BSONCallback (org.bson.BSONCallback)4 BSONDecoder (org.bson.BSONDecoder)4 BasicBSONCallback (org.bson.BasicBSONCallback)4 BSONObject (org.bson.BSONObject)3 FileNotFoundException (java.io.FileNotFoundException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DBObject (com.mongodb.DBObject)1 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)1 JSONCallback (com.mongodb.util.JSONCallback)1 BsonGenerator (de.undercouch.bson4jackson.BsonGenerator)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Converter (org.apache.camel.Converter)1 IOConverter (org.apache.camel.converter.IOConverter)1 Configuration (org.apache.hadoop.conf.Configuration)1