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

co.cask.cdap.etl.batch.spark.Serializations Maven / Gradle / Ivy

/*
 * Copyright © 2015 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package co.cask.cdap.etl.batch.spark;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Helpers for serializing and deserializing collections.
 */
final class Serializations {

  static  void serializeMap(Map map, ObjectWriter valueWriter, DataOutput output) throws IOException {
    output.writeInt(map.size());
    for (Map.Entry entry : map.entrySet()) {
      output.writeUTF(entry.getKey());
      valueWriter.write(entry.getValue(), output);
    }
  }

  static  Map deserializeMap(DataInput input, ObjectReader valueReader) throws IOException {
    Map map = new HashMap<>();
    int size = input.readInt();
    for (int i = 0; i < size; i++) {
      map.put(input.readUTF(), valueReader.read(input));
    }
    return map;
  }

  static ObjectReader createStringObjectReader() {
    return new ObjectReader() {
      @Override
      public String read(DataInput input) throws IOException {
        return input.readUTF();
      }
    };
  }

  static ObjectWriter createStringObjectWriter() {
    return new ObjectWriter() {
      @Override
      public void write(String object, DataOutput output) throws IOException {
        output.writeUTF(object);
      }
    };
  }

  static ObjectReader> createStringSetObjectReader() {
    return new ObjectReader>() {
      @Override
      public Set read(DataInput input) throws IOException {
        int setSize = input.readInt();
        Set outputs = new HashSet<>();
        while (setSize > 0) {
          outputs.add(input.readUTF());
          setSize--;
        }
        return outputs;
      }
    };
  }

  static ObjectWriter> createStringSetObjectWriter() {
    return new ObjectWriter>() {
      @Override
      public void write(Set object, DataOutput output) throws IOException {
        output.writeInt(object.size());
        for (String val : object) {
          output.writeUTF(val);
        }
      }
    };
  }

  interface ObjectWriter {
    void write(T object, DataOutput output) throws IOException;
  }

  interface ObjectReader {
    T read(DataInput input) throws IOException;
  }

  private Serializations() {
    // no-op
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy