Search in sources :

Example 31 with DoubleWritable

use of org.apache.hadoop.io.DoubleWritable in project mongo-hadoop by mongodb.

the class BSONWritable method toBSON.

/**
 * Unwrap a (usually Writable) Object, getting back a value suitable for
 * putting into a BSONObject. If the given object is not Writable, then
 * simply return the Object back.
 *
 * @param x the Object to turn into BSON.
 * @return the BSON representation of the Object.
 */
@SuppressWarnings("unchecked")
public static Object toBSON(final Object x) {
    if (x == null) {
        return null;
    }
    if (x instanceof Text) {
        return x.toString();
    }
    if (x instanceof BSONWritable) {
        return ((BSONWritable) x).getDoc();
    }
    if (x instanceof Writable) {
        if (x instanceof AbstractMapWritable) {
            if (!(x instanceof Map)) {
                throw new IllegalArgumentException(String.format("Cannot turn %s into BSON, since it does " + "not implement java.util.Map.", x.getClass().getName()));
            }
            Map<Writable, Writable> map = (Map<Writable, Writable>) x;
            BasicBSONObject bson = new BasicBSONObject();
            for (Map.Entry<Writable, Writable> entry : map.entrySet()) {
                bson.put(entry.getKey().toString(), toBSON(entry.getValue()));
            }
            return bson;
        }
        if (x instanceof ArrayWritable) {
            Writable[] o = ((ArrayWritable) x).get();
            Object[] a = new Object[o.length];
            for (int i = 0; i < o.length; i++) {
                a[i] = toBSON(o[i]);
            }
            return a;
        }
        if (x instanceof NullWritable) {
            return null;
        }
        if (x instanceof BooleanWritable) {
            return ((BooleanWritable) x).get();
        }
        if (x instanceof BytesWritable) {
            return ((BytesWritable) x).getBytes();
        }
        if (x instanceof ByteWritable) {
            return ((ByteWritable) x).get();
        }
        if (x instanceof DoubleWritable) {
            return ((DoubleWritable) x).get();
        }
        if (x instanceof FloatWritable) {
            return ((FloatWritable) x).get();
        }
        if (x instanceof LongWritable) {
            return ((LongWritable) x).get();
        }
        if (x instanceof IntWritable) {
            return ((IntWritable) x).get();
        }
    // TODO - Support counters
    }
    return x;
}
Also used : NullWritable(org.apache.hadoop.io.NullWritable) Writable(org.apache.hadoop.io.Writable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) IntWritable(org.apache.hadoop.io.IntWritable) AbstractMapWritable(org.apache.hadoop.io.AbstractMapWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) NullWritable(org.apache.hadoop.io.NullWritable) BasicBSONObject(org.bson.BasicBSONObject) FloatWritable(org.apache.hadoop.io.FloatWritable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) AbstractMapWritable(org.apache.hadoop.io.AbstractMapWritable) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) LongWritable(org.apache.hadoop.io.LongWritable) Map(java.util.Map) ByteWritable(org.apache.hadoop.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable)

Example 32 with DoubleWritable

use of org.apache.hadoop.io.DoubleWritable in project mongo-hadoop by mongodb.

the class TreasuryYieldUpdateReducer method reduce.

@Override
public void reduce(final IntWritable pKey, final Iterable<DoubleWritable> pValues, final Context pContext) throws IOException, InterruptedException {
    int count = 0;
    double sum = 0;
    for (final DoubleWritable value : pValues) {
        sum += value.get();
        count++;
    }
    final double avg = sum / count;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Average 10 Year Treasury for " + pKey.get() + " was " + avg);
    }
    BasicBSONObject query = new BasicBSONObject("_id", pKey.get());
    BasicBSONObject modifiers = new BasicBSONObject();
    modifiers.put("$set", BasicDBObjectBuilder.start().add("count", count).add("avg", avg).add("sum", sum).get());
    modifiers.put("$push", new BasicBSONObject("calculatedAt", new Date()));
    modifiers.put("$inc", new BasicBSONObject("numCalculations", 1));
    reduceResult.setQuery(query);
    reduceResult.setModifiers(modifiers);
    pContext.write(null, reduceResult);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DoubleWritable(org.apache.hadoop.io.DoubleWritable) Date(java.util.Date)

Example 33 with DoubleWritable

use of org.apache.hadoop.io.DoubleWritable in project mongo-hadoop by mongodb.

the class TreasuryYieldReducer method reduce.

@Override
public void reduce(final IntWritable pKey, final Iterable<DoubleWritable> pValues, final Context pContext) throws IOException, InterruptedException {
    int count = 0;
    double sum = 0;
    for (final DoubleWritable value : pValues) {
        sum += value.get();
        count++;
    }
    final double avg = sum / count;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Average 10 Year Treasury for " + pKey.get() + " was " + avg);
    }
    BasicBSONObject output = new BasicBSONObject();
    output.put("count", count);
    output.put("avg", avg);
    output.put("sum", sum);
    reduceResult.setDoc(output);
    pContext.write(pKey, reduceResult);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DoubleWritable(org.apache.hadoop.io.DoubleWritable)

