Java Code Examples for org.apache.hadoop.io.Writable#getClass()

The following examples show how to use org.apache.hadoop.io.Writable#getClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SequenceFileAccessor.java    From pxf with Apache License 2.0 6 votes vote down vote up
@Override
public boolean writeNextObject(OneRow onerow) throws IOException {
    Writable value = (Writable) onerow.getData();
    Writable key = (Writable) onerow.getKey();

    // init writer on first approach here, based on onerow.getData type
    // TODO: verify data is serializable.
    if (writer == null) {
        Class<? extends Writable> valueClass = value.getClass();
        Class<? extends Writable> keyClass = (key == null) ? LongWritable.class
                : key.getClass();
        // create writer - do not allow overwriting existing file
        writer = SequenceFile.createWriter(fc, configuration, file, keyClass,
                valueClass, compressionType, codec,
                new SequenceFile.Metadata(), EnumSet.of(CreateFlag.CREATE));
    }

    try {
        writer.append((key == null) ? defaultKey : key, value);
    } catch (IOException e) {
        LOG.error("Failed to write data to file: {}", e.getMessage());
        return false;
    }

    return true;
}
 
Example 2
Source File: WALFile.java    From streamx with Apache License 2.0 6 votes vote down vote up
/**
 * Read the next key in the file into <code>key</code>, skipping its value.  True if another
 * entry exists, and false at end of file.
 */
public synchronized boolean next(Writable key) throws IOException {
  if (key.getClass() != WALEntry.class) {
    throw new IOException("wrong key class: " + key.getClass().getName()
                          + " is not " + WALEntry.class);
  }

  outBuf.reset();

  keyLength = next(outBuf);
  if (keyLength < 0) {
    return false;
  }

  valBuffer.reset(outBuf.getData(), outBuf.getLength());

  key.readFields(valBuffer);
  valBuffer.mark(0);
  if (valBuffer.getPosition() != keyLength) {
    throw new IOException(key + " read " + valBuffer.getPosition()
                          + " bytes, should read " + keyLength);
  }


  return true;
}
 
Example 3
Source File: DataWritableWriter.java    From parquet-mr with Apache License 2.0 6 votes vote down vote up
private void writePrimitive(final Writable value) {
  if (value == null) {
    return;
  }
  if (value instanceof DoubleWritable) {
    recordConsumer.addDouble(((DoubleWritable) value).get());
  } else if (value instanceof BooleanWritable) {
    recordConsumer.addBoolean(((BooleanWritable) value).get());
  } else if (value instanceof FloatWritable) {
    recordConsumer.addFloat(((FloatWritable) value).get());
  } else if (value instanceof IntWritable) {
    recordConsumer.addInteger(((IntWritable) value).get());
  } else if (value instanceof LongWritable) {
    recordConsumer.addLong(((LongWritable) value).get());
  } else if (value instanceof ShortWritable) {
    recordConsumer.addInteger(((ShortWritable) value).get());
  } else if (value instanceof ByteWritable) {
    recordConsumer.addInteger(((ByteWritable) value).get());
  } else if (value instanceof BigDecimalWritable) {
    throw new UnsupportedOperationException("BigDecimal writing not implemented");
  } else if (value instanceof BinaryWritable) {
    recordConsumer.addBinary(((BinaryWritable) value).getBinary());
  } else {
    throw new IllegalArgumentException("Unknown value type: " + value + " " + value.getClass());
  }
}
 
Example 4
Source File: ReadRCFileBuilder.java    From kite with Apache License 2.0 6 votes vote down vote up
private Writable updateColumnValue(RCFileColumn column, BytesRefWritable bytesRef) throws IOException {
  if(bytesRef.getLength() == 0) {
    // This is a null field.
    return NullWritable.get();
  }
  Writable newColumnValue = column.newWritable();
  // Small optimization to bypass DataInput read if the column writable is
  // BytesRefWritable
  if (newColumnValue.getClass() == BytesRefWritable.class) {
    newColumnValue = bytesRef;
  } else {
    byte[] currentRowBytes = Arrays.copyOfRange(bytesRef.getData(),
        bytesRef.getStart(), bytesRef.getStart() + bytesRef.getLength());
    DataInput dataInput = ByteStreams.newDataInput(currentRowBytes);
    newColumnValue.readFields(dataInput);
  }
  return newColumnValue;
}
 
