Search in sources :

Example 16 with SerializationFactory

use of org.apache.hadoop.io.serializer.SerializationFactory in project hadoop by apache.

the class ReflectionUtils method copy.

/**
   * Make a copy of the writable object using serialization to a buffer
   * @param src the object to copy from
   * @param dst the object to copy into, which is destroyed
   * @return dst param (the copy)
   * @throws IOException
   */
@SuppressWarnings("unchecked")
public static <T> T copy(Configuration conf, T src, T dst) throws IOException {
    CopyInCopyOutBuffer buffer = CLONE_BUFFERS.get();
    buffer.outBuffer.reset();
    SerializationFactory factory = getFactory(conf);
    Class<T> cls = (Class<T>) src.getClass();
    Serializer<T> serializer = factory.getSerializer(cls);
    serializer.open(buffer.outBuffer);
    serializer.serialize(src);
    buffer.moveData();
    Deserializer<T> deserializer = factory.getDeserializer(cls);
    deserializer.open(buffer.inBuffer);
    dst = deserializer.deserialize(dst);
    return dst;
}
Also used : SerializationFactory(org.apache.hadoop.io.serializer.SerializationFactory)

Example 17 with SerializationFactory

use of org.apache.hadoop.io.serializer.SerializationFactory in project hadoop by apache.

the class JobSplitWriter method writeNewSplits.

@SuppressWarnings("unchecked")
private static <T extends InputSplit> SplitMetaInfo[] writeNewSplits(Configuration conf, T[] array, FSDataOutputStream out) throws IOException, InterruptedException {
    SplitMetaInfo[] info = new SplitMetaInfo[array.length];
    if (array.length != 0) {
        SerializationFactory factory = new SerializationFactory(conf);
        int i = 0;
        int maxBlockLocations = conf.getInt(MRConfig.MAX_BLOCK_LOCATIONS_KEY, MRConfig.MAX_BLOCK_LOCATIONS_DEFAULT);
        long offset = out.getPos();
        for (T split : array) {
            long prevCount = out.getPos();
            Text.writeString(out, split.getClass().getName());
            Serializer<T> serializer = factory.getSerializer((Class<T>) split.getClass());
            serializer.open(out);
            serializer.serialize(split);
            long currCount = out.getPos();
            String[] locations = split.getLocations();
            if (locations.length > maxBlockLocations) {
                LOG.warn("Max block location exceeded for split: " + split + " splitsize: " + locations.length + " maxsize: " + maxBlockLocations);
                locations = Arrays.copyOf(locations, maxBlockLocations);
            }
            info[i++] = new JobSplit.SplitMetaInfo(locations, offset, split.getLength());
            offset += currCount - prevCount;
        }
    }
    return info;
}
Also used : SplitMetaInfo(org.apache.hadoop.mapreduce.split.JobSplit.SplitMetaInfo) SplitMetaInfo(org.apache.hadoop.mapreduce.split.JobSplit.SplitMetaInfo) SerializationFactory(org.apache.hadoop.io.serializer.SerializationFactory)

Example 18 with SerializationFactory

use of org.apache.hadoop.io.serializer.SerializationFactory in project gora by apache.

the class IOUtils method serialize.

/**
   * Serializes the object to the given data output using
   * available Hadoop serializations.
   *
   * @param conf Hadoop conf.
   * @param obj object instance to be serialized.
   * @param out data stream which serialized content is written.
   * @param objClass Class type of the object to be serialized.
   * @param <T> class type of object to be serialized.
   * @throws IOException occurred while serializing the object to bytes.
   */
public static <T> void serialize(Configuration conf, DataOutput out, T obj, Class<T> objClass) throws IOException {
    SerializationFactory serializationFactory = new SerializationFactory(getOrCreateConf(conf));
    Serializer<T> serializer = serializationFactory.getSerializer(objClass);
    try (ByteBufferOutputStream os = new ByteBufferOutputStream()) {
        serializer.open(os);
        serializer.serialize(obj);
        int length = 0;
        List<ByteBuffer> buffers = os.getBufferList();
        for (ByteBuffer buffer : buffers) {
            length += buffer.limit() - buffer.arrayOffset();
        }
        WritableUtils.writeVInt(out, length);
        for (ByteBuffer buffer : buffers) {
            byte[] arr = buffer.array();
            out.write(arr, buffer.arrayOffset(), buffer.limit());
        }
    } finally {
        if (serializer != null)
            serializer.close();
    }
}
Also used : ByteBufferOutputStream(org.apache.avro.util.ByteBufferOutputStream) SerializationFactory(org.apache.hadoop.io.serializer.SerializationFactory) ByteBuffer(java.nio.ByteBuffer)

Example 19 with SerializationFactory

use of org.apache.hadoop.io.serializer.SerializationFactory in project ignite by apache.

the class HadoopV2TaskContext method readExternalSplit.

/**
     * @param split External split.
     * @return Native input split.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
private Object readExternalSplit(HadoopExternalSplit split) throws IgniteCheckedException {
    Path jobDir = new Path(jobConf().get(MRJobConfig.MAPREDUCE_JOB_DIR));
    FileSystem fs;
    try {
        fs = fileSystemForMrUserWithCaching(jobDir.toUri(), jobConf(), fsMap);
    } catch (IOException e) {
        throw new IgniteCheckedException(e);
    }
    try (FSDataInputStream in = fs.open(JobSubmissionFiles.getJobSplitFile(jobDir))) {
        in.seek(split.offset());
        String clsName = Text.readString(in);
        Class<?> cls = jobConf().getClassByName(clsName);
        assert cls != null;
        Serialization serialization = new SerializationFactory(jobConf()).getSerialization(cls);
        Deserializer deserializer = serialization.getDeserializer(cls);
        deserializer.open(in);
        Object res = deserializer.deserialize(null);
        deserializer.close();
        assert res != null;
        return res;
    } catch (IOException | ClassNotFoundException e) {
        throw new IgniteCheckedException(e);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) SerializationFactory(org.apache.hadoop.io.serializer.SerializationFactory) IOException(java.io.IOException) WritableSerialization(org.apache.hadoop.io.serializer.WritableSerialization) HadoopSerialization(org.apache.ignite.internal.processors.hadoop.HadoopSerialization) Serialization(org.apache.hadoop.io.serializer.Serialization) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Deserializer(org.apache.hadoop.io.serializer.Deserializer) FileSystem(org.apache.hadoop.fs.FileSystem) LocalFileSystem(org.apache.hadoop.fs.LocalFileSystem) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream)

Example 20 with SerializationFactory

use of org.apache.hadoop.io.serializer.SerializationFactory in project ignite by apache.

the class HadoopV2TaskContext method getSerialization.

/**
     * Gets serializer for specified class.
     *
     * @param cls Class.
     * @param jobConf Job configuration.
     * @return Appropriate serializer.
     */
@SuppressWarnings("unchecked")
private HadoopSerialization getSerialization(Class<?> cls, Configuration jobConf) throws IgniteCheckedException {
    A.notNull(cls, "cls");
    SerializationFactory factory = new SerializationFactory(jobConf);
    Serialization<?> serialization = factory.getSerialization(cls);
    if (serialization == null)
        throw new IgniteCheckedException("Failed to find serialization for: " + cls.getName());
    if (serialization.getClass() == WritableSerialization.class)
        return new HadoopWritableSerialization((Class<? extends Writable>) cls);
    return new HadoopSerializationWrapper(serialization, cls);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SerializationFactory(org.apache.hadoop.io.serializer.SerializationFactory) Writable(org.apache.hadoop.io.Writable) ByteWritable(org.apache.hadoop.io.ByteWritable)

Aggregations

SerializationFactory (org.apache.hadoop.io.serializer.SerializationFactory)20 Deserializer (org.apache.hadoop.io.serializer.Deserializer)5 IOException (java.io.IOException)4 Serializer (org.apache.hadoop.io.serializer.Serializer)4 ByteBuffer (java.nio.ByteBuffer)2 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 LocalFileSystem (org.apache.hadoop.fs.LocalFileSystem)2 DataInputBuffer (org.apache.hadoop.io.DataInputBuffer)2 DataOutputBuffer (org.apache.hadoop.io.DataOutputBuffer)2 JobConf (org.apache.hadoop.mapred.JobConf)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2 JobContext (org.apache.hadoop.mapreduce.JobContext)2 JobID (org.apache.hadoop.mapreduce.JobID)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ByteBufferInputStream (org.apache.avro.util.ByteBufferInputStream)1 ByteBufferOutputStream (org.apache.avro.util.ByteBufferOutputStream)1 Configuration (org.apache.hadoop.conf.Configuration)1