简体   繁体   中英

Is it possible to join cloud sql table to bigquery?

I have a large amount of data in bigquery and I want to do some analysis that would be enhanced by doing a join to a small set of data I have in cloud sql. I've search but cannot find a sql based bridge between the two. I was thinking something like this:

SELECT
  bqdb.table as a,
  csdb.table as b,
  csdb.table as c
FROM bigquery:project:bqdb.table as t1,cloudsql:project:csdb.table as t2
JOIN t1 ON t1.a=t2.b
WHERE a='foo'
GROUP BY a,b
ORDER BY c

There's not currently a direct bridge between data in Cloud SQL and Google BigQuery. In order to run a query like this, you will need to export your Cloud SQL table data in CSV format via the mysqldump tool, and then import this data into BigQuery as a new table.

This is now possible according to the documentation here :

Data is often scattered in many places. You may store a customer table in BigQuery, while storing a sales table in Cloud SQL, and want to join the two tables in a single query.

BigQuery Cloud SQL federation enables BigQuery to query data residing in Cloud SQL in real-time, without copying or moving data. It supports both MySQL (2nd generation) and PostgreSQL instances in Cloud SQL.

After the initial one-time set up, you can write a query with the new SQL function EXTERNAL_QUERY() .

...

Suppose you need the date of the first order for each of your customers to include in the report we described in the Overview. This data is not currently in BigQuery but is available in your operational PostgreSQL database in Cloud SQL. The following federated query example accomplishes this.

 SELECT c.customer_id, c.name, SUM(t.amount) AS total_revenue, rq.first_order_date FROM customers AS c INNER JOIN transaction_fact AS t ON c.customer_id = t.customer_id LEFT OUTER JOIN EXTERNAL_QUERY( 'connection_id', '''SELECT customer_id, MIN(order_date) AS first_order_date FROM orders GROUP BY customer_id''') AS rq ON rq.customer_id = c.customer_id GROUP BY c.customer_id, c.name, rq.first_order_date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM