Example usage for org.apache.hadoop.io BooleanWritable write

List of usage examples for org.apache.hadoop.io BooleanWritable write

Introduction

In this page you can find the example usage for org.apache.hadoop.io BooleanWritable write.

Prototype

@Override
public void write(DataOutput out) throws IOException 

Source Link

Usage

From source file:com.moz.fiji.hive.io.FijiCellWritable.java

License:Apache License

/**
 * Reads and converts data according to the specified schema.
 *
 * @param out DataOutput to serialize this object into.
 * @param data data to be serialized.//from   ww w . j  av  a2 s .co  m
 * @param schema Schema to be used for serializing this data.
 * @throws IOException if there was an error writing.
 */
private static void writeData(DataOutput out, Object data, Schema schema) throws IOException {
    switch (schema.getType()) {
    case INT:
        Integer intData = (Integer) data;
        WritableUtils.writeVInt(out, intData);
        break;
    case LONG:
        Long longData = (Long) data;
        WritableUtils.writeVLong(out, longData);
        break;
    case DOUBLE:
        Double doubleData = (Double) data;
        DoubleWritable doubleWritable = new DoubleWritable(doubleData);
        doubleWritable.write(out);
        break;
    case ENUM:
    case STRING:
        String stringData = data.toString();
        WritableUtils.writeString(out, stringData);
        break;
    case FLOAT:
        Float floatData = (Float) data;
        FloatWritable floatWritable = new FloatWritable(floatData);
        floatWritable.write(out);
        break;
    case ARRAY:
        List<Object> listData = (List<Object>) data;
        WritableUtils.writeVInt(out, listData.size());
        for (Object listElement : listData) {
            writeData(out, listElement, schema.getElementType());
        }
        break;
    case RECORD:
        IndexedRecord recordData = (IndexedRecord) data;
        WritableUtils.writeVInt(out, schema.getFields().size());
        for (Schema.Field field : schema.getFields()) {
            WritableUtils.writeString(out, field.name());
            writeData(out, recordData.get(field.pos()), field.schema());
        }
        break;
    case MAP:
        Map<String, Object> mapData = (Map<String, Object>) data;
        WritableUtils.writeVInt(out, mapData.size());
        for (Map.Entry<String, Object> entry : mapData.entrySet()) {
            WritableUtils.writeString(out, entry.getKey());
            writeData(out, entry.getValue(), schema.getValueType());
        }
        break;
    case UNION:
        final Integer tag = GenericData.get().resolveUnion(schema, data);
        WritableUtils.writeVInt(out, tag);
        Schema unionSubSchema = schema.getTypes().get(tag);
        writeData(out, data, unionSubSchema);
        break;
    case BYTES:
        byte[] bytesData = (byte[]) data;
        WritableUtils.writeCompressedByteArray(out, bytesData);
        break;
    case BOOLEAN:
        Boolean booleanData = (Boolean) data;
        BooleanWritable booleanWritable = new BooleanWritable(booleanData);
        booleanWritable.write(out);
        break;
    case NULL:
        // Don't need to write anything for null.
        break;
    case FIXED:
    default:
        throw new UnsupportedOperationException("Unsupported type: " + schema.getType());
    }
}