Example 5
Source File: WALFile.java    From streamx with Apache License 2.0 5 votes vote down vote up
/**
 * Read the next key/value pair in the file into <code>key</code> and <code>val</code>.  Returns
 * true if such a pair exists and false when at end of file
 */
public synchronized boolean next(Writable key, Writable val)
    throws IOException {
  if (val.getClass() != WALEntry.class) {
    throw new IOException("wrong value class: " + val + " is not " + WALEntry.class);
  }

  boolean more = next(key);

  if (more) {
    getCurrentValue(val);
  }

  return more;
}
 
Example 6
Source File: SequenceFile.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/** Read the next key/value pair in the file into <code>key</code> and
 * <code>val</code>.  Returns true if such a pair exists and false when at
 * end of file */
public synchronized boolean next(Writable key, Writable val)
  throws IOException {
  if (val.getClass() != getValueClass())
    throw new IOException("wrong value class: "+val+" is not "+valClass);

  boolean more = next(key);
  
  if (more) {
    getCurrentValue(val);
  }

  return more;
}
 
Example 7
Source File: SequenceFile.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/** Read the next key/value pair in the file into <code>key</code> and
 * <code>val</code>.  Returns true if such a pair exists and false when at
 * end of file */
public synchronized boolean next(Writable key, Writable val)
  throws IOException {
  if (val.getClass() != getValueClass())
    throw new IOException("wrong value class: "+val+" is not "+valClass);

  boolean more = next(key);
  
  if (more) {
    getCurrentValue(val);
  }

  return more;
}
 
Example 8
Source File: Warp10RecordWriter.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public void write(Writable key, Writable value) throws IOException, InterruptedException {
  if (!init) {
    synchronized(props) {
      if (!init) {
        init();
      }
    }
  }
  
  //
  // Assume the value is a GTSWrapper
  //
  
  long count = 0L;

  TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());

  GTSWrapper gtsWrapper = new GTSWrapper();

  try {
    if (value instanceof BytesWritable) {
      deserializer.deserialize(gtsWrapper, ((BytesWritable) value).copyBytes());
    } else if (value instanceof Text) {
      deserializer.deserialize(gtsWrapper, OrderPreservingBase64.decode(((Text) value).copyBytes()));
    } else {
      throw new IOException("Invalid value class, expecting BytesWritable or Text, was " + value.getClass());
    }
  } catch (TException te) {
    throw new IOException(te);
  }

  Metadata metadataChunk;
  
  if (gtsWrapper.isSetMetadata()) {
    metadataChunk = new Metadata(gtsWrapper.getMetadata());
  } else {
    metadataChunk = new Metadata();
  }

  GTSDecoder decoder = GTSWrapperHelper.fromGTSWrapperToGTSDecoder(gtsWrapper);

  StringBuilder metasb = new StringBuilder();
  // We don't care about exposing labels since they are forced by the token
  GTSHelper.metadataToString(metasb, metadataChunk.getName(), metadataChunk.getLabels(), false);

  boolean first = true;

  while (decoder.next()) {
    if (null != this.limiter) {
      this.limiter.acquire(1);
    }

    if (!first) {
      this.pw.print("=");
      this.pw.println(GTSHelper.tickToString(null, decoder.getTimestamp(), decoder.getLocation(), decoder.getElevation(), decoder.getBinaryValue()));
    } else {
      pw.println(GTSHelper.tickToString(metasb, decoder.getTimestamp(), decoder.getLocation(), decoder.getElevation(), decoder.getBinaryValue()));
      first = false;
    }
    count++;
  }
}