Example 34 with DoubleWritable

use of org.apache.hadoop.io.DoubleWritable in project mongo-hadoop by mongodb.

the class BSONWritableTest method testToBSON.

@Test
public void testToBSON() {
    assertEquals(null, toBSON(null));
    assertEquals(null, toBSON(NullWritable.get()));
    assertEquals("hello", toBSON(new Text("hello")));
    DBObject obj = new BasicDBObject("hello", "world");
    assertEquals(obj, toBSON(new BSONWritable(obj)));
    final BasicBSONObject bsonResult = new BasicBSONObject("one", 1);
    SortedMapWritable smw = new SortedMapWritable();
    smw.put(new Text("one"), new IntWritable(1));
    assertEquals(bsonResult, toBSON(smw));
    MapWritable mw = new MapWritable();
    mw.put(new Text("one"), new IntWritable(1));
    assertEquals(bsonResult, toBSON(mw));
    String[] expectedObjects = new String[] { "one", "two" };
    Writable[] writableObjects = new Writable[] { new Text("one"), new Text("two") };
    ArrayWritable aw = new ArrayWritable(Text.class, writableObjects);
    Object[] actual = (Object[]) toBSON(aw);
    assertTrue(Arrays.equals(expectedObjects, actual));
    assertEquals(false, toBSON(new BooleanWritable(false)));
    byte[] bytes = new byte[] { '0', '1', '2' };
    assertEquals(bytes, toBSON(new BytesWritable(bytes)));
    byte b = (byte) 'c';
    assertEquals(b, toBSON(new ByteWritable(b)));
    assertEquals(3.14159, toBSON(new DoubleWritable(3.14159)));
    assertEquals(3.14159f, toBSON(new FloatWritable(3.14159f)));
    assertEquals(42L, toBSON(new LongWritable(42L)));
    assertEquals(42, toBSON(new IntWritable(42)));
    // Catchall
    assertEquals("hi", toBSON("hi"));
}
Also used : NullWritable(org.apache.hadoop.io.NullWritable) SortedMapWritable(org.apache.hadoop.io.SortedMapWritable) Writable(org.apache.hadoop.io.Writable) MapWritable(org.apache.hadoop.io.MapWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) IntWritable(org.apache.hadoop.io.IntWritable) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) SortedMapWritable(org.apache.hadoop.io.SortedMapWritable) SortedMapWritable(org.apache.hadoop.io.SortedMapWritable) MapWritable(org.apache.hadoop.io.MapWritable) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) FloatWritable(org.apache.hadoop.io.FloatWritable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable) Test(org.junit.Test)

Example 35 with DoubleWritable

use of org.apache.hadoop.io.DoubleWritable in project hive by apache.

the class TestETypeConverter method testGetDoubleConverter.

@Test
public void testGetDoubleConverter() throws Exception {
    MyConverterParent converterParent = new MyConverterParent();
    PrimitiveType primitiveType = Types.optional(PrimitiveTypeName.DOUBLE).named("value");
    PrimitiveConverter converter = ETypeConverter.getNewConverter(primitiveType, 1, converterParent, null);
    ((PrimitiveConverter) converter).addDouble(3276);
    Writable writable = converterParent.getValue();
    DoubleWritable doubleWritable = (DoubleWritable) writable;
    assertEquals(3276, doubleWritable.get(), 0);
}
Also used : PrimitiveConverter(org.apache.parquet.io.api.PrimitiveConverter) Writable(org.apache.hadoop.io.Writable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) IntWritable(org.apache.hadoop.io.IntWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) PrimitiveType(org.apache.parquet.schema.PrimitiveType) DoubleWritable(org.apache.hadoop.io.DoubleWritable) Test(org.junit.Test)

Aggregations

DoubleWritable (org.apache.hadoop.io.DoubleWritable)38 IntWritable (org.apache.hadoop.io.IntWritable)20 Test (org.junit.Test)19 Path (org.apache.hadoop.fs.Path)12 BooleanWritable (org.apache.hadoop.io.BooleanWritable)12 LongWritable (org.apache.hadoop.io.LongWritable)12 BytesWritable (org.apache.hadoop.io.BytesWritable)11 FloatWritable (org.apache.hadoop.io.FloatWritable)11 FileSystem (org.apache.hadoop.fs.FileSystem)10 Writable (org.apache.hadoop.io.Writable)10 Configuration (org.apache.hadoop.conf.Configuration)9 Text (org.apache.hadoop.io.Text)8 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)7 ByteWritable (org.apache.hadoop.io.ByteWritable)7 NullWritable (org.apache.hadoop.io.NullWritable)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 SequenceFile (org.apache.hadoop.io.SequenceFile)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4