org.bson.BSONObject Java Examples

The following examples show how to use org.bson.BSONObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SequoiaResultSet.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Object getObject(String columnLabel) throws SQLException {
	
	if (isSum) {
	   if (isGroupBy){
		  Object ob=dblist.get(_row-1);
		  if (ob instanceof BSONObject) {
			  return ((BSONObject)ob).get(columnLabel);
		  }
		  else {
			  return "0";  
		  }
	   }
	   else{
		   return  this._sum;
	   }
	}
	else {
		return this._cur.get(columnLabel);
	}
}
 
Example #2
Source File: SequoiaSQLParser.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private int UpData(SQLUpdateStatement state) {
	SQLTableSource table=state.getTableSource();
	DBCollection coll =this._db.getCollection(table.toString());
	
	SQLExpr expr=state.getWhere();
	BSONObject query = parserWhere(expr);
	
	BasicBSONObject set = new BasicBSONObject();
	for(SQLUpdateSetItem col : state.getItems()){
		set.put(getFieldName2(col.getColumn()), getExpValue(col.getValue()));	
	}
	BSONObject mod = new BasicBSONObject("$set", set);
	//coll.updateMulti(query, mod);
	coll.update(query, mod, null);
	//System.out.println("changs count:"+coll.getStats().size());
	return 1;		
}
 
Example #3
Source File: TenantAndIdEmittableKeyTest.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Test
public void testToBSON() {
    TenantAndIdEmittableKey key =
        new TenantAndIdEmittableKey("meta.data.tenantId", "test.id.key.field");
    key.setTenantId(new Text("Midgar"));
    key.setId(new Text("1234"));

    BSONObject bson = key.toBSON();
    assertNotNull(bson);
    assertTrue(bson.containsField("meta.data.tenantId"));

    Object obj = bson.get("meta.data.tenantId");
    assertNotNull(obj);
    assertTrue(obj instanceof String);
    String val = (String) obj;
    assertEquals(val, "Midgar");

    assertTrue(bson.containsField("test.id.key.field"));
    obj = bson.get("test.id.key.field");
    assertNotNull(obj);
    assertTrue(obj instanceof String);
    val = (String) obj;
    assertEquals(val, "1234");
}
 
Example #4
Source File: MongoDBClientSupport.java    From brooklyn-library with Apache License 2.0 6 votes vote down vote up
/**
 * Reconfigures the replica set that this client is the primary member of to include a new member.
 * <p/>
 * Note that this can cause long downtime (typically 10-20s, even up to a minute).
 *
 * @param secondary New member of the set.
 * @param id The id for the new set member. Must be unique within the set.
 * @return True if successful
 */
public boolean addMemberToReplicaSet(MongoDBServer secondary, Integer id) {
    // We need to:
    // - get the existing configuration
    // - update its version
    // - add the new member to its list of members
    // - run replSetReconfig with the new configuration.
    BSONObject existingConfig = getReplicaSetConfig();
    if (existingConfig == null) {
        LOG.warn("Couldn't load existing config for replica set from {}. Server {} not added.",
                getServerAddress(), secondary);
        return false;
    }

    BasicBSONObject newConfig = ReplicaSetConfig.fromExistingConfig(existingConfig)
            .primary(getServerHostAndPort())
            .member(secondary, id)
            .build();
    return reconfigureReplicaSet(newConfig);
}
 
Example #5
Source File: BSONValueLookupTest.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetValues() {
    // root.body.profile.name.first = George
    BSONObject root = new BasicBSONObject();
    BSONObject body = new BasicBSONObject();
    BasicBSONList list = new BasicBSONList();

    list.add("hello");
    list.add("goodbye");
    list.add("have a nice day");

    body.put("body", list);
    root.put("root", body);

    String[] values = BSONUtilities.getValues(root,  "root.body");
    assertNotNull(values);
    assertEquals(values.length, 3);
}
 
