Search in sources :

Example 1 with MongoRecordReader

use of com.mongodb.hadoop.input.MongoRecordReader in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testBinaryNoSchema.

@Test
@SuppressWarnings("unchecked")
public void testBinaryNoSchema() throws IOException {
    byte[] data = new byte[] { 1, 2, 3 };
    BasicDBObject obj = new BasicDBObject("bytes", new Binary(data));
    MongoRecordReader rr = mock(MongoRecordReader.class);
    when(rr.nextKeyValue()).thenReturn(true);
    when(rr.getCurrentValue()).thenReturn(obj);
    // No explicit schema.
    MongoLoader ml = new MongoLoader();
    ml.prepareToRead(rr, null);
    Tuple result = ml.getNext();
    // Tuple just contains a Map.
    Map<String, Object> tupleContents;
    tupleContents = (Map<String, Object>) result.get(0);
    // Map contains DataByteArray with binary data.
    assertArrayEquals(data, ((DataByteArray) tupleContents.get("bytes")).get());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MongoRecordReader(com.mongodb.hadoop.input.MongoRecordReader) BasicDBObject(com.mongodb.BasicDBObject) Binary(org.bson.types.Binary) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Example 2 with MongoRecordReader

use of com.mongodb.hadoop.input.MongoRecordReader in project mongo-hadoop by mongodb.

the class MongoRecordReaderTest method testGetCurrentKey.

@Test
public void testGetCurrentKey() throws Exception {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoClientURI uri = new MongoClientURIBuilder().collection("mongo_hadoop", "mongo_record_reader_test").build();
    DBCollection collection = client.getDB(uri.getDatabase()).getCollection(uri.getCollection());
    collection.drop();
    BasicDBList colors = new BasicDBList() {

        {
            add(new BasicBSONObject("red", 255));
            add(new BasicBSONObject("blue", 255));
            add(new BasicBSONObject("green", 0));
        }
    };
    collection.insert(new BasicDBObject("_id", 0).append("address", new BasicDBObject("street", "foo street")).append("colors", colors));
    // Default case: "_id" is used as inputKey.
    MongoInputSplit split = new MongoInputSplit();
    split.setInputURI(uri);
    MongoRecordReader reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), 0);
    // Use a nested field as inputKey.
    split = new MongoInputSplit();
    split.setInputURI(uri);
    split.setKeyField("address.street");
    reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), "foo street");
    // Use a key within an array as the inputKey.
    split = new MongoInputSplit();
    split.setInputURI(uri);
    split.setKeyField("colors.1");
    reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), new BasicBSONObject("blue", 255));
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) MongoInputSplit(com.mongodb.hadoop.input.MongoInputSplit) MongoClientURIBuilder(com.mongodb.hadoop.util.MongoClientURIBuilder) MongoRecordReader(com.mongodb.hadoop.input.MongoRecordReader) MongoClientURI(com.mongodb.MongoClientURI) Test(org.junit.Test)

Example 3 with MongoRecordReader

use of com.mongodb.hadoop.input.MongoRecordReader in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testByteArrayNoSchema.

@Test
@SuppressWarnings("unchecked")
public void testByteArrayNoSchema() throws IOException {
    byte[] data = new byte[] { 1, 2, 3 };
    BasicDBObject obj = new BasicDBObject("bytes", data);
    MongoRecordReader rr = mock(MongoRecordReader.class);
    when(rr.nextKeyValue()).thenReturn(true);
    when(rr.getCurrentValue()).thenReturn(obj);
    // No explicit schema.
    MongoLoader ml = new MongoLoader();
    ml.prepareToRead(rr, null);
    Tuple result = ml.getNext();
    // Tuple just contains a Map.
    Map<String, Object> tupleContents;
    tupleContents = (Map<String, Object>) result.get(0);
    // Map contains DataByteArray with binary data.
    assertArrayEquals(data, ((DataByteArray) tupleContents.get("bytes")).get());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MongoRecordReader(com.mongodb.hadoop.input.MongoRecordReader) BasicDBObject(com.mongodb.BasicDBObject) Tuple(org.apache.pig.data.Tuple) Test(org.junit.Test)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)3 MongoRecordReader (com.mongodb.hadoop.input.MongoRecordReader)3 Test (org.junit.Test)3 Tuple (org.apache.pig.data.Tuple)2 BasicDBList (com.mongodb.BasicDBList)1 DBCollection (com.mongodb.DBCollection)1 MongoClient (com.mongodb.MongoClient)1 MongoClientURI (com.mongodb.MongoClientURI)1 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)1 MongoClientURIBuilder (com.mongodb.hadoop.util.MongoClientURIBuilder)1 BasicBSONObject (org.bson.BasicBSONObject)1 Binary (org.bson.types.Binary)1