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

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

Introduction

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

Prototype

public BooleanWritable(boolean value) 

Source Link

Usage

From source file:org.huahinframework.core.util.ObjectUtil.java

License:Apache License

/**
 * Convert the HadoopObject from Java primitive.
 * @param object Java primitive object/*w ww .  j  a  v a2s. co  m*/
 * @return HadoopObject
 */
public static HadoopObject primitive2Hadoop(Object object) {
    if (object == null) {
        return new HadoopObject(NULL, NullWritable.get());
    }

    if (object instanceof Byte) {
        return new HadoopObject(BYTE, new ByteWritable((Byte) object));
    } else if (object instanceof Integer) {
        return new HadoopObject(INTEGER, new IntWritable((Integer) object));
    } else if (object instanceof Long) {
        return new HadoopObject(LONG, new LongWritable((Long) object));
    } else if (object instanceof Double) {
        return new HadoopObject(DOUBLE, new DoubleWritable((Double) object));
    } else if (object instanceof Float) {
        return new HadoopObject(FLOAT, new FloatWritable((Float) object));
    } else if (object instanceof Boolean) {
        return new HadoopObject(BOOLEAN, new BooleanWritable((Boolean) object));
    } else if (object instanceof String) {
        return new HadoopObject(STRING, new Text((String) object));
    } else if (object.getClass().isArray()) {
        return arrayPrimitive2Hadoop(object);
    } else if (object instanceof Collection<?>) {
        Collection<?> collection = (Collection<?>) object;
        return arrayPrimitive2Hadoop(collection.toArray());
    } else if (object instanceof Map<?, ?>) {
        Map<?, ?> map = (Map<?, ?>) object;
        if (map.size() == 0) {
            throw new ClassCastException("object not found");
        }

        MapWritable mapWritable = new MapWritable();
        for (Entry<?, ?> entry : map.entrySet()) {
            mapWritable.put(primitive2Hadoop(entry.getKey()).getObject(),
                    primitive2Hadoop(entry.getValue()).getObject());
        }

        return new HadoopObject(MAP, mapWritable);
    }

    throw new ClassCastException("cast object not found");
}

From source file:org.huahinframework.core.util.ObjectUtilTest.java

License:Apache License

@Test
public void testPrimitive2HadoopIOBoolean() {
    boolean o = true;
    HadoopObject ho = ObjectUtil.primitive2Hadoop(o);
    assertEquals(ObjectUtil.BOOLEAN, ho.getType());
    assertEquals(new BooleanWritable(o), ho.getObject());
}

From source file:org.huahinframework.core.util.ObjectUtilTest.java

License:Apache License

@Test
public void testHadoopIO2PrimitiveBoolean() {
    boolean o = true;
    PrimitiveObject no = ObjectUtil.hadoop2Primitive(new BooleanWritable(o));
    assertEquals(ObjectUtil.BOOLEAN, no.getType());
    assertEquals(o, no.getObject());//from   w  w w .  ja v  a 2  s  .  com
}

From source file:org.openflamingo.mapreduce.aggregator.BooleanAndAggregator.java

License:Apache License

@Override
public BooleanWritable getAggregatedValue() {
    return new BooleanWritable(result);
}

From source file:org.openflamingo.mapreduce.aggregator.BooleanAndAggregatorTest.java

License:Apache License

@Test
public void aggregateWritable() throws Exception {
    BooleanAndAggregator aggregator = new BooleanAndAggregator();
    aggregator.aggregate(new BooleanWritable(false));
    aggregator.aggregate(new BooleanWritable(true));
    aggregator.aggregate(new BooleanWritable(true));

    BooleanWritable value = aggregator.getAggregatedValue();

    Assert.assertFalse(value.get());//  w w  w  . jav  a  2s  .co  m
}

From source file:org.openx.data.jsonserde.objectinspector.primitive.JavaStringBooleanObjectInspector.java

License:Open Source License

@Override
public Object getPrimitiveWritableObject(Object o) {
    if (o == null)
        return null;

    if (o instanceof String) {
        return new BooleanWritable(Boolean.parseBoolean((String) o));
    } else {/*w w w. ja v a 2  s.  co  m*/
        return new BooleanWritable(((Boolean) o));
    }
}

From source file:org.pentaho.hadoop.mapreduce.converter.converters.KettleTypeToBooleanWritableConverterTest.java

License:Apache License

@Test
public void convert() throws Exception {
    KettleTypeToBooleanWritableConverter c = new KettleTypeToBooleanWritableConverter();
    BooleanWritable expected = new BooleanWritable(false);
    String value = "false";

    // Convert from a normal String
    ValueMeta normalMeta = new ValueMeta("test", ValueMetaInterface.TYPE_STRING,
            ValueMetaInterface.STORAGE_TYPE_NORMAL);
    assertEquals(expected, c.convert(normalMeta, value));

    // Convert from a byte array
    ValueMeta binaryMeta = new ValueMeta("test", ValueMetaInterface.TYPE_STRING,
            ValueMetaInterface.STORAGE_TYPE_BINARY_STRING);
    ValueMeta storageMeta = new ValueMeta("test", ValueMetaInterface.TYPE_STRING,
            ValueMetaInterface.STORAGE_TYPE_NORMAL);
    binaryMeta.setStorageMetadata(storageMeta);
    byte[] rawValue = value.getBytes("UTF-8");
    assertEquals(expected, c.convert(binaryMeta, rawValue));

    // Convert from an Integer
    ValueMeta integerMeta = new ValueMeta("test", ValueMetaInterface.TYPE_INTEGER,
            ValueMetaInterface.STORAGE_TYPE_NORMAL);
    assertEquals(new BooleanWritable(true), c.convert(integerMeta, Long.valueOf(1)));

    try {//from w  w  w .j av a2s  .c  om
        c.convert(null, null);
        fail();
    } catch (TypeConversionException ex) {
        assertTrue(ex.getMessage().contains("Error converting to"));
    }

    try {
        c.convert(integerMeta, "not an integer");
        fail();
    } catch (TypeConversionException ex) {
        assertTrue(ex.getMessage().contains("Error converting to"));
    }
}

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

License:Apache License

/**
 * Writes an {@link Object} to the {@link DataOutput}.
 * /*from www  .  j a  va  2 s.c o m*/
 * @param obj
 *            the object to write.
 * @param out
 *            the data output stream.
 * @throws IOException
 *             if I/O error occurs.
 */
public static final void writeObject(Object obj, DataOutput out) throws IOException {
    try {
        if (obj == null) {
            throw new IOException("Writing object is not defined: null.");
        } else if (ClassUtils.isBoolean(obj)) {
            (new BooleanWritable((boolean) obj)).write(out);
        } else if (ClassUtils.isByte(obj)) {
            (new ByteWritable((byte) obj)).write(out);
        } else if (ClassUtils.isShort(obj)) {
            (new ShortWritable((short) obj)).write(out);
        } else if (ClassUtils.isInteger(obj)) {
            (new IntWritable((int) obj)).write(out);
        } else if (ClassUtils.isLong(obj)) {
            (new LongWritable((long) obj)).write(out);
        } else if (ClassUtils.isFloat(obj)) {
            (new FloatWritable((float) obj)).write(out);
        } else if (ClassUtils.isDouble(obj)) {
            (new DoubleWritable((double) obj)).write(out);
        } else if (ClassUtils.isString(obj)) {
            Text.writeString(out, (String) obj);
        } else if (ClassUtils.isEnum(obj)) {
            (new IntWritable(((Enum<?>) obj).ordinal())).write(out);
        } else if (ClassUtils.isArray(obj)) {
            int length = Array.getLength(obj);
            writeObject(length, out);
            for (int j = 0; j < length; j++) {
                writeObject(Array.get(obj, j), out);
            }
        } else {
            ((Writable) obj).write(out);
        }
    } catch (IllegalArgumentException exc) {
        throw new IOException(exc);
    }
}

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

License:Apache License

/**
 * Test reading of {@code boolean} value.
 *///from  w ww .  j  a v  a  2  s  .c  om
@Test
public void testReadBoolean() {
    byte[] buf = null;

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

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
            DataInputStream in = new DataInputStream(bais);) {
        assertFalse((boolean) IOUtils.readObject(boolean.class, in));
    } catch (IOException exc) {
        fail(exc.getMessage());
    }

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

    try (ByteArrayInputStream bais = new ByteArrayInputStream(buf);
            DataInputStream in = new DataInputStream(bais);) {
        assertTrue((boolean) IOUtils.readObject(boolean.class, in));
    } catch (IOException exc) {
        fail(exc.getMessage());
    }
}

From source file:org.wonderbee.elasticsearch.hive.ElasticSearchSerDe.java

License:Apache License

/**
 * Recursively converts an arbitrary object into the appropriate writable. Please enlighten me if there is an existing
 * method for doing this./*ww  w  . ja v  a 2 s  . c  om*/
 */
private Writable toWritable(Object thing) {
    if (thing instanceof String) {
        return new Text((String) thing);
    } else if (thing instanceof Long) {
        return new LongWritable((Long) thing);
    } else if (thing instanceof Integer) {
        return new IntWritable((Integer) thing);
    } else if (thing instanceof Double) {
        return new DoubleWritable((Double) thing);
    } else if (thing instanceof Float) {
        return new FloatWritable((Float) thing);
    } else if (thing instanceof Boolean) {
        return new BooleanWritable((Boolean) thing);
    } else if (thing instanceof Map) {
        MapWritable result = new MapWritable();
        for (Map.Entry<String, Object> entry : ((Map<String, Object>) thing).entrySet()) {
            result.put(new Text(entry.getKey().toString()), toWritable(entry.getValue()));
        }
        return result;
    } else if (thing instanceof List) {
        if (((List) thing).size() > 0) {
            Object first = ((List) thing).get(0);
            Writable[] listOfThings = new Writable[((List) thing).size()];
            for (int i = 0; i < listOfThings.length; i++) {
                listOfThings[i] = toWritable(((List) thing).get(i));
            }
            return new ArrayWritable(toWritable(first).getClass(), listOfThings);
        }
    }
    return NullWritable.get();
}