0

I am new to BQ I am trying to save my BQ tables as a pandas dataframe in my colab environment. This is the code I am using but I am getting a "Bad request Error". Any ideas how I can trouble shoot? I can't figure out what I am doing wrong. My code is as follows:

from google.cloud import bigquery

client = bigquery.Client(project=project_id)

sample_count = 2000
row_count = client.query('''
  SELECT 
    COUNT(*) as total
  FROM `123.cleaned_sales`''').to_dataframe().total[0]

df = client.query('''
  SELECT
    *
  FROM
    `123.cleaned_sales`
  WHERE RAND() < %d/%d
''' % (sample_count, row_count)).to_dataframe()

print('Full dataset has %d rows' % row_count)```

Here is my error message 

[enter image description here][1]


  [1]: https://i.stack.imgur.com/VQOox.png
Adli K
  • 3
  • 1

1 Answers1

0

The image you shared indicates the projectId and datasetId should not to be empty in this query :

row_count = client.query('''
  SELECT 
    COUNT(*) as total
  FROM `123.cleaned_sales`''').to_dataframe().total[0]

You have to set the projectId :

row_count = client.query('''
  SELECT 
    COUNT(*) as total
  FROM `your_project_id.123.cleaned_sales`''').to_dataframe().total[0]

Do the same with the second query.

Mazlum Tosun
  • 5,761
  • 1
  • 9
  • 23
  • Thank you for the reply, but that is not working: `project_id = '[sales-forecast-361716]' from google.cloud import bigquery client = bigquery.Client(project=project_id) sample_count = 2000 row_count = client.query(''' SELECT COUNT(*) as total FROM `sales-forecast-361716.123.cleaned_sales`''').to_dataframe().total[0] df = client.query(''' SELECT * FROM `sales-forecast-361716.123.cleaned_sales` WHERE RAND() < %d/%d ''' % (sample_count, row_count)).to_dataframe() print('Full dataset has %d rows' % row_count)` – Adli K Sep 08 '22 at 01:00
  • do you know what I might be doing wrong? – Adli K Sep 08 '22 at 01:10
  • You put braquets on project ID, can you test without braquets on project ID please ? project_id = 'sales-forecast-361716' I will edit my answer because normally if you set project in the Bigquery client, you not are obliged to passed it in bq queries – Mazlum Tosun Sep 08 '22 at 06:49