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

com.mongodb.hadoop.pig.udf.ToObjectId 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.

The newest version!
package com.mongodb.hadoop.pig.udf;

import com.mongodb.hadoop.pig.udf.types.PigBoxedObjectId;
import org.apache.pig.data.DataByteArray;
import org.apache.pig.data.Tuple;
import org.bson.types.ObjectId;

import java.io.IOException;

/**
 * UDF that transforms the incoming value into a BSON ObjectId.
 */
public class ToObjectId extends ByteArrayTypeEvalFunc {
    public PigBoxedObjectId exec(final Tuple input) throws IOException {
        if (null == input || input.size() == 0) {
            return null;
        }
        Object o = input.get(0);
        if (o instanceof String) {
            return new PigBoxedObjectId(
              new ObjectId((String) o).toByteArray());
        } else if (o instanceof DataByteArray) {
            return new PigBoxedObjectId(((DataByteArray) o).get());
        }
        throw new IOException(
          "Need a String or DataByteArray to build an ObjectId, not " + o);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy