数据库中间件 MyCAT 源码分析 —— SQL ON MongoDB

数据库 其他数据库 MongoDB
Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC也是Sun Microsystems的商标。JDBC是面向关系型数据库的。
  • 1. 概述
  • 2. 主流程
  • 3. 查询操作
  • 4. 插入操作

1. 概述

可能你在看到这个标题会小小的吃惊,MyCAT 能使用 MongoDB 做数据节点。是的,没错,确实可以。

吼吼吼,让我们开启这段神奇的“旅途”。

本文主要分成四部分:

  1. 总体流程,让你有个整体的认识
  2. 查询操作
  3. 插入操作

2. 主流程

MyCAT Server 接收 MySQL Client 基于 MySQL协议 的请求,翻译 SQL 成 MongoDB操作 发送给 MongoDB Server。

MyCAT Server 接收 MongoDB Server 返回的 MongoDB数据,翻译成 MySQL数据结果 返回给 MySQL Client。

这样一看,MyCAT 连接 MongoDB 是不是少神奇一点列。

Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC也是Sun Microsystems的商标。JDBC是面向关系型数据库的。

MyCAT 使用 JDBC 规范,抽象了对 MongoDB 的访问。通过这样的方式,MyCAT 也抽象了 SequoiaDB 的访问。可能这样说法有些抽象,看个类图压压惊。

是不是熟悉的味道。不得不说 JDBC 规范的精妙。

3. 查询操作

  1. SELECT id, name FROM user WHERE name > '' ORDER BY _id DESC

看顺序图已经很方便的理解整体逻辑,我就不多废话啦。我们来看几个核心的代码逻辑。

1、查询 MongoDB

  1. // MongoSQLParser.java 
  2. public MongoData query() throws MongoSQLException { 
  3.    if (!(statement instanceof SQLSelectStatement)) { 
  4.        //return null
  5.        throw new IllegalArgumentException("not a query sql statement"); 
  6.    } 
  7.    MongoData mongo = new MongoData(); 
  8.    DBCursor c = null
  9.    SQLSelectStatement selectStmt = (SQLSelectStatement) statement; 
  10.    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery(); 
  11.    int icount = 0; 
  12.    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) { 
  13.        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery(); 
  14.  
  15.        BasicDBObject fields = new BasicDBObject(); 
  16.  
  17.        // 显示(返回)的字段 
  18.        for (SQLSelectItem item : mysqlSelectQuery.getSelectList()) { 
  19.            //System.out.println(item.toString()); 
  20.            if (!(item.getExpr() instanceof SQLAllColumnExpr)) { 
  21.                if (item.getExpr() instanceof SQLAggregateExpr) { 
  22.                    SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr(); 
  23.                    if (expr.getMethodName().equals("COUNT")) { // TODO 待读:count(*) 
  24.                        icount = 1; 
  25.                        mongo.setField(getExprFieldName(expr), Types.BIGINT); 
  26.                    } 
  27.                    fields.put(getExprFieldName(expr), 1); 
  28.                } else { 
  29.                    fields.put(getFieldName(item), 1); 
  30.                } 
  31.            } 
  32.  
  33.        } 
  34.  
  35.        // 表名 
  36.        SQLTableSource table = mysqlSelectQuery.getFrom(); 
  37.        DBCollection coll = this._db.getCollection(table.toString()); 
  38.        mongo.setTable(table.toString()); 
  39.  
  40.        // WHERE 
  41.        SQLExpr expr = mysqlSelectQuery.getWhere(); 
  42.        DBObject query = parserWhere(expr); 
  43.  
  44.        // GROUP BY 
  45.        SQLSelectGroupByClause groupby = mysqlSelectQuery.getGroupBy(); 
  46.        BasicDBObject gbkey = new BasicDBObject(); 
  47.        if (groupby != null) { 
  48.            for (SQLExpr gbexpr : groupby.getItems()) { 
  49.                if (gbexpr instanceof SQLIdentifierExpr) { 
  50.                    String name = ((SQLIdentifierExpr) gbexpr).getName(); 
  51.                    gbkey.put(nameInteger.valueOf(1)); 
  52.                } 
  53.            } 
  54.            icount = 2; 
  55.        } 
  56.  
  57.        // SKIP / LIMIT 
  58.        int limitoff = 0; 
  59.        int limitnum = 0; 
  60.        if (mysqlSelectQuery.getLimit() != null) { 
  61.            limitoff = getSQLExprToInt(mysqlSelectQuery.getLimit().getOffset()); 
  62.            limitnum = getSQLExprToInt(mysqlSelectQuery.getLimit().getRowCount()); 
  63.        } 
  64.        if (icount == 1) { // COUNT(*) 
  65.            mongo.setCount(coll.count(query)); 
  66.        } else if (icount == 2) { // MapReduce 
  67.            BasicDBObject initial = new BasicDBObject(); 
  68.            initial.put("num", 0); 
  69.            String reduce = "function (obj, prev) { " + "  prev.num++}"
  70.            mongo.setGrouyBy(coll.group(gbkey, query, initial, reduce)); 
  71.        } else { 
  72.            if ((limitoff > 0) || (limitnum > 0)) { 
  73.                c = coll.find(query, fields).skip(limitoff).limit(limitnum); 
  74.            } else { 
  75.                c = coll.find(query, fields); 
  76.            } 
  77.  
  78.            // order by 
  79.            SQLOrderBy orderby = mysqlSelectQuery.getOrderBy(); 
  80.            if (orderby != null) { 
  81.                BasicDBObject order = new BasicDBObject(); 
  82.                for (int i = 0; i < orderby.getItems().size(); i++) { 
  83.                    SQLSelectOrderByItem orderitem = orderby.getItems().get(i); 
  84.                    order.put(orderitem.getExpr().toString(), getSQLExprToAsc(orderitem.getType())); 
  85.                } 
  86.                c.sort(order); 
  87.                // System.out.println(order); 
  88.            } 
  89.        } 
  90.        mongo.setCursor(c); 
  91.    } 
  92.    return mongo; 

