def get_query_results(self, query, use_legacy_sql=False, max_wait_secs=None): # type: (str, Optional[bool], Optional[int]) -> List[Tuple[Any]] """Returns a list or rows, each of which is a tuple of values. Args: query: A string with a complete SQL query. use_legacy_sql: Whether to use legacy SQL max_wait_secs: The maximum number of seconds to wait for the query to complete. If not set, the class default will be used. Returns: A list of tuples of values. """ config = QueryJobConfig() if self.maximum_billing_tier: config.maximum_billing_tier = self.maximum_billing_tier config.use_legacy_sql = use_legacy_sql query_job = self.gclient.query(query, job_config=config, retry=self.default_retry_for_api_calls) # The above retry is for errors encountered in executing the jobs. The below retry is # for errors encountered in polling to see whether the job is done. query_job._retry = self.default_retry_for_async_jobs rows = self._wait_for_job(query_job, query, max_wait_secs=max_wait_secs or self.max_wait_secs) if query_job.errors: logging.warning('Errors in get_query_results: {}'.format(query_job.errors)) return [x.values() for x in list(rows)]
def get_query_results(self, query, use_legacy_sql=False, max_wait_secs=None): # type: (str, Optional[bool], Optional[int]) -> List[Tuple[Any]] """Returns a list or rows, each of which is a tuple of values. Args: query: A string with a complete SQL query. use_legacy_sql: Whether to use legacy SQL max_wait_secs: The maximum number of seconds to wait for the query to complete. If not set, the class default will be used. Returns: A list of tuples of values. """ config = QueryJobConfig() if self.maximum_billing_tier: config.maximum_billing_tier = self.maximum_billing_tier config.use_legacy_sql = use_legacy_sql query_job = self._run_async_query(query, job_config=config) rows = self._wait_for_job(query_job, query, max_wait_secs=max_wait_secs or self.max_wait_secs) if query_job.errors: logging.warning('Errors in get_query_results: {}'.format( query_job.errors)) return [x.values() for x in list(rows)]
def create_table_from_query( self, query, # type: str table_path, # type: str write_disposition='WRITE_EMPTY', # type: Optional[str] use_legacy_sql=False, # type: Optional[bool] max_wait_secs=None, # type: Optional[int] expected_schema=None # type: Optional[List[SchemaField]] ): # type: (...) -> None """Creates a table in BigQuery from a specified query. Args: query: The query to run. table_path: The path to the table (in the client's project) to write the results to. write_disposition: Specifies behavior if table already exists. See options here: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs under configuration.query.writeDisposition use_legacy_sql: Whether the query is written in standard or legacy sql. max_wait_secs: Seconds to wait for the query before timing out. If not set, the class default will be used. expected_schema: The expected schema of the resulting table; unused in this implementation """ if write_disposition not in [ 'WRITE_TRUNCATE', 'WRITE_APPEND', 'WRITE_EMPTY' ]: raise ValueError( 'write_disposition must be one of WRITE_TRUNCATE, ' 'WRITE_APPEND, or WRITE_EMPTY') config = QueryJobConfig() if self.maximum_billing_tier: config.maximum_billing_tier = self.maximum_billing_tier config.use_legacy_sql = use_legacy_sql config.write_disposition = write_disposition config.allow_large_results = True config.destination = self.get_table_reference_from_path(table_path) query_job = self._run_async_query(query, job_config=config) return self._wait_for_job(query_job, query, max_wait_secs=max_wait_secs or self.max_wait_secs)
def create_table_from_query(self, query, # type: str table_path, # type: str write_disposition='WRITE_EMPTY', # type: Optional[str] use_legacy_sql=False, # type: Optional[bool] max_wait_secs=None, # type: Optional[int] expected_schema=None # type: Optional[List[SchemaField]] ): # type: (...) -> None """Creates a table in BigQuery from a specified query. Args: query: The query to run. table_path: The path to the table (in the client's project) to write the results to. write_disposition: One of 'WRITE_TRUNCATE', 'WRITE_APPEND', 'WRITE_EMPTY'. Default is WRITE_EMPTY. use_legacy_sql: Whether the query is written in standard or legacy sql. max_wait_secs: Seconds to wait for the query before timing out. If not set, the class default will be used. expected_schema: The expected schema of the resulting table; unused in this implementation """ if write_disposition not in ['WRITE_TRUNCATE', 'WRITE_APPEND', 'WRITE_EMPTY']: raise ValueError('write_disposition must be one of WRITE_TRUNCATE, ' 'WRITE_APPEND, or WRITE_EMPTY') config = QueryJobConfig() if self.maximum_billing_tier: config.maximum_billing_tier = self.maximum_billing_tier config.use_legacy_sql = use_legacy_sql config.write_disposition = write_disposition config.allow_large_results = True config.destination = self.get_table_reference_from_path(table_path) query_job = self.gclient.query(query, job_config=config, retry=self.default_retry) return query_job.result(timeout=max_wait_secs or self.max_wait_secs)
def get_query_results(self, query, use_legacy_sql=False, max_wait_secs=None): # type: (str, Optional[Bool], Optional[int]) -> List[Tuple[Any]] """Returns a list or rows, each of which is a tuple of values. Args: query: A string with a complete SQL query. use_legacy_sql: Whether to use legacy SQL max_wait_secs: The maximum number of seconds to wait for the query to complete. If not set, the class default will be used. Returns: A list of tuples of values. """ config = QueryJobConfig() if self.maximum_billing_tier: config.maximum_billing_tier = self.maximum_billing_tier config.use_legacy_sql = use_legacy_sql query_job = self.gclient.query(query, job_config=config, retry=self.default_retry) rows = query_job.result(retry=self.default_retry, timeout=max_wait_secs or self.max_wait_secs) return [x.values() for x in list(rows)]