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());
}
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));
}
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());
}
Aggregations