Search in sources :

Example 1 with Binary

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;
}
Also used : ObjectId(org.bson.types.ObjectId) LazyBSONObject(org.bson.LazyBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BSONTimestamp(org.bson.types.BSONTimestamp) Binary(org.bson.types.Binary) ByteBuffer(java.nio.ByteBuffer) Date(java.util.Date)

Example 2 with Binary

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;
    }
}
Also used : ObjectId(org.bson.types.ObjectId) HashMap(java.util.HashMap) BasicBSONList(org.bson.types.BasicBSONList) DBRef(com.mongodb.DBRef) Date(java.util.Date) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) Binary(org.bson.types.Binary) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple)

Example 3 with Binary

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());
}
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 4 with Binary

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);
}
Also used : MinKey(org.bson.types.MinKey) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) DBRef(com.mongodb.DBRef) Binary(org.bson.types.Binary) Document(org.bson.Document) BeforeClass(org.junit.BeforeClass)

Example 5 with Binary

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);
}
Also used : MinKey(org.bson.types.MinKey) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) MaxKey(org.bson.types.MaxKey) BsonBinaryReader(org.bson.BsonBinaryReader) Binary(org.bson.types.Binary) Document(org.bson.Document) Code(org.bson.types.Code) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Binary (org.bson.types.Binary)20 Date (java.util.Date)11 ObjectId (org.bson.types.ObjectId)10 Test (org.junit.Test)10 MaxKey (org.bson.types.MaxKey)8 MinKey (org.bson.types.MinKey)8 BSONTimestamp (org.bson.types.BSONTimestamp)7 Code (org.bson.types.Code)7 BasicDBObject (com.mongodb.BasicDBObject)5 DBRef (com.mongodb.DBRef)5 CodeWScope (org.bson.types.CodeWScope)5 Document (org.bson.Document)4 Symbol (org.bson.types.Symbol)3 MongoGridFSException (com.mongodb.MongoGridFSException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 GregorianCalendar (java.util.GregorianCalendar)2 List (java.util.List)2 Map (java.util.Map)2