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

com.twitter.elephantbird.pig.util.IntWritableConverter Maven / Gradle / Ivy

There is a newer version: 4.17
Show newest version
package com.twitter.elephantbird.pig.util;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.pig.ResourceSchema.ResourceFieldSchema;
import org.apache.pig.data.DataByteArray;
import org.apache.pig.data.DataType;

/**
 * Supports conversion between Pig types and {@link IntWritable}.
 *
 * @author Andy Schlaikjer
 */
public class IntWritableConverter extends AbstractWritableConverter {
  public IntWritableConverter() {
    super(new IntWritable());
  }

  @Override
  public ResourceFieldSchema getLoadSchema() throws IOException {
    ResourceFieldSchema schema = new ResourceFieldSchema();
    schema.setType(DataType.INTEGER);
    return schema;
  }

  @Override
  public Object bytesToObject(DataByteArray dataByteArray) throws IOException {
    return bytesToInteger(dataByteArray.get());
  }

  @Override
  protected String toCharArray(IntWritable writable) throws IOException {
    return String.valueOf(writable.get());
  }

  @Override
  protected Integer toInteger(IntWritable writable) throws IOException {
    return writable.get();
  }

  @Override
  protected Long toLong(IntWritable writable) throws IOException {
    return (long) writable.get();
  }

  @Override
  protected Float toFloat(IntWritable writable) throws IOException {
    return (float) writable.get();
  }

  @Override
  protected Double toDouble(IntWritable writable) throws IOException {
    return (double) writable.get();
  }

  @Override
  public void checkStoreSchema(ResourceFieldSchema schema) throws IOException {
    switch (schema.getType()) {
      case DataType.CHARARRAY:
      case DataType.INTEGER:
      case DataType.LONG:
      case DataType.FLOAT:
      case DataType.DOUBLE:
        return;
    }
    throw new IOException("Pig type '" + DataType.findTypeName(schema.getType()) + "' unsupported");
  }

  @Override
  protected IntWritable toWritable(String value) throws IOException {
    return toWritable(Integer.parseInt(value));
  }

  @Override
  protected IntWritable toWritable(Integer value) throws IOException {
    writable.set(value);
    return writable;
  }

  @Override
  protected IntWritable toWritable(Long value) throws IOException {
    return toWritable(value.intValue());
  }

  @Override
  protected IntWritable toWritable(Float value) throws IOException {
    return toWritable(value.intValue());
  }

  @Override
  protected IntWritable toWritable(Double value) throws IOException {
    return toWritable(value.intValue());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy