Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Google cloud API call with Python "Cannot read and write in different locations"

fyzeri

New Coder
Hi,

I'm trying to make a API call to read data from google storage file and then to write that data to google bigquery. For both currently the region/location is set to EU but somehow I still receive the following API error "google.api_core.exceptions.BadRequest: 400 Cannot read and write in different locations: source: EU, destination: US"

Here is the python file:

from google.cloud import bigquery

client = bigquery.Client()

external_config = bigquery. ExternalConfig("CSV")
external_config.source_uris = [
"gs/😛athwrittenhere"
]
external_config.schema = [
bigquery.SchemaField("name", "STRING"),
bigquery.SchemaField("post_abbr", "STRING"),
]

external_config.csv_options.skip_leading_rows = 1

table_id = "us_states_table"



sql = 'SELECT * FROM `{0}` WHERE name LIKE "W%"'.format(table_id)

job_config = bigquery.QueryJobConfig(table_definitions={table_id: external_config})
query_job = client.query(sql, job_config =job_config)

w_states = list(query_job)

print (w_states)

Is there anyway to fix this or do I have to set up new storage and bigquery projects?
Thank you a lot in advance, Im quite new with these API calls to cloud! All the help is really appreciated.
 
hi fyzeri
To fix the error "google.api_core.exceptions.BadRequest: 400 Cannot read and write in different locations: source: EU, destination: US", you can either change the location of the file in Cloud Storage to be the same as the location of the table in BigQuery, or change the location of the table in BigQuery to be the same as the location of the file in Cloud Storage.

If you choose to change the location of the file in Cloud Storage, you can do this by moving the file to a bucket in a different location. If you choose to change the location of the table in BigQuery, you can do this by creating a new table in a different location and then inserting the data from the file in Cloud Storage into the new table.

It is generally recommended to keep data in the same location as the resources that will be accessing it, to minimize latency and improve performance. If you have to access data across locations, it may be more efficient to copy the data to a location closer to the resources that will be accessing it, rather than reading the data from a remote location.
 
Back
Top Bottom