Search in sources :

Example 36 with BasicBSONObject

use of org.bson.BasicBSONObject in project v7files by thiloplanz.

the class V7FileTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // the root folder
    prepareMockData("test.v7files.files", new BasicBSONObject("_id", "root"));
    gridFS = new V7GridFS(getMongo().getDB("test"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject)

Example 37 with BasicBSONObject

use of org.bson.BasicBSONObject in project Mycat-Server by MyCATApache.

the class SequoiaSQLParser method UpData.

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;
}
Also used : DBCollection(com.sequoiadb.base.DBCollection) BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 38 with BasicBSONObject

use of org.bson.BasicBSONObject in project Mycat-Server by MyCATApache.

the class SequoiaSQLParser method parserDBObject.

private void parserDBObject(BasicBSONObject ob, String akey, String aop, Object aval) {
    boolean isok = false;
    if (!(ob.keySet().isEmpty())) {
        for (String field : ob.keySet()) {
            if (akey.equals(field)) {
                Object val = ob.get(field);
                if (val instanceof BasicBSONObject) {
                    ((BasicBSONObject) val).put(aop, aval);
                    ob.put(field, (BasicBSONObject) val);
                    isok = true;
                    break;
                } else if (val instanceof BasicBSONList) {
                // newobj.put(field, ((BasicDBList)val).copy());
                }
            }
        }
    }
    if (isok == false) {
        BasicBSONObject xo = new BasicBSONObject();
        xo.put(aop, aval);
        ob.put(akey, xo);
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONList(org.bson.types.BasicBSONList) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject)

Example 39 with BasicBSONObject

use of org.bson.BasicBSONObject in project Mycat-Server by MyCATApache.

the class SequoiaSQLParser method query.

public SequoiaData query() throws SequoiaSQLException {
    if (!(statement instanceof SQLSelectStatement)) {
        // return null;
        throw new IllegalArgumentException("not a query sql statement");
    }
    SequoiaData mongo = new SequoiaData();
    DBCursor c = null;
    SQLSelectStatement selectStmt = (SQLSelectStatement) statement;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    int icount = 0;
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        BasicBSONObject fields = new BasicBSONObject();
        // 显示的字段
        for (SQLSelectItem item : mysqlSelectQuery.getSelectList()) {
            // System.out.println(item.toString());
            if (!(item.getExpr() instanceof SQLAllColumnExpr)) {
                if (item.getExpr() instanceof SQLAggregateExpr) {
                    SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr();
                    if (expr.getMethodName().equals("COUNT")) {
                        icount = 1;
                        mongo.setField(getExprFieldName(expr), Types.BIGINT);
                    }
                    fields.put(getExprFieldName(expr), Integer.valueOf(1));
                } else {
                    fields.put(getFieldName(item), Integer.valueOf(1));
                }
            }
        }
        // 表名
        SQLTableSource table = mysqlSelectQuery.getFrom();
        DBCollection coll = this._db.getCollection(table.toString());
        mongo.setTable(table.toString());
        SQLExpr expr = mysqlSelectQuery.getWhere();
        BSONObject query = parserWhere(expr);
        // System.out.println(query);
        SQLSelectGroupByClause groupby = mysqlSelectQuery.getGroupBy();
        BasicBSONObject gbkey = new BasicBSONObject();
        if (groupby != null) {
            for (SQLExpr gbexpr : groupby.getItems()) {
                if (gbexpr instanceof SQLIdentifierExpr) {
                    String name = ((SQLIdentifierExpr) gbexpr).getName();
                    gbkey.put(name, Integer.valueOf(1));
                }
            }
            icount = 2;
        }
        int limitoff = 0;
        int limitnum = 0;
        if (mysqlSelectQuery.getLimit() != null) {
            limitoff = getSQLExprToInt(mysqlSelectQuery.getLimit().getOffset());
            limitnum = getSQLExprToInt(mysqlSelectQuery.getLimit().getRowCount());
        }
        SQLOrderBy orderby = mysqlSelectQuery.getOrderBy();
        BasicBSONObject order = new BasicBSONObject();
        if (orderby != null) {
            for (int i = 0; i < orderby.getItems().size(); i++) {
                SQLSelectOrderByItem orderitem = orderby.getItems().get(i);
                order.put(orderitem.getExpr().toString(), Integer.valueOf(getSQLExprToAsc(orderitem.getType())));
            }
        // c.sort(order);
        // System.out.println(order);
        }
        if (icount == 1) {
            mongo.setCount(coll.getCount(query));
        } else if (icount == 2) {
            BasicBSONObject initial = new BasicBSONObject();
            initial.put("num", 0);
            String reduce = "function (obj, prev) { " + "  prev.num++}";
        // mongo.setGrouyBy(coll.group(gbkey, query, initial, reduce));
        } else {
            if ((limitoff > 0) || (limitnum > 0)) {
                // .skip(limitoff).limit(limitnum);
                c = coll.query(query, fields, order, null, limitoff, limitnum);
            } else {
                c = coll.query(query, fields, order, null, 0, -1);
            }
        }
        mongo.setCursor(c);
    }
    return mongo;
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) BasicBSONObject(org.bson.BasicBSONObject) DBCollection(com.sequoiadb.base.DBCollection) DBCursor(com.sequoiadb.base.DBCursor)

Example 40 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-java-driver by mongodb.

the class BasicDBObjectTest method testEqualsAndHashCode.

@Test
public void testEqualsAndHashCode() {
    assertEquality(new BasicDBObject(), new BasicDBObject());
    assertEquality(new BasicDBObject("x", 1), new BasicDBObject("x", 1));
    assertEquality(new BasicDBObject("x", 1), new BasicBSONObject("x", 1));
    assertInequality(new BasicDBObject("x", 1), new BasicDBObject("x", 2));
    assertInequality(new BasicDBObject("x", 1), new BasicBSONObject("x", 2));
    assertInequality(new BasicDBObject("x", 1), new BasicDBObject("y", 1));
    assertInequality(new BasicDBObject("x", 1), new BasicBSONObject("y", 1));
    assertEquality(new BasicDBObject("x", asList(1, 2, 3)), new BasicDBObject("x", new int[] { 1, 2, 3 }));
    assertEquality(new BasicDBObject("x", asList(1, 2, 3)), new BasicBSONObject("x", asList(1, 2, 3)));
    BasicDBList list = new BasicDBList();
    list.put(0, 1);
    list.put(1, 2);
    list.put(2, 3);
    assertEquality(new BasicDBObject("x", asList(1, 2, 3)), new BasicDBObject("x", list));
    assertEquality(new BasicDBObject("x", asList(1, 2, 3)), new BasicBSONObject("x", list));
    assertEquality(new BasicDBObject("x", 1).append("y", 2), new BasicDBObject("y", 2).append("x", 1));
    assertEquality(new BasicDBObject("x", 1).append("y", 2), new BasicBSONObject("y", 2).append("x", 1));
    assertEquality(new BasicDBObject("a", new BasicDBObject("y", 2).append("x", 1)), new BasicDBObject("a", new BasicDBObject("x", 1).append("y", 2)));
    assertEquality(new BasicDBObject("a", new BasicDBObject("y", 2).append("x", 1)), new BasicBSONObject("a", new BasicBSONObject("x", 1).append("y", 2)));
    assertEquality(new BasicDBObject("a", asList(new BasicDBObject("y", 2).append("x", 1))), new BasicDBObject("a", asList(new BasicDBObject("x", 1).append("y", 2))));
    assertEquality(new BasicDBObject("a", asList(new BasicDBObject("y", 2).append("x", 1))), new BasicBSONObject("a", asList(new BasicBSONObject("x", 1).append("y", 2))));
    assertEquality(new BasicDBObject("a", new BasicDBList().put(1, new BasicDBObject("y", 2).append("x", 1))), new BasicDBObject("a", new BasicDBList().put(1, new BasicDBObject("x", 1).append("y", 2))));
    assertEquality(new BasicDBObject("a", new BasicDBList().put(1, new BasicDBObject("y", 2).append("x", 1))), new BasicBSONObject("a", new BasicBSONList().put(1, new BasicBSONObject("x", 1).append("y", 2))));
    Map<String, Object> first = new HashMap<String, Object>();
    first.put("1", new BasicDBObject("y", 2).append("x", 1));
    first.put("2", new BasicDBObject("a", 2).append("b", 1));
    Map<String, Object> second = new TreeMap<String, Object>();
    second.put("2", new BasicDBObject("b", 1).append("a", 2));
    second.put("1", new BasicDBObject("x", 1).append("y", 2));
    Map<String, Object> third = new TreeMap<String, Object>();
    third.put("2", new BasicBSONObject("a", 2).append("b", 1));
    third.put("1", new BasicBSONObject("x", 1).append("y", 2));
    assertEquality(new BasicDBObject("a", first), new BasicDBObject("a", second));
    assertEquality(new BasicDBObject("a", first), new BasicBSONObject("a", third));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) HashMap(java.util.HashMap) BasicBSONList(org.bson.types.BasicBSONList) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)88 Test (org.junit.Test)39 BSONObject (org.bson.BSONObject)37 ArrayList (java.util.ArrayList)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)13 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)13 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 BasicDBObject (com.mongodb.BasicDBObject)11 ObjectId (org.bson.types.ObjectId)11 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DataBag (org.apache.pig.data.DataBag)6 Map (java.util.Map)5 Tuple (org.apache.pig.data.Tuple)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4