Example #6
Source File: SchoolProficiencyReducer.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Override
public void reduce(final TenantAndIdEmittableKey pKey,
                   final Iterable<Text> pValues,
                   final Context context)
        throws IOException, InterruptedException {

    for (Text result : pValues) {
        count(result);
    }
    Logger.getLogger("SchoolProficiencyReducer").warning("writing reduce record to: " + pKey.toString());

    String field = context.getConfiguration().get(MongoAggFormatter.UPDATE_FIELD);
    BSONObject obj = BSONUtilities.setValue(field, counts);
    BSONWritable output = new BSONWritable(obj);
    context.write(pKey, output);
}
 
Example #7
Source File: SchoolProficiencyMapper.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected void setup(Mapper.Context context) throws IOException, InterruptedException {
    super.setup(context);

    ConfigSections cfg = JobConfiguration.fromHadoopConfiguration(context.getConfiguration());
    MetadataConfig meta = cfg.getMetadata();
    bands = meta.getCutPoints();

    BSONObject obj = MongoConfigUtil.getFields(context.getConfiguration());
    if (obj != null) {
        fields = obj.keySet().toArray(new String[0]);
    } else {
        throw new IllegalArgumentException("Invalid configuration found. Aggregates must "
            + "specify a the hadoop.map.fields property.");
    }
}
 
Example #8
Source File: RascalMetricsTest.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private JSONArray getJSONArrayFromDump(File dump) {
	try {
		JSONArray result = new JSONArray(); 
		InputStream is = new FileInputStream(dump);
		BSONDecoder decoder = new BasicBSONDecoder();

		while(is.available() > 0) {
			BSONObject obj = decoder.readObject(is);
			if(obj == null) {
				break;
			}
			else {
				JSONObject json = new JSONObject(JSON.serialize(obj));
				result.put(json);
			}
		}
		is.close();
		return result;
	}
	catch(IOException e) {
		System.out.println("We got an error when parsing a BSON file: " + e);
		return null;
	}
}
 
Example #9
Source File: BSONValueLookupTest.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetValue() {
    // root.body.profile.name.first = George
    BSONObject root = new BasicBSONObject();
    BSONObject body = new BasicBSONObject();
    BSONObject profile = new BasicBSONObject();
    BSONObject name = new BasicBSONObject();
    BSONObject first = new BasicBSONObject();

    first.put("first", "George");
    name.put("name", first);
    profile.put("profile", name);
    body.put("body", profile);
    root.put("root", body);

    assertEquals(BSONUtilities.getValue(root, "root.body.profile.name.first"), "George");
}
 
Example #10
Source File: BasicTranslator.java    From zerowing with MIT License 6 votes vote down vote up
@Override
public byte[] createRowKey(BSONObject object) {
  Object id = object.get("_id");

  byte[] raw;
  if (id instanceof ObjectId) {
    raw = ((ObjectId) id).toByteArray();
  } else if (id instanceof String) {
    raw = ((String) id).getBytes();
  } else if (id instanceof BSONObject) {
    raw = bsonEncoder.encode(((BSONObject) id));
  } else {
    throw new RuntimeException("Don't know how to serialize _id: " + id.toString());
  }

  return DigestUtils.md5(raw);
}
 
Example #11
Source File: MongoAggWriterTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
protected void testWrite(final T data) throws IOException {

        Mockito
            .when(
                mockCollection.findAndModify(Matchers.any(DBObject.class),
                    Matchers.any(DBObject.class))).thenAnswer(new Answer<DBObject>() {
                @Override
                public DBObject answer(InvocationOnMock inv) {
                    Object[] args = inv.getArguments();

                    // Expect 2 objects -- key and value
                    assertTrue(args.length == 2);

                    // Both should be BSONObject types
                    assertTrue(args[0] instanceof BSONObject);
                    assertTrue(args[1] instanceof BSONObject);

                    BSONObject arg1 = (BSONObject) args[1];

                    // value is a single value
                    BSONObject s = (BSONObject) arg1.get("$set");
                    assertNotNull(s);
                    assertEquals(s.get("testKey"), data);

                    return null;
                }
            });

        BSONWritable value = new BSONWritable();
        value.put("testKey", data);
        writer.write(key, value);
    }
 
Example #12
Source File: ValueMapperTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testMapValueNotFound() throws Exception {
    TenantAndIdEmittableKey key = new TenantAndIdEmittableKey();
    ValueMapper m = new MockValueMapper();
    BSONObject entry = new BasicBSONObject("not_found", "data");
    BSONWritable entity = new BSONWritable(entry);

    Context context = Mockito.mock(Context.class);
    PowerMockito.when(context, "write", Matchers.any(TenantAndIdEmittableKey.class),
        Matchers.any(BSONObject.class)).thenAnswer(new Answer<BSONObject>() {

        @Override
        public BSONObject answer(InvocationOnMock invocation) throws Throwable {

            Object[] args = invocation.getArguments();

            assertNotNull(args);
            assertEquals(args.length, 2);

            assertTrue(args[0] instanceof TenantAndIdEmittableKey);
            assertTrue(args[1] instanceof NullWritable);

            return null;
        }
    });

    m.map(key, entity, context);
}
 
Example #13
Source File: LongValueMapperTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueNotFound() {
    BSONObject field = new BasicBSONObject("field", 123L);
    BSONObject entry = new BasicBSONObject("long", field);
    BSONWritable entity = new BSONWritable(entry);

    LongValueMapper mapper = new LongValueMapper("long.missing_field");

    Writable value = mapper.getValue(entity);
    assertTrue(value instanceof NullWritable);
}
 
Example #14
Source File: StringValueMapperTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueNotFound() {
    BSONObject field = new BasicBSONObject("field", "testing123");
    BSONObject entry = new BasicBSONObject("string", field);
    BSONWritable entity = new BSONWritable(entry);

    StringValueMapper mapper = new StringValueMapper("string.missing_field");

    Writable value = mapper.getValue(entity);
    assertTrue(value instanceof NullWritable);
}
 
Example #15
Source File: StringValueMapperTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetValue() {
    BSONObject field = new BasicBSONObject("field", "testing123");
    BSONObject entry = new BasicBSONObject("string", field);
    BSONWritable entity = new BSONWritable(entry);

    StringValueMapper mapper = new StringValueMapper("string.field");

    Writable value = mapper.getValue(entity);
    assertFalse(value instanceof NullWritable);
    assertTrue(value instanceof Text);
    assertEquals(value.toString(), "testing123");
}
 
Example #16
Source File: BasicTranslator.java    From zerowing with MIT License 5 votes vote down vote up
@Override
public Put createPut(byte[] row, BSONObject object) {
  byte[] raw = bsonEncoder.encode(object);
  Put put = new Put(row);
  put.add(COL_FAMILY, COL_QUALIFIER, raw);

  return put;
}
 
Example #17
Source File: DoubleValueMapperTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetValueNotDouble() {
    BSONObject field = new BasicBSONObject("field", "Bob");
    BSONObject entry = new BasicBSONObject("double", field);
    BSONWritable entity = new BSONWritable(entry);

    DoubleValueMapper mapper = new DoubleValueMapper("double.field");

    Writable value = mapper.getValue(entity);
    assertTrue(value instanceof NullWritable);
}
 
Example #18
Source File: OpInsert.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public OpReply doOp( MongoChannelHandler handler, ChannelHandlerContext ctx, MessageEvent messageEvent ) {

    ApplicationInfo application = SubjectUtils.getApplication( Identifier.from( getDatabaseName() ) );

    if ( application == null ) {
        ctx.setAttachment( new IllegalArgumentException(
                String.format( "Could not find application with name '%s' ", getDatabaseName() ) ) );
        return null;
    }


    EntityManager em = handler.getEmf().getEntityManager( application.getId() );


    for ( BSONObject document : documents ) {
        try {
            //special case to serialize mongo ObjectId if required
            Object id = document.get( "_id" );

            if ( id instanceof ObjectId ) {
                document.put( "_id", ( ( ObjectId ) id ).toStringMongod() );
            }

            em.create( getCollectionName(), document.toMap() );
        }
        catch ( Exception e ) {
            logger.error( "Unable to insert mongo document {}", document, e );
            ctx.setAttachment( e );
        }
    }

    //insert never returns a response in mongo
    return null;
}
 
Example #19
Source File: OpReply.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public void setDocuments( List<BSONObject> documents ) {
    if ( documents == null ) {
        documents = new ArrayList<BSONObject>();
    }
    this.documents = documents;
    numberReturned = documents.size();
}
 
Example #20
Source File: MongoTenantAndIdRecordReader.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
public EmittableKey getCurrentKey() {
    BSONObject obj = privateReader.getCurrentValue();

    String id = BSONUtilities.getValue(obj, keyField);
    String tenantId = BSONUtilities.getValue(obj, "metaData.tenantId");

    TenantAndIdEmittableKey rval = new TenantAndIdEmittableKey();
    rval.setId(new Text(id));
    rval.setTenantId(new Text(tenantId));
    return rval;
}
 
Example #21
Source File: EnumValueMapperTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetValue() {

    BSONObject field = new BasicBSONObject("field", "TEST1");
    BSONObject entry = new BasicBSONObject("enum", field);
    BSONWritable entity = new BSONWritable(entry);

    EnumValueMapper<Testing> m = new EnumValueMapper<Testing>("enum.field", Testing.class);

    Writable value = m.getValue(entity);
    assertFalse(value instanceof NullWritable);
    assertTrue(value instanceof Text);
    assertEquals(((Text) value).toString(), Testing.TEST1.toString());
}
 
Example #22
Source File: MongoIdRecordReader.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
public EmittableKey getCurrentKey() {
    BSONObject obj = privateReader.getCurrentValue();

    String id = BSONUtilities.getValue(obj, keyField);

    IdFieldEmittableKey rval = new IdFieldEmittableKey(keyField);
    rval.setId(new Text(id));
    return rval;
}
 
Example #23
Source File: DoubleValueMapperTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testValueNotFound() {
    BSONObject field = new BasicBSONObject("field", 1.312D);
    BSONObject entry = new BasicBSONObject("double", field);
    BSONWritable entity = new BSONWritable(entry);

    DoubleValueMapper mapper = new DoubleValueMapper("double.missing_field");

    Writable value = mapper.getValue(entity);
    assertTrue(value instanceof NullWritable);
}
 
Example #24
Source File: MongoAggWriterTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testKey() {
    Mockito
        .when(
            mockCollection.findAndModify(Matchers.any(DBObject.class),
                Matchers.any(DBObject.class))).thenAnswer(new Answer<DBObject>() {
            @Override
            public DBObject answer(InvocationOnMock inv) {
                Object[] args = inv.getArguments();

                // Expect 2 objects -- key and value
                assertTrue(args.length == 2);

                // Both should be BSONObject types
                assertTrue(args[0] instanceof BSONObject);
                assertTrue(args[1] instanceof BSONObject);

                BSONObject arg0 = (BSONObject) args[0];

                // Key should contain two entries: tenant and id
                assertTrue(arg0.containsField("testTenant"));
                assertTrue(arg0.get("testTenant").toString().equals("Midgar"));
                assertTrue(arg0.containsField("testId"));
                assertTrue(arg0.get("testId").toString().equals("abcdefg01234567890"));

                return null;
            }
        });
}
 
Example #25
Source File: TenantAndIdEmittableKey.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
public BSONObject toBSON() {
    BSONObject bson = new BasicBSONObject();
    bson.put(getTenantIdField().toString(), getTenantId().toString());
    bson.put(getIdField().toString(), getId().toString());
    return bson;
}
 
