# BigQuery

Este módulo permite manejar distintos métodos de BigQuery (opens new window) para la gestión de consultas, jobs, y exportar datos.

# Funciones disponibles

La clase Jobs, implementada en el BackEndBase contiene las siguientes funciones:

  • create_job
  • get_status_job
  • cancel_job

La clase BigQuery, implementada en el BackEndBase contiene las siguientes funciones:

  • init
  • send_query
  • response
  • big_response
  • create_table
  • insert_json
  • export_data

# Jobs

# create_job

@staticmethod
def create_job(query, prefix=None, location=None, destination_table=None, labels=None, maximum_bytes_billed=None):
    ...

# get_status_job

@staticmethod
def get_status_job(job_id, location=None)
    ...

# cancel_job

@staticmethod
def cancel_job(job_id, location=None):
    ...

# Un ejemplo








 


from app.ext.google_lib.bigquery import Jobs
...

export_table = 'awesome_table'
location = "US"
query_job = "SELECT * FROM awesome_table"

result_job = Jobs.create_job(query_job, None, location, export_table)
print("result_job:", result_job)

El resultado del ejemplo anterior:

{
    "job_id": "awesome_export_job_20201023113411d616c614-5974-4103-9713-eb0c86948b46",
    "prefix": "awesome_export_job_20201023113411",
    "location": "US",
    "labels": {
        "awesome_export_job": "awesome_export_job_20201023113411"
    },
    "maximum_bytes_billed": 999869312,
    "job_result": <google.cloud.bigquery.table.RowIterator object at 0x112ceca50>
}

# BigQuery

# init

@staticmethod
def init(dataset, table):
    ...

# send_query

@staticmethod
def send_query(query, location="US"):
    ...

# response

@staticmethod
def response(query_job, raw_mode=False):
    ...

# big_response

@staticmethod
def big_response(query_job=None):
    ...

# create_table

@staticmethod
def create_table(schema=None, dataset=None, table=None):
    ...

# insert_json

@staticmethod
def insert_json(dataset, table, rows_to_insert):
    ...

# export_data

@staticmethod
def export_data(dataset, table_id, location=None):
    ...

Ejemplo:











 
 


from app.ext.google_lib.bigquery import BigQuery
...

dataset = 'awesome_dataset'
table = 'awesome_table'

query = "SELECT * FROM awesome_table"
fields = ['name', 'lastName', 'address', 'email']

BigQuery.init(dataset, table)
result = BigQuery.send_query(query, result_fields=fields)

print("BigQuery result:", result)

El resultado del ejemplo anterior:

[
  {
    "name": "brad",
    "lastName": "gibson",
    "address": "Elm Street",
    "email": "brad@gibsonfamily.com"
  }
]

# Más información

Para mas información consulte: