def select_insert(self,
                   source_table_id,
                   destination_table_id,
                   query_field,
                   prefix='    ',
                   fg='yellow'):
     query = 'SELECT {query_field} FROM {dataset_id}.{source_table_id}'.format(
         query_field=query_field,
         dataset_id=self._dataset_ref.dataset_id,
         source_table_id=source_table_id)
     destination_table = self.dataset.table(destination_table_id)
     job_config = QueryJobConfig()
     job_config.use_legacy_sql = False
     job_config.use_query_cache = False
     job_config.write_disposition = WriteDisposition.WRITE_TRUNCATE
     job_config.destination = destination_table
     job = self._client.query(query, job_config)
     echo('Inserting... {0}'.format(job.job_id),
          prefix=prefix,
          fg=fg,
          no_color=self.no_color)
     echo('  {0}'.format(job.query),
          prefix=prefix,
          fg=fg,
          no_color=self.no_color)
     job.result()
     assert job.state == 'DONE'
     error_result = job.error_result
     if error_result:
         raise RuntimeError(job.errors)
예제 #2
0
def execute_sync_query(project_id, query_str, bq_client=None):
    if bq_client is None:
        bq_client = bigquery.Client(project_id)
    config = QueryJobConfig()
    config.use_legacy_sql = False
    config.use_query_cache = False
    query_job = bq_client.query(query_str, job_config=config, location="EU")

    result = []
    for row in query_job:
        result.append(row)

    return result