Example #26
Source File: BulkImportMapper.java    From zerowing with MIT License 5 votes vote down vote up
public void map(Object key, BSONObject value, Context context) throws IOException, InterruptedException {
  byte[] row = _translator.createRowKey(value);
  Put put = _translator.createPut(row, value);

  ImmutableBytesWritable outKey = new ImmutableBytesWritable(row);
  context.write(outKey, put);
}
 
Example #27
Source File: MapReduceExercise.java    From mongodb-hadoop-workshop with Apache License 2.0 5 votes vote down vote up
@Override
public void map(final Object key, final BSONObject doc, final Context context)
  throws IOException, InterruptedException {
    final int movieId = (Integer)doc.get("movieid");
    final Number movieRating = (Number)doc.get("rating");

    context.write(new IntWritable(movieId), new DoubleWritable(movieRating.doubleValue()));
}
 
Example #28
Source File: UtilMongoDB.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * converts from BsonObject to cell class with deep's anotations
 *
 * @param bsonObject the bson object
 * @param tableName  the table name
 * @return cell from bson
 * @throws IllegalAccessException the illegal access exception
 * @throws IllegalAccessException the instantiation exception
 * @throws IllegalAccessException the invocation target exception
 */
public static Cells getCellFromBson(BSONObject bsonObject, String tableName) {

    Cells cells = tableName != null ? new Cells(tableName) : new Cells();

    Map<String, Object> map = bsonObject.toMap();

    Set<Map.Entry<String, Object>> entryBson = map.entrySet();

    for (Map.Entry<String, Object> entry : entryBson) {
        try {

            if (List.class.isAssignableFrom(entry.getValue().getClass())) {
                List innerCell = new ArrayList<>();
                for (Object innerBson : (List)entry.getValue()) {
                    if(innerBson instanceof DBObject){
                        innerCell.add(getCellFromBson((DBObject)innerBson, null));
                    }else{
                        innerCell.add(innerBson);
                    }

                }
                cells.add(Cell.create(entry.getKey(), innerCell));
            } else if (BSONObject.class.isAssignableFrom(entry.getValue().getClass())) {
                Cells innerCells = getCellFromBson((BSONObject) entry.getValue(), null);
                cells.add(Cell.create(entry.getKey(), innerCells));
            } else {
                cells.add(Cell.create(entry.getKey(), entry.getValue()));
            }
        } catch (IllegalArgumentException e) {
            LOG.error("impossible to create a java cell from Bson field:" + entry.getKey() + ", type:" + entry
                    .getValue().getClass() + ", value:" + entry.getValue());
        }

    }
    return cells;
}
 
Example #29
Source File: UtilMongoDB.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Sub document list case.
 *
 * @param <T>       the type parameter
 * @param type      the type
 * @param bsonOject the bson oject
 * @return the object
 * @throws IllegalAccessException    the illegal access exception
 * @throws InstantiationException    the instantiation exception
 * @throws InvocationTargetException the invocation target exception
 */
private static <T> Object subDocumentListCase(Type type, List<T> bsonOject)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    ParameterizedType listType = (ParameterizedType) type;

    Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];

    List list = new ArrayList();
    for (T t : bsonOject) {
        list.add(getObjectFromBson(listClass, (BSONObject) t));
    }

    return list;
}
 
Example #30
Source File: BSONValueLookupTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetValue() {

    BSONObject root = BSONUtilities.setValue("body.profile.name.first", "George");
    assertNotNull(root);
    BSONObject body = (BSONObject) root.get("body");
    assertNotNull(body);
    BSONObject profile = (BSONObject) body.get("profile");
    assertNotNull(profile);
    BSONObject name = (BSONObject) profile.get("name");
    assertNotNull(name);
    assertNotNull(name.get("first"));
    assertEquals(name.get("first"), "George");
}