use of org.bson.types.Binary in project mongo-hadoop by mongodb.
the class BSONComparator method compareValues.
/**
* @param one, two - two objects with the same type Cast the two objects and compare the values
*/
private int compareValues(final Object one, final Object two) {
int diff = 0;
if (one instanceof Number) {
// Need to be comparing all numeric values to one another,
// so cast all of them to Double
diff = Double.valueOf(one.toString()).compareTo(Double.valueOf(two.toString()));
} else if (one instanceof String) {
diff = ((String) one).compareTo((String) two);
} else if (one instanceof BSONObject) {
// BasicBSONObject and BasicBSONList both covered in this cast
diff = compare((BSONObject) one, (BSONObject) two);
} else if (one instanceof Binary) {
ByteBuffer buff1 = ByteBuffer.wrap(((Binary) one).getData());
ByteBuffer buff2 = ByteBuffer.wrap(((Binary) two).getData());
diff = buff1.compareTo(buff2);
} else if (one instanceof byte[]) {
ByteBuffer buff1 = ByteBuffer.wrap((byte[]) one);
ByteBuffer buff2 = ByteBuffer.wrap((byte[]) two);
diff = buff1.compareTo(buff2);
} else if (one instanceof ObjectId) {
diff = ((ObjectId) one).compareTo((ObjectId) two);
} else if (one instanceof Boolean) {
diff = ((Boolean) one).compareTo((Boolean) two);
} else if (one instanceof Date) {
diff = ((Date) one).compareTo((Date) two);
} else if (one instanceof BSONTimestamp) {
diff = ((BSONTimestamp) one).compareTo((BSONTimestamp) two);
}
return diff;
}
use of org.bson.types.Binary in project mongo-hadoop by mongodb.
the class BSONLoader method convertBSONtoPigType.
/**
* Convert an object from a MongoDB document into a type that Pig can
* understand, based on the type of the input object.
* @param o object from a MongoDB document
* @return object appropriate for pig
* @throws ExecException for lower-level Pig errors
*/
public static Object convertBSONtoPigType(final Object o) throws ExecException {
if (o == null) {
return null;
} else if (o instanceof Number || o instanceof String) {
return o;
} else if (o instanceof Date) {
return ((Date) o).getTime();
} else if (o instanceof ObjectId) {
return o.toString();
} else if (o instanceof UUID) {
return o.toString();
} else if (o instanceof BasicBSONList) {
BasicBSONList bl = (BasicBSONList) o;
Tuple t = tupleFactory.newTuple(bl.size());
for (int i = 0; i < bl.size(); i++) {
t.set(i, convertBSONtoPigType(bl.get(i)));
}
return t;
} else if (o instanceof Map) {
//TODO make this more efficient for lazy objects?
Map<String, Object> fieldsMap = (Map<String, Object>) o;
HashMap<String, Object> pigMap = new HashMap<String, Object>(fieldsMap.size());
for (Map.Entry<String, Object> field : fieldsMap.entrySet()) {
pigMap.put(field.getKey(), convertBSONtoPigType(field.getValue()));
}
return pigMap;
} else if (o instanceof byte[]) {
return new DataByteArray((byte[]) o);
} else if (o instanceof Binary) {
return new DataByteArray(((Binary) o).getData());
} else if (o instanceof DBRef) {
HashMap<String, String> pigMap = new HashMap<String, String>(2);
pigMap.put("$ref", ((DBRef) o).getCollectionName());
pigMap.put("$id", ((DBRef) o).getId().toString());
return pigMap;
} else {
return o;
}
}
use of org.bson.types.Binary 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 org.bson.types.Binary in project mongo-hadoop by mongodb.
the class UDFTest method setUpClass.
@BeforeClass
public static void setUpClass() {
INPUT_COLLECTION.drop();
insertedDocuments = new ArrayList<Document>(100);
for (int i = 0; i < 100; ++i) {
ObjectId id = new ObjectId();
insertedDocuments.add(new Document("_id", id).append("minkey", new MinKey()).append("maxkey", new MaxKey()).append("dbref", new DBRef("othercollection", new ObjectId())).append("binary", new Binary(new byte[] { 1, 2, 3, 4, 5 })).append("oidBytes", new Binary(id.toByteArray())));
}
INPUT_COLLECTION.insertMany(insertedDocuments);
}
use of org.bson.types.Binary in project mongo-java-driver by mongodb.
the class DocumentCodecTest method testPrimitiveBSONTypeCodecs.
@Test
public void testPrimitiveBSONTypeCodecs() throws IOException {
DocumentCodec documentCodec = new DocumentCodec();
Document doc = new Document();
doc.put("oid", new ObjectId());
doc.put("integer", 1);
doc.put("long", 2L);
doc.put("string", "hello");
doc.put("double", 3.2);
doc.put("decimal", Decimal128.parse("0.100"));
doc.put("binary", new Binary(BsonBinarySubType.USER_DEFINED, new byte[] { 0, 1, 2, 3 }));
doc.put("date", new Date(1000));
doc.put("boolean", true);
doc.put("code", new Code("var i = 0"));
doc.put("minkey", new MinKey());
doc.put("maxkey", new MaxKey());
// doc.put("pattern", Pattern.compile("^hello")); // TODO: Pattern doesn't override equals method!
doc.put("null", null);
documentCodec.encode(writer, doc, EncoderContext.builder().build());
BsonInput bsonInput = createInputBuffer();
Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
assertEquals(doc, decodedDocument);
}
Aggregations