Search in sources :

Example 16 with MapWritable

use of org.apache.hadoop.io.MapWritable 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 17 with MapWritable

use of org.apache.hadoop.io.MapWritable in project elephant-bird by twitter.

the class LzoW3CLogLoader method getNext.

/**
 * Return every non-null line as a single-element tuple to Pig.
 */
@Override
public Tuple getNext() throws IOException {
    LzoW3CLogRecordReader w3CLogRecordReader = (LzoW3CLogRecordReader) reader;
    if (w3CLogRecordReader == null) {
        return null;
    }
    MapWritable value_;
    try {
        if (w3CLogRecordReader.nextKeyValue() && (value_ = w3CLogRecordReader.getCurrentValue()) != null) {
            Map<String, String> values = Maps.newHashMap();
            for (Writable key : value_.keySet()) {
                Writable value = value_.get(key);
                values.put(key.toString(), value != null ? value.toString() : null);
            }
            incrCounter(LzoW3CLogLoaderCounters.LinesW3CDecoded, 1L);
            incrCounter(LzoW3CLogLoaderCounters.UnparseableLines, w3CLogRecordReader.getBadRecordsSkipped());
            return tupleFactory_.newTuple(values);
        }
    } catch (InterruptedException e) {
        int errCode = 6018;
        String errMsg = "Error while reading input";
        throw new ExecException(errMsg, errCode, PigException.REMOTE_ENVIRONMENT, e);
    }
    return null;
}
Also used : ExecException(org.apache.pig.backend.executionengine.ExecException) LzoW3CLogRecordReader(com.twitter.elephantbird.mapreduce.input.LzoW3CLogRecordReader) Writable(org.apache.hadoop.io.Writable) MapWritable(org.apache.hadoop.io.MapWritable) MapWritable(org.apache.hadoop.io.MapWritable)

Example 18 with MapWritable

use of org.apache.hadoop.io.MapWritable in project elephant-bird by twitter.

the class TestLzoJsonRecordReader method testNullString.

/**
 * {@link LzoJsonRecordReader#decodeLineToJson(JSONParser, Text, MapWritable)}
 * must not choke on lines containing the word "null" (i.e. not the null
 * value but the string "null").
 *
 * This can happen when the original input line to JSONParser contains "null"
 * as a string.  In this case {@link JSONParser#parse(java.io.Reader)} will
 * return a null reference.
 */
@Test
public void testNullString() {
    Text line = new Text("null");
    boolean result = LzoJsonRecordReader.decodeLineToJson(new JSONParser(), line, new MapWritable());
    assertEquals("Parsing line with contents 'null'", false, result);
}
Also used : Text(org.apache.hadoop.io.Text) JSONParser(org.json.simple.parser.JSONParser) MapWritable(org.apache.hadoop.io.MapWritable) Test(org.junit.Test)

Aggregations

MapWritable (org.apache.hadoop.io.MapWritable)18 Text (org.apache.hadoop.io.Text)10 Writable (org.apache.hadoop.io.Writable)9 BytesWritable (org.apache.hadoop.io.BytesWritable)5 IOException (java.io.IOException)4 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 ArrayWritable (org.apache.hadoop.io.ArrayWritable)3 BooleanWritable (org.apache.hadoop.io.BooleanWritable)3 LongWritable (org.apache.hadoop.io.LongWritable)3 NullWritable (org.apache.hadoop.io.NullWritable)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 InputTableConfig (org.apache.accumulo.core.client.mapreduce.InputTableConfig)2 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)2 ByteWritable (org.apache.hadoop.io.ByteWritable)2 DoubleWritable (org.apache.hadoop.io.DoubleWritable)2 FloatWritable (org.apache.hadoop.io.FloatWritable)2