Example usage for org.apache.hadoop.io BytesWritable getBytes

List of usage examples for org.apache.hadoop.io BytesWritable getBytes

Introduction

In this page you can find the example usage for org.apache.hadoop.io BytesWritable getBytes.

Prototype

@Override
public byte[] getBytes() 

Source Link

Document

Get the data backing the BytesWritable.

Usage

From source file:com.endgame.binarypig.util.StreamUtils.java

License:Apache License

public static void writeToFile(BytesWritable value, File binaryFile) throws IOException {
    FileOutputStream fileOut = new FileOutputStream(binaryFile);
    fileOut.write(value.getBytes(), 0, value.getLength());
    fileOut.close();//from  www.ja v a2s .  c  o  m
}

From source file:com.facebook.hive.orc.lazy.LazyBinaryTreeReader.java

License:Open Source License

@Override
public Object next(Object previous) throws IOException {
    BytesWritable result = null;
    if (valuePresent) {
        if (previous == null) {
            result = new BytesWritable();
        } else {/*from   ww  w.  ja v a  2 s. c om*/
            result = (BytesWritable) previous;
        }
        int len = (int) lengths.next();
        result.setSize(len);
        int offset = 0;
        while (len > 0) {
            int written = stream.read(result.getBytes(), offset, len);
            if (written < 0) {
                throw new EOFException("Can't finish byte read from " + stream);
            }
            len -= written;
            offset += written;
        }
    }
    return result;
}

From source file:com.facebook.hive.orc.lazy.OrcLazyBinary.java

License:Open Source License

public OrcLazyBinary(OrcLazyBinary copy) {
    super(copy);/*  w w w  .  j a v a 2  s . co  m*/
    if (copy.previous != null) {
        BytesWritable copyPrevious = (BytesWritable) copy.previous;
        byte[] bytes = new byte[copyPrevious.getLength()];
        System.arraycopy(copyPrevious.getBytes(), 0, bytes, 0, copyPrevious.getLength());
        previous = new BytesWritable(bytes);
    }
}

From source file:com.facebook.presto.hive.DwrfHiveRecordCursor.java

License:Apache License

private void parseStringColumn(int column) {
    // don't include column number in message because it causes boxing which is expensive here
    checkArgument(!isPartitionColumn[column], "Column is a partition key");

    loaded[column] = true;//from  www .  j  ava 2 s  . c om
    nulls[column] = false;

    OrcLazyObject lazyObject = getRawValue(column);
    if (lazyObject == null) {
        nulls[column] = true;
        return;
    }

    Object value = materializeValue(lazyObject);
    if (value == null) {
        nulls[column] = true;
        return;
    }

    HiveType type = hiveTypes[column];
    if (type.getCategory() == Category.MAP || type.getCategory() == Category.LIST
            || type.getCategory() == Category.STRUCT) {
        slices[column] = Slices
                .wrappedBuffer(getJsonBytes(sessionTimeZone, lazyObject, fieldInspectors[column]));
    } else if (type.equals(HIVE_STRING)) {
        Text text = checkWritable(value, Text.class);
        slices[column] = Slices.copyOf(Slices.wrappedBuffer(text.getBytes()), 0, text.getLength());
    } else if (type.equals(HIVE_BINARY)) {
        BytesWritable bytesWritable = checkWritable(value, BytesWritable.class);
        slices[column] = Slices.copyOf(Slices.wrappedBuffer(bytesWritable.getBytes()), 0,
                bytesWritable.getLength());
    } else {
        throw new RuntimeException(String.format("%s is not a valid STRING type", type));
    }
}

From source file:com.facebook.presto.hive.orc.DwrfHiveRecordCursor.java

License:Apache License

private void parseStringColumn(int column) {
    // don't include column number in message because it causes boxing which is expensive here
    checkArgument(!isPartitionColumn[column], "Column is a partition key");

    loaded[column] = true;// w  ww  . j a v  a  2 s  .  co m
    nulls[column] = false;

    OrcLazyObject lazyObject = getRawValue(column);
    if (lazyObject == null) {
        nulls[column] = true;
        return;
    }

    Object value = materializeValue(lazyObject);
    if (value == null) {
        nulls[column] = true;
        return;
    }

    HiveType type = hiveTypes[column];
    if (type.equals(HIVE_STRING)) {
        Text text = checkWritable(value, Text.class);
        slices[column] = Slices.copyOf(Slices.wrappedBuffer(text.getBytes()), 0, text.getLength());
    } else if (type.equals(HIVE_BINARY)) {
        BytesWritable bytesWritable = checkWritable(value, BytesWritable.class);
        slices[column] = Slices.copyOf(Slices.wrappedBuffer(bytesWritable.getBytes()), 0,
                bytesWritable.getLength());
    } else {
        throw new RuntimeException(String.format("%s is not a valid STRING type", type));
    }
}

From source file:com.facebook.presto.hive.orc.OrcHiveRecordCursor.java

License:Apache License

