Exemplo n.º 1
0
def _autocomplete(db, database=None, table=None, column=None, nested=None, query=None):
  response = {}

  try:
    if database is None:
      response['databases'] = db.get_databases()
    elif table is None:
      tables_meta = db.get_tables_meta(database=database)
      response['tables_meta'] = tables_meta
    elif column is None:
      if query is not None:
        table = SubQueryTable(db, query)
      else:
        table = db.get_table(database, table)
      response['hdfs_link'] = table.hdfs_link
      response['comment'] = table.comment

      cols_extended = massage_columns_for_json(table.cols)

      if 'org.apache.kudu.mapreduce.KuduTableOutputFormat' in str(table.properties): # When queries from Impala directly
        table.is_impala_only = True

      if table.is_impala_only: # Expand Kudu columns information
        query_server = get_query_server_config('impala')
        db = dbms.get(db.client.user, query_server)

        col_options = db.get_table_describe(database, table.name)
        extra_col_options = dict([(col[0], dict(zip(col_options.cols(), col))) for col in col_options.rows()])

        for col_props in cols_extended:
          col_props.update(extra_col_options.get(col_props['name'], {}))

      response['support_updates'] = table.is_impala_only
      response['columns'] = [column.name for column in table.cols]
      response['extended_columns'] = cols_extended
      response['is_view'] = table.is_view
      response['partition_keys'] = [{'name': part.name, 'type': part.type} for part in table.partition_keys]
    else:
      col = db.get_column(database, table, column)
      if col:
        parse_tree = parser.parse_column(col.name, col.type, col.comment)
        if nested:
          parse_tree = _extract_nested_type(parse_tree, nested)
        response = parse_tree
        # If column or nested type is scalar/primitive, add sample of values
        if parser.is_scalar_type(parse_tree['type']):
          sample = _get_sample_data(db, database, table, column)
          if 'rows' in sample:
            response['sample'] = sample['rows']
      else:
        raise Exception('Could not find column `%s`.`%s`.`%s`' % (database, table, column))
  except (QueryServerTimeoutException, TTransportException), e:
    response['code'] = 503
    response['error'] = e.message
Exemplo n.º 2
0
Arquivo: api.py Projeto: gilvbp/hue
def _autocomplete(db,
                  database=None,
                  table=None,
                  column=None,
                  nested=None,
                  query=None,
                  cluster=None,
                  operation='schema'):
    response = {}

    try:
        if operation == 'functions':
            response['functions'] = _get_functions(db, database)
        elif database is None:
            response['databases'] = db.get_databases()
        elif table is None:
            tables_meta = db.get_tables_meta(database=database)
            response['tables_meta'] = tables_meta
        elif column is None:
            if query is not None:
                table = SubQueryTable(db, query)
            else:
                table = db.get_table(database, table)
            response['hdfs_link'] = table.hdfs_link
            response['comment'] = table.comment

            cols_extended = massage_columns_for_json(table.cols)

            if table.is_impala_only:  # Expand Kudu table information
                if db.client.query_server['server_name'] != 'impala':
                    query_server = get_query_server_config('impala',
                                                           connector=cluster)
                    db = dbms.get(db.client.user,
                                  query_server,
                                  cluster=cluster)

                col_options = db.get_table_describe(
                    database, table.name)  # Expand columns information
                extra_col_options = dict([
                    (col[0], dict(list(zip(col_options.cols(), col))))
                    for col in col_options.rows()
                ])
                for col_props in cols_extended:
                    col_props.update(
                        extra_col_options.get(col_props['name'], {}))

                primary_keys = [
                    col['name'] for col in extra_col_options.values()
                    if col.get('primary_key') == 'true'
                ]  # Until IMPALA-8291
                foreign_keys = []  # Not supported yet
            else:
                primary_keys = [pk.name for pk in table.primary_keys]
                foreign_keys = table.foreign_keys

            response['support_updates'] = table.is_impala_only
            response['columns'] = [column.name for column in table.cols]
            response['extended_columns'] = cols_extended
            response['is_view'] = table.is_view
            response['partition_keys'] = [{
                'name': part.name,
                'type': part.type
            } for part in table.partition_keys]
            response['primary_keys'] = [{'name': pk} for pk in primary_keys]
            response['foreign_keys'] = [{
                'name': fk.name,
                'to': fk.type
            } for fk in foreign_keys]
        else:
            col = db.get_column(database, table, column)
            if col:
                parse_tree = parser.parse_column(col.name, col.type,
                                                 col.comment)
                if nested:
                    parse_tree = _extract_nested_type(parse_tree, nested)
                response = parse_tree
                # If column or nested type is scalar/primitive, add sample of values
                if parser.is_scalar_type(parse_tree['type']):
                    sample = _get_sample_data(db,
                                              database,
                                              table,
                                              column,
                                              cluster=cluster)
                    if 'rows' in sample:
                        response['sample'] = sample['rows']
            else:
                raise Exception('Could not find column `%s`.`%s`.`%s`' %
                                (database, table, column))
    except (QueryServerTimeoutException, TTransportException) as e:
        response['code'] = 503
        response['error'] = str(e)
    except TypeError as e:
        response['code'] = 500
        response['error'] = str(e)
    except Exception as e:
        LOG.warn('Autocomplete data fetching error: %s' % e)
        response['code'] = 500
        response['error'] = str(e)

    return response