bigquery_在BigQuery中链接多个SQL查询

bigquery

Bigquery is a fantastic tool! It lets you do really powerful analytics works all using SQL like syntax.

Bigquery是一个很棒的工具! 它使您能够使用像语法一样SQL来进行真正强大的分析工作。

But it lacks chaining the SQL queries. We cannot run one SQL right after the completion of another. There are many real-life applications where the output of one query depends upon for the execution of another. And we would want to run multiple queries to achieve the results.

但是它缺少链接SQL查询的方法。 我们不能在完成另一个SQL之后立即运行一个SQL。 在许多实际应用中,一个查询的输出取决于另一个查询的执行。 我们希望运行多个查询来获得结果。

Here is one scenario, suppose you are doing RFM analysis using BigQuery ML. Where first you have to calculate the RFM values for all the users then apply the k-means cluster to the result of the first query and then merge the output of the first query and second query to generate the final data table.

这是一种情况,假设您正在使用BigQuery ML进行RFM分析 。 首先,您必须为所有用户计算RFM值,然后将k-means群集应用于第一个查询的结果,然后合并第一个查询和第二个查询的输出以生成最终数据表。

In the above scenario, every next query depends upon the output of the previous query and the output of each query also needs to be stored in data for other uses.

在上述情况下,每个下一个查询都取决于上一个查询的输出,每个查询的输出也需要存储在数据中以用于其他用途。

I this guide I will show how to execute as many SQL queries as you want in BigQuery one after another creating a chaining effect to gain the desire results.

在本指南中,我将展示如何在BigQuery中一个接一个地执行任意数量SQL查询,以及如何创建链接效果以获得所需的结果。

方法 (Methods)

I will demonstrate two approaches to chaining the queries

我将演示两种链接查询的方法

  1. The First using cloud pub/sub and cloud function: This is a more sophisticated method as it ensures that the current query is finished executing before executing the next one. This method also required a bit of programming experience so better to reach out to someone with a technical background in your company.

    第一种使用云发布/订阅和云功能:这是一种更为复杂的方法,因为它可以确保在执行下一个查询之前完成当前查询的执行。 这种方法还需要一点编程经验,因此更好地与您公司中具有技术背景的人员联系。

  2. The second using BigQuery’s own scheduler: However, the query scheduler cannot ensure the execution of one query is complete before the next is triggered so we will have to hack it using query execution time. More on this later.

    第二个使用BigQuery自己的调度程序:但是,查询调度程序无法确保一个查询的执行在触发下一个查询之前就已经完成,因此我们将不得不利用查询执行时间来破解它。 稍后再详细介绍。

And If you want to get your hands dirty yourself then here is an excellent course to start with.

而且,如果您想弄脏自己的双手,那么 这是一个很好的 起点。

Note: We will continue with the RFM example discussed above to get you the idea of the process. But the same can be applied for any possible scenario where triggering multiple SQL queries is needed.

注意 :我们将继续上面讨论的RFM示例,以使您了解该过程。 但是,对于需要触发多个SQL查询的任何可能情况,也可以应用相同的方法。

方法1 (Method 1)

Method 1 uses the combination of cloud functions and pub/subs to chain the entire flow. The process starts by query scheduler which after executing the first query sends a message to pub/sub topic that triggers a cloud function responsible to trigger 2nd query and once completed sends a message to another pub/sub topic to start yet another cloud function. The process continues until the last query is executed by the cloud function.

方法1使用云功能和pub / sub的组合来链接整个流程。 该过程由查询调度程序开始,查询调度程序在执行第一个查询后向pub / sub主题发送一条消息,该消息触发一个负责触发第二次查询的云功能,一旦完成,就向另一个pub / sub主题发送一条消息以启动另一个云功能。 该过程一直持续到云功能执行最后一个查询为止。

Let’s understand the process with our RFM analysis use case.

让我们通过我们的RFM分析用例来了解流程。

