Search code examples
google-cloud-platformgoogle-bigquerygoogle-cloud-sdkgcloud-cli

Getting details of a BigQuery job using gcloud CLI on local machine


I am trying to process the billed bytes of each bigquery job runned by all user. I was able to find the details in BigQuery UI under Project History. Also running bq --location=europe-west3 show --job=true --format=prettyjson JOB_ID on Google Cloud Shell gives the exact information that I want (BQ SQL query, billed bytes, run time for each bigquery job).

For the next step, I want to access the json that returned by above script on local machine. I have already configured gcloud cli properly, and able to find bigquery jobs using gcloud alpha bq jobs list --show-all-users --limit=10. I select a job id and run the following script: gcloud alpha bq jobs describe JOB_ID --project=PROJECT_ID, I get (gcloud.alpha.bq.jobs.describe) NOT_FOUND: Not found: Job PROJECT_ID:JOB_ID--toyFH. It is possibly because of creation and end times as shown here

What am I doing wrong? Is there another way to get details of a bigquery job using gcloud cli (maybe there is a way to get billed bytes with query details using Python SDK)?


Solution

  • You can get job details with diff APIs or as you are doing, but first, why are you using the alpha version of the bq?

    To do it in python, you can try something like this:

    from google.cloud import bigquery
    
    def get_job(
        client: bigquery.Client,
        location: str = "us",
        job_id: str = << JOB_ID >>,
    ) -> None:
        job = client.get_job(job_id, location=location)
    
        print(f"{job.location}:{job.job_id}")
        print(f"Type: {job.job_type}")
        print(f"State: {job.state}")
        print(f"Created: {job.created.isoformat()}")
    

    There are more properties that you can get with some kind of command from the job. Also check the status of the job in the console first, to compare between them

    You can find more details here: https://cloud.google.com/bigquery/docs/managing-jobs#python