Beispiel #1
0
def exec_query(stmt):

    my_lexer = lex(module=lexer, optimize=True, debug=True)

    my_parser = yacc(debug=True, module=parser)

    val = my_parser.parse(lexer=my_lexer.clone(), debug=False, input=sql)

    es = Elasticsearch([{'host': "10.68.23.84", "port": 9200}])
    if val.get_type() == TK.TOK_QUERY:

        query = Query(val)

        print(query.dsl())

        print(query._index, query._type)

        res = es.search(index=query._index,
                        doc_type=query._type,
                        body=query.dsl(),
                        request_timeout=100)

        stmt_res = response_hits(res)

        print(json.dumps(stmt_res, indent=4))

    elif val.get_type() == TK.TOK_CREATE_TABLE:

        stmt = Create(val)

        res = es.indices.create(index=stmt._index,
                                body=stmt._options,
                                request_timeout=100,
                                ignore=400)

        res = es.indices.put_mapping(index=stmt._index,
                                     doc_type=stmt._type,
                                     body=stmt.dsl(),
                                     request_timeout=100)

        print(json.dumps(res, indent=4))

    elif val.get_type() == TK.TOK_INSERT_INTO:

        #         val.debug()

        stmt = Insert(val)

        parms = stmt.metas

        res = es.index(index=stmt._index,
                       doc_type=stmt._type,
                       body=stmt.dsl(),
                       **parms)

        print(json.dumps(res, indent=4))

    elif val.get_type() == TK.TOK_BULK_INTO:

        #         val.debug()

        stmt = Bulk(val)

        res = es.bulk(index=stmt._index, doc_type=stmt._type, body=stmt.dsl())

        print(json.dumps(res, indent=4))

    elif val.get_type() == TK.TOK_UPDATE:

        val.debug()

        stmt = Update(val)

        print(json.dumps(stmt.dsl(), indent=4))

        res = es.update(index=stmt._index,
                        doc_type=stmt._type,
                        body=stmt.dsl(),
                        **stmt.conditions)

        print(json.dumps(res, indent=4))

    elif val.get_type() == TK.TOK_UPSERT_INTO:

        val.debug()

        stmt = Upsert(val)

        print(json.dumps(stmt.dsl(), indent=4))

        res = es.update(index=stmt._index,
                        doc_type=stmt._type,
                        body=stmt.dsl(),
                        **stmt.conditions)

        print(json.dumps(res, indent=4))

    elif val.get_type() == TK.TOK_DELETE:

        val.debug()

        stmt = Delete(val)

        res = es.delete(index=stmt._index,
                        doc_type=stmt._type,
                        **stmt.conditions,
                        ignore=404)

        print(json.dumps(res, indent=4))

    elif val.get_type() == TK.TOK_EXPLAIN:
        stmt = Explain(val)
        print(stmt.curl_str)
        print(json.dumps(stmt.dsl(), indent=4))

    elif val.get_type() == TK.TOK_DESC_TABLE:

        stmt = Describe(val)

        res = es.indices.get_mapping(index=stmt._index, doc_type=stmt._type)

        print(res)

    else:
        res = es.cat.indices(index='qs_test*', v=True)
        val.debug()
        print(res)
Beispiel #2
0
 def _exec_explain(self, ast):
     try:
         stmt = Explain(ast)
     except Exception:
         return http_response_error('Parse statement to dsl error!')
     return http_response_nor(stmt.dsl(), 202)