Suppose we have three queries that are needed to be run one after another to perform RFM analysis. First, that calculates RFM values, we will call it RFM Values. Second, that creates the model, we will call itRFM Model. Third, that merges model output with users RFM values, we will call it RFM Final.

假设我们有三个查询需要一个接一个地运行以执行RFM分析。 首先 ,它计算RFM值,我们将其称为 RFM Values 其次 ,创建模型,我们将其称为 RFM Model 第三 ,将模型输出与用户RFM值合并,我们将其称为 RFM Final

Here is how the data pipeline looks like:

数据管道如下所示:

Image for post
Chaining query in BigQuery data pipeline, by Muffaddal
Muffaddal在BigQuery数据管道中链接查询

Note: I will assume that tables for all three queries have already been created.

注意我将假设已经创建了所有三个查询的表。

1- We start by first creating a Pub/Sub topic as it will be needed while creating RFM Values query schedular. I have named it RFM_Model_Topic as it will trigger the cloud function responsible for executing our model query (i.e RFM Model).

1-我们首先创建一个Pub / Sub主题,因为在创建RFM Values查询计划时将需要它。 我将其命名为RFM_Model_Topic ,因为它将触发负责执行我们的模型查询的云函数(即RFM Model )。

Image for post
_Topic Pub/sub topic, by Muffaddal_Topic Pub / sub主题,作者:Muffaddal

Copy the topic name that is needed while creating RFM Values schedular.

计划创建 RFM Values ,复制所需的主题名称

2- Next, go to BigQuery, paste the RFM Values query that calculates RFM values for our users, in the query editor, and click the ‘Schedule query’ button to create a new query schedular.

2-接下来,转到BigQuery,在查询编辑器中粘贴为我们的用户计算RFM值的RFM Values查询,然后单击“计划查询”按钮以创建新的查询计划。

Image for post
create a scheduled query, by Muffaddal
通过Muffaddal创建计划的查询

3- Enter the required values in the scheduler creation menu to create the scheduler

3-在调度程序创建菜单中输入所需的值以创建调度程序

Image for post
query schedular creation menu, by Muffaddal
查询时间表创建菜单,由Muffaddal编写

What this scheduler will do is it will execute on the specified time to calculate users' recency, frequency, and monetary values and store it in the mentioned BigQuery table. Once the schedule is done executing the query it will send a message to our RFM_Model_Topic which will trigger a cloud function to trigger our model query. So next let’s create a cloud function.

该调度程序将执行的操作是在指定的时间执行以计算用户的新近度,频率和货币值,并将其存储在提到的BigQuery表中。 计划执行完查询后,它将向我们的RFM_Model_Topic发送一条消息,这将触发一个云函数来触发我们的模型查询。 因此,接下来让我们创建一个云功能。

4- Go to RFM_Model_Topicpub/sub topi and click ‘Trigger Cloud Function’ Button at the top of the screen.

4-转到RFM_Model_Topic pub / sub RFM_Model_Topic ,然后单击屏幕顶部的“触发云功能”按钮。

Image for post
create cloud function from pub/sub topic, by Muffaddal
通过发布/订阅主题创建云函数,作者:Muffaddal

5- Enters settings as shown below and name the cloud function as RFM_Model_Function

5-输入如下所示的设置,并将云功能命名为RFM_Model_Function

Image for post
cloud function settings, by Muffaddal
云功能设置,通过Muffaddal

6- And paste below code in index.js file

6-并将以下代码粘贴到index.js文件中

Cloud function to trigger RFM_Model Query, by Muffaddal
通过Muffaddal触发Cloud功能以触发RFM_Model查询

Once the query is executed cloud function sends a publish message to a new pub/sub topic named RFM_Final which triggers cloud function responsible for the last query that combines both RFM values and model results in one data set.

执行查询后,云功能会将发布消息发送到名为RFM_Final的新发布/子主题,该主题会触发负责最后一次查询的云功能,该功能将RFM值和模型结果组合到一个数据集中。

7- Therefore, next, create RFM_Model topic in pub/sub and a cloud function as we did in the previous step. Copy-paste below code in cloud function so that it can run the last query.

7-因此,接下来,像在上一步中一样,在pub / sub和一个云函数中创建RFM_Model主题。 将以下代码复制粘贴到云函数中,以便它可以运行最后一个查询。

Cloud function to trigger RFM_Final Query, by Muffaddal
通过Muffaddal触发Cloud功能以触发RFM_Final查询

And that is it!

就是这样!

We can use as many pub/sub and cloud functions as we want to chain as many SQL queries as we want.

我们可以使用任意数量的pub / sub和cloud函数,以根据需要链接任意数量SQL查询。

方法2 (Method 2)

Now the first approach is robust but requires a bit of programming background and says it is not your strong suit. You can use method 2 to chain the BigQuery queries.

现在,第一种方法是健壮的,但是需要一定的编程背景,并且说这不是您的强项。 您可以使用方法2链接BigQuery查询。

BigQuery’s query scheduler can be used to run the queries one after another.

BigQuery的查询计划程序可用于依次运行查询。

Idea is that we start the process the same as we did in method 1, i.e. trigger the first query using a scheduler and estimate its time for completion. Let’s say the first query takes 5 minutes to complete. What we will do is trigger the 2nd query 10 mints after the first query start time. This way we are ensured that the second query is triggered after the first query is completely executed.

想法是,我们开始的过程与方法1相同,即使用调度程序触发第一个查询并估计其完成时间。 假设第一个查询需要5分钟才能完成。 我们要做的是在第一个查询开始时间之后10分钟触发第二个查询。 这样,我们可以确保在完全执行第一个查询后触发第二个查询。

Let’s understand this by example

让我们通过示例来了解这一点

Image for post
Chain queries using query scheduler, by Muffaddal
使用查询调度程序链接查询,作者:Muffaddal

Suppose we scheduled the first query at 12:30 am. It takes 10 mints to complete. So we know at 12:40 am the first query should be completed. We will set the second query scheduler to execute at 12:50 am (keeping a 10 mint gap between two schedulers just in case). And we will trigger the third query at 1:10 am and so on.

假设我们将第一个查询安排在上午12:30。 它需要10颗薄荷糖才能完成。 因此,我们知道应该在12:40 am完成第一个查询。 我们将第二个查询调度程序设置为在上午12:50执行(以防万一,两个调度程序之间要保持10分钟的间隔)。 然后,我们将在上午1:10触发第三个查询,依此类推。

Note: Since the query scheduler doesn’t work with BigQuery ML, therefore, method 2 won’t work for our RFM analysis case but It should get you the idea on how to use the scheduler to chain queries.

注意 :由于查询调度程序不适用于BigQuery ML,因此方法2在我们的RFM分析案例中不起作用,但是它应该使您了解如何使用调度程序链接查询。

摘要 (Summary)

Executing queries one after another helps to achieve really great results especially when the result of one query depends on the output of another and all the query results are also needed as table format as well. BigQuery out of the box doesn’t support this functionality but using GCP’s component we can streamline the process to achieve the results.

逐个执行查询有助于获得非常好的结果,尤其是当一个查询的结果取决于另一个查询的输出并且所有查询结果也都需要作为表格式时。 开箱即用的BigQuery不支持此功能,但是使用GCP的组件,我们可以简化流程以实现结果。

In this article, we went through two of the method to do this. First using the cloud pub/sub and cloud function and another using BigQuery’s own query scheduler.

在本文中,我们介绍了两种方法来执行此操作。 首先使用云发布/订阅和云功能,另一个使用BigQuery自己的查询调度程序。

With this article, I hope I was able to convey the idea of the process for you to pick it up and tailor it for your particular business case.

希望通过这篇文章,您可以传达有关流程的想法,以供您选择并针对您的特定业务案例进行调整。

