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

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

Introduction

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

Prototype

@Override
public void readFields(DataInput in) 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 in DataInput to deserialize this object from.
 * @param schema Schema to be used for deserializing this data.
 * @return the data read and converted according to the schema.
 * @throws IOException if there was an error reading.
 *///w  w  w.  ja  v  a  2  s . c  om
private static Object readData(DataInput in, Schema schema) throws IOException {
    switch (schema.getType()) {
    case INT:
        Integer intData = WritableUtils.readVInt(in);
        return intData;
    case LONG:
        Long longData = WritableUtils.readVLong(in);
        return longData;
    case DOUBLE:
        DoubleWritable doubleWritable = (DoubleWritable) WritableFactories.newInstance(DoubleWritable.class);
        doubleWritable.readFields(in);
        return doubleWritable.get();
    case ENUM:
    case STRING:
        String stringData = WritableUtils.readString(in);
        return stringData;
    case FLOAT:
        FloatWritable floatWritable = (FloatWritable) WritableFactories.newInstance(FloatWritable.class);
        floatWritable.readFields(in);
        return floatWritable.get();
    case ARRAY:
        List<Object> listData = Lists.newArrayList();
        Integer numElements = WritableUtils.readVInt(in);
        for (int c = 0; c < numElements; c++) {
            Object listElement = readData(in, schema.getElementType());
            listData.add(listElement);
        }
        return listData;
    case RECORD:
        GenericRecord recordData = new GenericData.Record(schema);
        Integer numFields = WritableUtils.readVInt(in);
        for (int c = 0; c < numFields; c++) {
            String fieldName = WritableUtils.readString(in);
            Object fieldData = readData(in, schema.getField(fieldName).schema());
            recordData.put(fieldName, fieldData);
        }
        return recordData;
    case MAP:
        Map<String, Object> mapData = Maps.newHashMap();
        Integer numEntries = WritableUtils.readVInt(in);
        for (int c = 0; c < numEntries; c++) {
            String key = WritableUtils.readString(in);
            Object value = readData(in, schema.getValueType());
            mapData.put(key, value);
        }
        return mapData;
    case UNION:
        Integer tag = WritableUtils.readVInt(in);
        Schema unionSubSchema = schema.getTypes().get(tag);
        Object unionData = readData(in, unionSubSchema);
        return unionData;
    case BYTES:
        byte[] bytesData = WritableUtils.readCompressedByteArray(in);
        return bytesData;
    case BOOLEAN:
        BooleanWritable booleanWritable = (BooleanWritable) WritableFactories
                .newInstance(BooleanWritable.class);
        booleanWritable.readFields(in);
        return booleanWritable.get();
    case NULL:
        return null;
    default:
        throw new UnsupportedOperationException("Unsupported type: " + schema.getType());
    }
}

From source file:org.shaf.core.util.IOUtils.java

License:Apache License

/**
 * Reads an {@link Object} of the specified type from the {@link DataInput}.
 * /*from  w ww .j  ava2  s.  c o  m*/
 * @param cls
 *            the type of the reading object.
 * @param in
 *            the data input stream.
 * @return the read object.
 * @throws IOException
 *             if I/O error occurs.
 */
public static final Object readObject(Class<?> cls, DataInput in) throws IOException {
    try {
        if (cls == null) {
            throw new IOException("Reading class is not defined: null.");
        } else if (ClassUtils.isBoolean(cls)) {
            BooleanWritable obj = new BooleanWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isByte(cls)) {
            ByteWritable obj = new ByteWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isShort(cls)) {
            ShortWritable obj = new ShortWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isInteger(cls)) {
            IntWritable obj = new IntWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isLong(cls)) {
            LongWritable obj = new LongWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isFloat(cls)) {
            FloatWritable obj = new FloatWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isDouble(cls)) {
            DoubleWritable obj = new DoubleWritable();
            obj.readFields(in);
            return obj.get();
        } else if (ClassUtils.isString(cls)) {
            return Text.readString(in);
        } else if (ClassUtils.isEnum(cls)) {
            IntWritable obj = new IntWritable();
            obj.readFields(in);
            return cls.getEnumConstants()[obj.get()];
        } else if (ClassUtils.isArray(cls)) {
            int length = (int) readObject(int.class, in);
            Object array = Array.newInstance(cls.getComponentType(), length);
            for (int j = 0; j < length; j++) {
                Object a = readObject(cls.getComponentType(), in);
                Array.set(array, j, a);
            }
            return array;
        } else {
            Object obj = cls.newInstance();
            ((Writable) obj).readFields(in);
            return obj;
        }
    } catch (IllegalArgumentException | InstantiationException | IllegalAccessException exc) {
        throw new IOException(exc);
    }
}

From source file:org.shaf.core.util.IOUtilsTest.java

License:Apache License

/**
 * Test writing of {@code boolean} value.
 */// www .ja va  2s . co  m
@Test
public void testWriteBoolean() {
    byte[] buf = null;

    // Tests write for boolean "false".
    boolean value = false;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(baos);) {
        IOUtils.writeObject(value, out);
        buf = baos.toByteArray();
    } catch (IOException exc) {
        fail(exc.getMessage());
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
            DataInputStream in = new DataInputStream(bais);) {
        BooleanWritable probe = new BooleanWritable();
        probe.readFields(in);
        assertFalse(probe.get());
    } catch (IOException exc) {
        fail(exc.getMessage());
    }

    // Tests write for boolean "true".
    value = true;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream(baos);) {
        IOUtils.writeObject(value, out);
        buf = baos.toByteArray();
    } catch (IOException exc) {
        fail(exc.getMessage());
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
            DataInputStream in = new DataInputStream(bais);) {
        BooleanWritable probe = new BooleanWritable();
        probe.readFields(in);
        assertTrue(probe.get());
    } catch (IOException exc) {
        fail(exc.getMessage());
    }
}