private void parseStringColumn(int column) {
    // don't include column number in message because it causes boxing which is expensive here
    checkArgument(!isPartitionColumn[column], "Column is a partition key");

    loaded[column] = true;/*w ww . jav  a2s .  co  m*/
    nulls[column] = false;

    Object object = getFieldValue(row, hiveColumnIndexes[column]);
    if (object == null) {
        nulls[column] = true;
        return;
    }

    HiveType type = hiveTypes[column];
    if (type.equals(HIVE_STRING)) {
        Text text = Types.checkType(object, Text.class, "materialized string value");
        slices[column] = Slices.copyOf(Slices.wrappedBuffer(text.getBytes()), 0, text.getLength());
    } else if (type.equals(HIVE_BINARY)) {
        BytesWritable bytesWritable = Types.checkType(object, BytesWritable.class, "materialized binary value");
        slices[column] = Slices.copyOf(Slices.wrappedBuffer(bytesWritable.getBytes()), 0,
                bytesWritable.getLength());
    } else {
        throw new RuntimeException(String.format("%s is not a valid STRING type", type));
    }
}

From source file:com.facebook.presto.hive.OrcHiveRecordCursor.java

License:Apache License

private void parseStringColumn(int column) {
    // don't include column number in message because it causes boxing which is expensive here
    checkArgument(!isPartitionColumn[column], "Column is a partition key");

    loaded[column] = true;/* ww w  .  j a v  a  2  s.  c  om*/
    nulls[column] = false;

    Object object = getFieldValue(row, hiveColumnIndexes[column]);
    if (object == null) {
        nulls[column] = true;
        return;
    }

    HiveType type = hiveTypes[column];
    if (type.getCategory() == Category.MAP || type.getCategory() == Category.LIST
            || type.getCategory() == Category.STRUCT) {
        slices[column] = Slices.wrappedBuffer(getJsonBytes(sessionTimeZone, object, fieldInspectors[column]));
    } else if (type.equals(HIVE_STRING)) {
        Text text = Types.checkType(object, Text.class, "materialized string value");
        slices[column] = Slices.copyOf(Slices.wrappedBuffer(text.getBytes()), 0, text.getLength());
    } else if (type.equals(HIVE_BINARY)) {
        BytesWritable bytesWritable = Types.checkType(object, BytesWritable.class, "materialized binary value");
        slices[column] = Slices.copyOf(Slices.wrappedBuffer(bytesWritable.getBytes()), 0,
                bytesWritable.getLength());
    } else {
        throw new RuntimeException(String.format("%s is not a valid STRING type", type));
    }
}

From source file:com.facebook.presto.hive.util.SerDeUtils.java

License:Apache License

private static String getPrimitiveAsString(DateTimeZone sessionTimeZone, Object object,
        PrimitiveObjectInspector inspector) {
    switch (inspector.getPrimitiveCategory()) {
    case BOOLEAN:
    case BYTE:/*from   w ww .ja  v  a  2s .  c o  m*/
    case SHORT:
    case INT:
    case LONG:
    case FLOAT:
    case DOUBLE:
    case STRING:
        return String.valueOf(inspector.getPrimitiveJavaObject(object));
    case TIMESTAMP:
        return String.valueOf(formatTimestamp(sessionTimeZone, object, (TimestampObjectInspector) inspector));
    case BINARY:
        // Using same Base64 encoder which Jackson uses in JsonGenerator.writeBinary().
        BytesWritable writable = ((BinaryObjectInspector) inspector).getPrimitiveWritableObject(object);
        return Base64Variants.getDefaultVariant()
                .encode(Arrays.copyOf(writable.getBytes(), writable.getLength()));
    default:
        throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
    }
}

From source file:com.gotometrics.orderly.FixedByteArrayRowKey.java

License:Apache License

@Override
public Object deserialize(ImmutableBytesWritable w) throws IOException {
    BytesWritable bw = (BytesWritable) super.deserialize(w);
    if (bw == null) {
        return null;
    } else {/*from  ww w  . j  av a 2  s  .  c  o  m*/
        final byte[] result = new byte[bw.getLength()];
        System.arraycopy(bw.getBytes(), 0, result, 0, bw.getLength());

        return result;
    }
}

From source file:com.gotometrics.orderly.FixedBytesWritableRowKey.java

License:Apache License

@Override
public void serialize(Object o, ImmutableBytesWritable w) throws IOException {
    byte[] bytesToWriteIn = w.get();
    int writeOffset = w.getOffset();

    final BytesWritable bytesWritableToWrite = (BytesWritable) o;
    final int srcLen = bytesWritableToWrite.getLength();
    final byte[] bytesToWrite = bytesWritableToWrite.getBytes();

    if (srcLen != length)
        throw new IllegalArgumentException(
                "can only serialize byte arrays of length " + length + ", not " + srcLen);

    // apply the sort order mask
    final byte[] maskedBytesToWrite = maskAll(bytesToWrite, order, 0, srcLen);

    Bytes.putBytes(bytesToWriteIn, writeOffset, maskedBytesToWrite, 0, srcLen);
    RowKeyUtils.seek(w, srcLen);/*  ww w  . j  a v  a 2s . c  o m*/
}