2、查询条件

  1. // MongoSQLParser.java 
  2. private void parserWhere(SQLExpr aexpr, BasicDBObject o) { 
  3.    if (aexpr instanceof SQLBinaryOpExpr) { 
  4.        SQLBinaryOpExpr expr = (SQLBinaryOpExpr) aexpr; 
  5.        SQLExpr exprL = expr.getLeft(); 
  6.        if (!(exprL instanceof SQLBinaryOpExpr)) { 
  7.            if (expr.getOperator().getName().equals("=")) { 
  8.                o.put(exprL.toString(), getExpValue(expr.getRight())); 
  9.            } else { 
  10.                String op = ""
  11.                if (expr.getOperator().getName().equals("<")) { 
  12.                    op = "$lt"
  13.                } else if (expr.getOperator().getName().equals("<=")) { 
  14.                    op = "$lte"
  15.                } else if (expr.getOperator().getName().equals(">")) { 
  16.                    op = "$gt"
  17.                } else if (expr.getOperator().getName().equals(">=")) { 
  18.                    op = "$gte"
  19.                } else if (expr.getOperator().getName().equals("!=")) { 
  20.                    op = "$ne"
  21.                } else if (expr.getOperator().getName().equals("<>")) { 
  22.                    op = "$ne"
  23.                } 
  24.                parserDBObject(o, exprL.toString(), op, getExpValue(expr.getRight())); 
  25.            } 
  26.        } else { 
  27.            if (expr.getOperator().getName().equals("AND")) { 
  28.                parserWhere(exprL, o); 
  29.                parserWhere(expr.getRight(), o); 
  30.            } else if (expr.getOperator().getName().equals("OR")) { 
  31.                orWhere(exprL, expr.getRight(), o); 
  32.            } else { 
  33.                throw new RuntimeException("Can't identify the operation of  of where"); 
  34.            } 
  35.        } 
  36.    } 
  37.  
  38. private void orWhere(SQLExpr exprL, SQLExpr exprR, BasicDBObject ob) { 
  39.    BasicDBObject xo = new BasicDBObject(); 
  40.    BasicDBObject yo = new BasicDBObject(); 
  41.    parserWhere(exprL, xo); 
  42.    parserWhere(exprR, yo); 
  43.    ob.put("$or", new Object[]{xo, yo}); 

3、解析 MongoDB 数据

  1. // MongoResultSet.java 
  2. public MongoResultSet(MongoData mongo, String schema) throws SQLException { 
  3.    this._cursor = mongo.getCursor(); 
  4.    this._schema = schema
  5.    this._table = mongo.getTable(); 
  6.    this.isSum = mongo.getCount() > 0; 
  7.    this._sum = mongo.getCount(); 
  8.    this.isGroupBy = mongo.getType(); 
  9.  
  10.    if (this.isGroupBy) { 
  11.        dblist = mongo.getGrouyBys(); 
  12.        this.isSum = true
  13.    } 
  14.    if (this._cursor != null) { 
  15.        select = _cursor.getKeysWanted().keySet().toArray(new String[0]); 
  16.        // 解析 fields 
  17.        if (this._cursor.hasNext()) { 
  18.            _cur = _cursor.next(); 
  19.            if (_cur != null) { 
  20.                if (select.length == 0) { 
  21.                    SetFields(_cur.keySet()); 
  22.                } 
  23.                _row = 1; 
  24.            } 
  25.        } 
  26.        // 设置 fields 类型 
  27.        if (select.length == 0) { 
  28.            select = new String[]{"_id"}; 
  29.            SetFieldType(true); 
  30.        } else { 
  31.            SetFieldType(false); 
  32.        } 
  33.    } else { 
  34.        SetFields(mongo.getFields().keySet());//new String[]{"COUNT(*)"}; 
  35.        SetFieldType(mongo.getFields()); 
  36.    } 
  • 当使用 SELECT * 查询字段时,fields 使用***条数据返回的 fields。即使,后面的数据有其他 fields,也不返回。

4、返回数据给 MySQL Client

  1. // JDBCConnection.java 
  2. private void ouputResultSet(ServerConnection sc, String sql) 
  3.        throws SQLException { 
  4.    ResultSet rs = null
  5.    Statement stmt = null
  6.  
  7.    try { 
  8.        stmt = con.createStatement(); 
  9.        rs = stmt.executeQuery(sql); 
  10.  
  11.        // header 
  12.        List<FieldPacket> fieldPks = new LinkedList<>(); 
  13.        ResultSetUtil.resultSetToFieldPacket(sc.getCharset(), fieldPks, rs, this.isSpark); 
  14.        int colunmCount = fieldPks.size(); 
  15.        ByteBuffer byteBuf = sc.allocate(); 
  16.        ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket(); 
  17.        headerPkg.fieldCount = fieldPks.size(); 
  18.        headerPkg.packetId = ++packetId; 
  19.        byteBuf = headerPkg.write(byteBuf, sc, true); 
  20.        byteBuf.flip(); 
  21.        byte[] header = new byte[byteBuf.limit()]; 
  22.        byteBuf.get(header); 
  23.        byteBuf.clear(); 
  24.        List<byte[]> fields = new ArrayList<byte[]>(fieldPks.size()); 
  25.        for (FieldPacket curField : fieldPks) { 
  26.            curField.packetId = ++packetId; 
  27.            byteBuf = curField.write(byteBuf, sc, false); 
  28.            byteBuf.flip(); 
  29.            byte[] field = new byte[byteBuf.limit()]; 
  30.            byteBuf.get(field); 
  31.            byteBuf.clear(); 
  32.            fields.add(field); 
  33.        } 
  34.        // header eof 
  35.        EOFPacket eofPckg = new EOFPacket(); 
  36.        eofPckg.packetId = ++packetId; 
  37.        byteBuf = eofPckg.write(byteBuf, sc, false); 
  38.        byteBuf.flip(); 
  39.        byte[] eof = new byte[byteBuf.limit()]; 
  40.        byteBuf.get(eof); 
  41.        byteBuf.clear(); 
  42.        this.respHandler.fieldEofResponse(header, fields, eof, this); 
  43.  
  44.        // row 
  45.        while (rs.next()) { 
  46.            RowDataPacket curRow = new RowDataPacket(colunmCount); 
  47.            for (int i = 0; i < colunmCount; i++) { 
  48.                int j = i + 1; 
  49.                if (MysqlDefs.isBianry((byte) fieldPks.get(i).type)) { 
  50.                    curRow.add(rs.getBytes(j)); 
  51.                } else if (fieldPks.get(i).type == MysqlDefs.FIELD_TYPE_DECIMAL || 
  52.                        fieldPks.get(i).type == (MysqlDefs.FIELD_TYPE_NEW_DECIMAL - 256)) { // field type is unsigned byte 
  53.                    // ensure that do not use scientific notation format 
  54.                    BigDecimal val = rs.getBigDecimal(j); 
  55.                    curRow.add(StringUtil.encode(val != null ? val.toPlainString() : null, sc.getCharset())); 
  56.                } else { 
  57.                    curRow.add(StringUtil.encode(rs.getString(j), sc.getCharset())); 
  58.                } 
  59.            } 
  60.            curRow.packetId = ++packetId; 
  61.            byteBuf = curRow.write(byteBuf, sc, false); 
  62.            byteBuf.flip(); 
  63.            byte[] row = new byte[byteBuf.limit()]; 
  64.            byteBuf.get(row); 
  65.            byteBuf.clear(); 
  66.            this.respHandler.rowResponse(row, this); 
  67.        } 
  68.        fieldPks.clear(); 
  69.        // row eof 
  70.        eofPckg = new EOFPacket(); 
  71.        eofPckg.packetId = ++packetId; 
  72.        byteBuf = eofPckg.write(byteBuf, sc, false); 
  73.        byteBuf.flip(); 
  74.        eof = new byte[byteBuf.limit()]; 
  75.        byteBuf.get(eof); 
  76.        sc.recycle(byteBuf); 
  77.        this.respHandler.rowEofResponse(eof, this); 
  78.    } finally { 
  79.        if (rs != null) { 
  80.            try { 
  81.                rs.close(); 
  82.            } catch (SQLException e) { 
  83.            } 
  84.        } 
  85.        if (stmt != null) { 
  86.            try { 
  87.                stmt.close(); 
  88.            } catch (SQLException e) { 
  89.            } 
  90.        } 
  91.    } 
  92.  
  93. // MongoResultSet.java 
  94. @Override 
  95. public String getString(String columnLabel) throws SQLException { 
  96.    Object x = getObject(columnLabel); 
  97.    if (x == null) { 
  98.        return null
  99.    } 
  100.    return x.toString(); 

当返回字段值是 Object 时,返回该对象.toString()。例如:

  1. mysql> select * from user order by _id asc
  2. +--------------------------+------+-------------------------------+ 
  3. | _id                      | name | profile                       | 
  4. +--------------------------+------+-------------------------------+ 
  5. | 1                        | 123  | { "age" : 1 , "height" : 100} | 

4. 插入操作

 

  1. // MongoSQLParser.java 
  2. public int executeUpdate() throws MongoSQLException { 
  3.    if (statement instanceof SQLInsertStatement) { 
  4.        return InsertData((SQLInsertStatement) statement); 
  5.    } 
  6.    if (statement instanceof SQLUpdateStatement) { 
  7.        return UpData((SQLUpdateStatement) statement); 
  8.    } 
  9.    if (statement instanceof SQLDropTableStatement) { 
  10.        return dropTable((SQLDropTableStatement) statement); 
  11.    } 
  12.    if (statement instanceof SQLDeleteStatement) { 
  13.        return DeleteDate((SQLDeleteStatement) statement); 
  14.    } 
  15.    if (statement instanceof SQLCreateTableStatement) { 
  16.        return 1; 
  17.    } 
  18.    return 1; 
  19.  
  20. private int InsertData(SQLInsertStatement state) { 
  21.    if (state.getValues().getValues().size() == 0) { 
  22.        throw new RuntimeException("number of  columns error"); 
  23.    } 
  24.    if (state.getValues().getValues().size() != state.getColumns().size()) { 
  25.        throw new RuntimeException("number of values and columns have to match"); 
  26.    } 
  27.    SQLTableSource table = state.getTableSource(); 
  28.    BasicDBObject o = new BasicDBObject(); 
  29.    int i = 0; 
  30.    for (SQLExpr col : state.getColumns()) { 
  31.        o.put(getFieldName2(col), getExpValue(state.getValues().getValues().get(i))); 
  32.        i++; 
  33.    } 
  34.    DBCollection coll = this._db.getCollection(table.toString()); 
  35.    coll.insert(o); 
  36.    return 1; 
责任编辑:武晓燕 来源: oschina博客
相关推荐

2017-07-18 17:35:16

数据库MyCATPreparedSta

2017-07-18 17:07:40

数据库 MyCATJoin

2017-12-01 05:04:32

数据库中间件Atlas

2017-11-27 05:06:42

数据库中间件cobar

2017-11-27 05:36:16

数据库中间件TDDL

2018-02-24 19:37:33

Java8数据库中间件

2009-01-20 10:45:55

Oracle数据库中间件

2011-08-10 13:03:58

CJDBC数据库集群

2017-05-23 18:55:05

mysql-proxy数据库架构

2020-04-10 17:00:33

Mycat分库分表SpringBoot

2017-12-11 13:30:49

Go语言数据库中间件

2017-12-01 05:40:56

数据库中间件join

2017-11-27 06:01:37

数据库中间件中间层

2017-11-30 08:56:14

数据库中间件架构师

2017-11-03 11:02:08

数据库中间件

2020-10-15 08:34:32

数据库中间件漫谈

2021-07-27 05:49:59

MySQL数据库中间件

2019-05-13 15:00:14

MySQLMyCat数据库

2018-11-07 15:30:19

数据库NewSQLNoSQL

2022-04-01 10:55:30

数据库混合云建设
点赞
收藏

51CTO技术栈公众号