您想要的类似读物: (Similar Reads You Would Like:)

  1. Automate the RFM analysis using BigQuery ML.

    使用BigQuery ML自动执行RFM分析。

  2. Store Standard Google Analytics Hit Level Data in BigQuery.

    在BigQuery中存储标准Google Analytics(分析)点击量数据 。

  3. Automate Data Import to Google Analytics using GCP.

    使用GCP自动将数据导入Google Analytics(分析) 。

翻译自: https://towardsdatascience.com/chaining-multiple-sql-queries-in-bigquery-8498d8885da5

bigquery

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388822.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

允许指定IP访问远程桌面

允许指定IP访问远程桌面 电脑软件 2010-01-23 02:33:40 阅读595 评论0 字号:大 中 小 订阅 一、新建IP安全策略 WINR打开运行对话框,输入gpedit.msc进入组策略编辑器。 依次打开“本地计算机”策略--计算机配置--Windows设置--安全设置--IP安…

大理石在哪儿 (Where is the Marble?,UVa 10474)

题目描述&#xff1a;算法竞赛入门经典例题5-1 1 #include <iostream>2 #include <algorithm>3 using namespace std;4 int maxn 10000 ;5 int main()6 {7 int n,q,a[maxn] ,k0;8 while(scanf("%d%d",&n,&q)2 && n &&q…

Volley 源码解析之网络请求

Volley源码分析三部曲Volley 源码解析之网络请求Volley 源码解析之图片请求Volley 源码解析之缓存机制 Volley 是 Google 推出的一款网络通信框架&#xff0c;非常适合数据量小、通信频繁的网络请求&#xff0c;支持并发、缓存和容易扩展、调试等&#xff1b;不过不太适合下载大…

为什么修改了ie级别里的activex控件为启用后,还是无法下载,显示还是ie级别设置太高?

如果下载插件时下载不了&#xff0c;这样设置&#xff0c;打开IE选工具/Internet 选项/安全/自定义级别/设置中的ActiveX控件自动提示“禁用”。 对标记为可安全执行脚本ActiveX控件执行脚本“启用” 对没有标记为安全的ActiveX初始化和脚本运行“启用”&#xff08;下载插件后…

mysql 迁移到tidb_通过从MySQL迁移到TiDB来水平扩展Hive Metastore数据库

mysql 迁移到tidbIndustry: Knowledge Sharing行业&#xff1a;知识共享 Author: Mengyu Hu (Platform Engineer at Zhihu)作者&#xff1a;胡梦瑜(Zhhu的平台工程师) Zhihu which means “Do you know?” in classical Chinese, is the Quora of China: a question-and-ans…

两个日期相差月份 java_Java获取两个指定日期之间的所有月份

String y1 "2016-02";//开始时间String y2 "2019-12";//结束时间try{Date startDate new SimpleDateFormat("yyyy-MM").parse(y1);Date endDate new SimpleDateFormat("yyyy-MM").parse(y2);Calendar calendarCalendar.getInstance(…

js前端日期格式化处理

js前端日期格式化处理 1.项目中时间返回值&#xff0c;很过时候为毫秒值&#xff0c;我们需要转换成 能够看懂的时间的格式&#xff1b; 例如&#xff1a; ​ yyyy-MM-dd HH:mm:ss 2.处理方法&#xff08;处理方法有多种&#xff0c;可以传值到前端处理&#xff0c;也可以后台可…

如何用sysbench做好IO性能测试

sysbench 是一个非常经典的综合性能测试工具&#xff0c;通常都用它来做数据库的性能压测&#xff0c;但也可以用来做CPU&#xff0c;IO的性能测试。而对于IO测试&#xff0c;不是很推荐sysbench&#xff0c;倒不是说它有错误&#xff0c;工具本身没有任何问题&#xff0c;它的…

XCode、Objective-C、Cocoa 说的是几样东西

大部分有一点其他平台开发基础的初学者看到XCode&#xff0c;第一感想是磨拳擦掌&#xff0c;看到 Interface Builder之后&#xff0c;第一感想是跃跃欲试&#xff0c;而看到Objective-C的语法&#xff0c;第一感想就变成就望而却步了。好吧&#xff0c;我是在说我自己。 如果…

java http2_探索HTTP/2: HTTP 2协议简述(原)

探索HTTP/2: HTTP/2协议简述HTTP/2的协议包含着两个RFC&#xff1a;Hypertext Transfer Protocol Version 2 (RFC7540)&#xff0c;即HTTP/2&#xff1b;HPACK: Header Compression for HTTP/2 (RFC7541)&#xff0c;即HPACK。RFC7540描述了HTTP/2的语义&#xff0c;RFC7541则描…

错误处理

错误处理&#xff1a; 许多系统调用和函数在失败后&#xff0c;会在失败时设置外部变量errno的值来指明失败原因。许多不同的函数库都把这个变量作为报告错误的标准方法。程序必须在函数报告出错后立刻检查errno变量&#xff0c;因为它可能被下一个函数调用所覆盖&#xff…

Android类库介绍

Android类库介绍 GPhone开发包Android SDK含了很多丰富的类库&#xff1a; android.util 涉及系统底层的辅助类库 android.os 提供了系统服务、消息传输、IPC管道 android.graphics GPhone图形库&#xff0c;包含了文本显示、输入输出、文字样式 android.database 包含底层的AP…

递归函数基例和链条_链条和叉子

递归函数基例和链条因果推论 (Causal Inference) This is the fifth post on the series we work our way through “Causal Inference In Statistics” a nice Primer co-authored by Judea Pearl himself.这是本系列的第五篇文章&#xff0c;我们通过“因果统计推断”一书进行…

前端技能拾遗

本文主要是对自己前端知识遗漏点的总结和归纳&#xff0c;希望对大家有用&#xff0c;会持续更新的~ 解释语言和编译型语言 解释型语言与编译型语言的区别翻译时间的不同。 编译型语言在程序执行之前&#xff0c;有一个单独的编译过程&#xff0c;将程序翻译成机器语言&#xf…

java lock 信号_java各种锁(ReentrantLock,Semaphore,CountDownLatch)的实现原理

先放结论&#xff1a;主要是实现AbstractQueuedSynchronizer中进入和退出函数&#xff0c;控制不同的进入和退出条件&#xff0c;实现适用于各种场景下的锁。JAVA中对于线程的同步提供了多种锁机制&#xff0c;比较著名的有可重入锁ReentrantLock&#xff0c;信号量机制Semapho…

Intent.ACTION_MAIN

1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始。比较常用。 Input:nothing Output:nothing 例如&#xff1a; 1 <activity android:name".Main"android:label"string/app_name">2 <intent-filter…

足球预测_预测足球热

足球预测By Aditya Pethe通过阿蒂亚皮特(Aditya Pethe) From September to January every year, football takes over America. Games dominate TV Sunday and Monday nights, and my brother tears his hair out each week over his consistently underperforming fantasy te…

C#的特性Attribute

一、什么是特性 特性是用于在运行时传递程序中各种元素&#xff08;比如类、方法、结构、枚举、组件等&#xff09;的行为信息的声明性标签&#xff0c;这个标签可以有多个。您可以通过使用特性向程序添加声明性信息。一个声明性标签是通过放置在它所应用的元素前面的方括号&am…

java 技能鉴定_JAVA试题-技能鉴定

一、单选题1.以下创建了几个对象( B)String A,B,CA"a";B"b":AAB;StringBuffer Dnew StringBuffer("abc");DD.append("567");A.6B.4C.3D.52.关于以下程序段&#xff0c;正确的说法是( C )1&#xff0e;String s1“a”“b”;2&#xff0…

ADD_SHORTCUT_ACTION

String ADD_SHORTCUT_ACTION 动作&#xff1a;在系统中添加一个快捷方式。. “android.intent.action.ADD_SHORTCUT”   String ALL_APPS_ACTION 动作&#xff1a;列举所有可用的应用。   输入&#xff1a;无。 “android.intent.action.ALL_APPS”   String ALTERNATIVE…