All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mongodb.hadoop.streaming.io.MongoUpdateInputWriter Maven / Gradle / Ivy

Go to download

The MongoDB Connector for Hadoop is a plugin for Hadoop that provides the ability to use MongoDB as an input source and/or an output destination.

There is a newer version: 2.0.2
Show newest version
package com.mongodb.hadoop.streaming.io;

import com.mongodb.hadoop.io.BSONWritable;
import com.mongodb.hadoop.io.MongoUpdateWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.streaming.PipeMapRed;
import org.apache.hadoop.streaming.io.InputWriter;

import java.io.DataOutput;
import java.io.IOException;

/**
 * InputWriter capable of handling both BSONWritable and MongoUpdateWritable
 * as value types.
 */
public class MongoUpdateInputWriter extends InputWriter {

    private DataOutput output;
    private final BSONWritable bsonWritable = new BSONWritable();

    @Override
    public void initialize(final PipeMapRed pipeMapRed) throws IOException {
        super.initialize(pipeMapRed);
        output = pipeMapRed.getClientOutput();
    }

    @Override
    public void writeKey(final Object key) throws IOException {
        // Nothing to do.
    }

    @Override
    public void writeValue(final Writable value) throws IOException {
        if (value instanceof MongoUpdateWritable) {
            // If we're writing to the input of a streaming script, just send
            // back the "query" portion of the MongoUpdateWritable, so that
            // mapper and reducer scripts can operate on a single document.
            bsonWritable.setDoc(((MongoUpdateWritable) value).getQuery());
            bsonWritable.write(output);
        } else if (value instanceof BSONWritable) {
            value.write(output);
        } else {
            throw new IOException("Unexpected Writable type :" + value);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy