bigquery使用教程_如何使用Python和Google BigQuery构建机器人以自动执行您的笨拙任务...
bigquery使用教程
Do you have repetitive tasks? Something that you do regularly, every week or even every day? Reporting might be one of your weekly or daily tasks. You query or ask for the data, and do some visualizations, then give it to your boss. What if, instead of doing it manually, you were to automate it so you don’t have to do the boring stuff, and you can use your precious time to do other things?
您有重复的任务吗? 您每周,甚至每天都定期做些什么? 报告可能是您每周或每天的任务之一。 您查询或索要数据,并进行一些可视化处理,然后将其提供给老板。 如果不是要手动执行操作,而是要自动执行操作,这样就不必做无聊的事情,而您可以利用宝贵的时间做其他事情怎么办?
In this tutorial, we are going to make a Telegram Bot that will automate the boring parts of your job — reporting. Oh, and did I mention that it won’t take more than 50 lines of code to build it? ;)
在本教程中,我们将制作一个Telegram Bot,它将自动执行您工作中无聊的部分-报告。 哦,我是否提到过构建它不会花费超过50行代码? ;)
If this is your first time building a Telegram Bot, you might want to read this post first.
如果这是您第一次构建Telegram Bot,则可能需要先阅读这篇文章 。
入门 (Getting started)
1.安装库 (1. Install the libraries)
We are going to use google-cloud-bigquery to query the data from Google BigQuery. matplotlib, numpy and pandas will help us with the data visualization. python-telegram-bot will send the visualization image through Telegram Chat.
我们将使用google-cloud-bigquery从Google BigQuery查询数据。 matplotlib , numpy和pandas将帮助我们进行数据可视化。 python-telegram-bot将通过Telegram Chat发送可视化图像。
pip3 install google-cloud-bigquery matplotlib numpy pandas python-telegram-bot
2.启用Google BigQuery API (2. Enable Google BigQuery API)
We need to enable the Google BigQuery API first if we want to use the service.
如果要使用该服务,我们需要先启用Google BigQuery API。
Go to Google Developers Console and create a new project (or select the one you have).
转到Google Developers Console并创建一个新项目(或选择您拥有的项目)。
In the project dashboard, click ENABLE APIS AND SERVICES, and search for BigQuery API.
在项目仪表板中,单击“ 启用 API 和服务” ,然后搜索BigQuery API。
Click ENABLE to enable the API.
单击启用以启用API。
3.创建服务帐户密钥 (
3. Create the service account key)
If we want to use Google Cloud services like Google BigQuery, we need a service account key. This is like our credentials to use Google’s services.
如果我们要使用Google BigQuery等Google Cloud服务,则需要一个服务帐户密钥。 就像我们使用Google服务的凭据一样。
Go to Google Developers Console, click the Credentials tab, choose Create credentials and click Service account key.
转到Google Developers Console ,单击“ 凭据”标签,选择“ 创建凭据” ,然后单击“ 服务帐户密钥” 。
Choose New service account, in the Service account name field, enter a name.
选择“ 新服务帐户” ,在“ 服务帐户名称”字段中输入名称。
From the Role drop-down list, select Project > Owner, then click Create.
从“ 角色”下拉列表中,选择“ 项目” >“ 所有者”,然后单击“ 创建” 。
There is a .json file that will be automatically downloaded, name it creds.json
.
有一个.json文件将被自动下载,命名为creds.json
。
Set the GOOGLE_APPLICATION_CREDENTIALS
with the path of our creds.json
file in the terminal.
在终端中使用我们creds.json
文件的路径设置GOOGLE_APPLICATION_CREDENTIALS
。
export GOOGLE_APPLICATION_CREDENTIALS='[PATH_TO_CREDS.JSON]'
Everything should be good now, it is time to write our program.
现在一切都应该很好,是时候编写我们的程序了。
编写程序 (Write the program)
We are going to write the program that will query the data from BigQuery (we assume the data is stored there). Then we will visualize the data and save as an image. The image will then be sent through Telegram Chat.
我们将编写一个程序,该程序将从BigQuery查询数据(假设数据存储在此处)。 然后,我们将数据可视化并另存为图像。 然后将通过电报聊天发送图像。
For this tutorial, we are using the bigquery-public-data.stackoverflow
dataset, and we will take the daily total posts data for our report.
在本教程中,我们使用bigquery-public-data.stackoverflow
数据集,我们将获取报告的每日总帖子数据。
The workflow of our program is pretty simple:
我们程序的工作流程非常简单:
Query the table -> Visualize the data -> Save the visualization -> Send the image
查询表-> 可视化数据-> 保存可视化-> 发送图像
Let’s make a single function to define each flow.
让我们做一个定义每个流程的函数。
1.查询到BigQuery (1. Query to BigQuery)
Import the library first.from google.cloud import bigquery
首先从google.cloud导入库。导入bigquery
Make a function called query_to_bigquery
which takes query
as the parameter.
创建一个名为query_to_bigquery
的函数,该函数将query
作为参数。
def query_to_bigquery(query):client = bigquery.Client() query_job = client.query(query) result = query_job.result() dataframe = result.to_dataframe() return dataframe
This function will return the data as a dataframe.
此函数将数据作为数据帧返回。
2.可视化数据 (2. Visualize the data)
We are going to use matplotlib to visualize the data.
我们将使用matplotlib可视化数据。
import matplotlib.pyplot as plt
We take five parameters which are x
as the x-axis data, x_label
as the x-axis label name, y
as the y-axis data, y_label
as the y-axis label name, and title
as our visualization title.
我们使用五个参数,其中x
作为x轴数据, x_label
作为x轴标签名称, y
作为y轴数据, y_label
作为y轴标签名称,以及title
作为我们的可视化标题。
def visualize_bar_chart(x, x_label, y, y_label, title):plt.title(title) plt.xlabel(x_label) plt.ylabel(y_label) index = np.arange(len(x)) plt.xticks(index, x, fontsize=5, rotation=30) plt.bar(index, y) return plt
3.保存图像 (3. Save the image)
Let’s use the two functions above to create a visualization then save the image.
让我们使用上面的两个函数创建可视化文件,然后保存图像。
Like I mentioned before, we want to send the daily total posts data. Write the query first.
就像我之前提到的,我们要发送每日总帖子数据。 首先编写查询。
query = """ SELECT DATE(creation_date) date, COUNT(*) total_postsFROM `bigquery-public-data.stackoverflow.post_history`GROUP BY 1HAVING date > DATE_SUB('2018-12-02', INTERVAL 14 DAY)ORDER BY 1"""
Note that in the query above, HAVING date > DATE_SUB('2018-12-02', INTERVAL 14 DAY)
means we want to gather the data starting 14 days ago from 2018–12–02.
请注意,在上面的查询中, HAVING date > DATE_SUB('2018-12-02', INTERVAL 14 DAY)
意味着我们要从14-12天开始从2018-12-02开始收集数据。
We use that date because 2018-12-02
is the last data recorded in bigquery-public-data.stackoverflow.post_history
, in different cases you might want to use CURRENT_DATE()
instead so you will get the newest data.
我们使用该日期,因为2018-12-02
是bigquery-public-data.stackoverflow.post_history
记录的最后一个数据,在不同情况下,您可能希望使用CURRENT_DATE()
来获取最新数据。
Call query_to_bigquery
function to get the data.
调用query_to_bigquery
函数以获取数据。
dataframe = query_to_bigquery(query)
Take the date
column as our x-axis data, and total_posts
column as our y-axis data.
将date
列作为我们的x轴数据,并将total_posts
列作为我们的y轴数据。
x = dataframe['date'].tolist()
y = dataframe['total_posts'].tolist()
Visualize the data using the visualize_bar_chart
function, then save it as an image.
使用visualize_bar_chart
函数可视化数据,然后将其另存为图像。
plt = visualize_bar_chart(x=x, x_label='Date', y=y, y_label='Total Posts', title='Daily Posts')
plt.savefig('viz.png')
Wrap that code in a function called get_and_save_image
.
将该代码包装在一个名为get_and_save_image
的函数中。
def get_and_save_image(): query = """ SELECT DATE(creation_date) date, COUNT(*) total_postsFROM `bigquery-public-data.stackoverflow.post_history`GROUP BY 1HAVING date > DATE_SUB('2018-12-02', INTERVAL 14 DAY)ORDER BY 1"""dataframe = query_to_bigquery(query)x = dataframe['date'].tolist()y = dataframe['total_posts'].tolist()plt = visualize_bar_chart(x=x, x_label='Date', y=y, y_label='Total Posts', title='Daily Posts')plt.savefig('viz.png')
4.发送图片 (4. Send the image)
To be able to send it to the right person, we need to know their chat_id
because that is one of the required parameters.
为了将其发送给合适的人,我们需要知道他们的chat_id
因为这是必需的参数之一。
Go to the userinfobot then type /start
. The bot will reply with our user information, and our chat_id
is the number in the Id
field.
转到userinfobot,然后键入/start
。 该漫游器将使用我们的用户信息进行回复,而我们的chat_id
是Id
字段中的数字。
Make a function called send_image
. This function will call get_and_save_image
function first to get and save the visualization, then send it to the person whose chat_id is declared in the chat_id
variable.
创建一个名为send_image
的函数。 此函数将首先调用get_and_save_image
函数以获取并保存可视化,然后将其发送给在chat_id
变量中声明chat_id
。
def send_image(bot, update):get_and_save_image()chat_id = 'CHAT_ID_RECEIVER'bot.send_photo(chat_id=chat_id, photo=open('viz.png','rb'))
5.主程序 (5. Main program)
Lastly, create another function called main
to run our program. Don’t forget to change YOUR_TOKEN
with your bot’s token.
最后,创建另一个名为main
函数以运行我们的程序。 别忘了用您的机器人令牌更改 YOUR_TOKEN
。
Remember, this program will send the image automatically based on the day and time we defined.
请记住,该程序将根据我们定义的日期和时间自动发送图像。
For example in this tutorial we will set it to 9:00 AM every day.
例如,在本教程中,我们将其设置为每天9:00 AM。
def main():updater = Updater('YOUR_TOKEN')updater.job_queue.run_daily(send_image, time=datetime.datetime.strptime('9:00AM', '%I:%M%p').time(),days=(0,1,2,3,4,5,6))updater.start_polling()updater.idle()if __name__ == '__main__': main()
At the end your code should look like this:
最后,您的代码应如下所示:
from google.cloud import bigquery
from telegram.ext import Updaterimport matplotlib.pyplot as plt
import numpy as np
import datetimedef query_to_bigquery(query):client = bigquery.Client()query_job = client.query(query)result = query_job.result()dataframe = result.to_dataframe()return dataframedef visualize_bar_chart(x, x_label, y, y_label, title):plt.title(title)plt.xlabel(x_label)plt.ylabel(y_label)index = np.arange(len(x))plt.xticks(index, x, fontsize=5, rotation=30)plt.bar(index, y)return pltdef get_and_save_image():query = """ SELECT DATE(creation_date) date, COUNT(*) total_postsFROM `bigquery-public-data.stackoverflow.post_history`GROUP BY 1HAVING date > DATE_SUB('2018-12-02', INTERVAL 14 DAY)ORDER BY 1"""dataframe = query_to_bigquery(query) x = dataframe['date'].tolist()y = dataframe['total_posts'].tolist()plt = visualize_bar_chart(x=x, x_label='Date', y=y, y_label='Total Posts', title='Daily Posts')plt.savefig('viz.png')def send_image(bot, update):get_and_save_image()chat_id = 'CHAT_ID_RECEIVER'bot.send_photo(chat_id=chat_id, photo=open('viz.png', 'rb'))def main():updater = Updater('YOUR_TOKEN')updater.job_queue.run_daily(send_image, time=datetime.datetime.strptime('9:00AM', '%I:%M%p').time(), days=(0,1,2,3,4,5,6))updater.start_polling()updater.idle()if __name__ == '__main__':main()
Save the file and name it main.py
.
保存文件并将其命名为main.py
Run the program by typing this command in the terminal.python3 main.py
通过在terminal.python3 main.py中键入以下命令来运行程序
Great! Now you have an automatic report generator built with no more than 50 lines of code — pretty cool right?
大! 现在,您已经建立了一个自动报表生成器,该生成器的代码不超过50行-很酷吧?
Go check the bot in here, and type the /send
command to see the example of the image visualization.
在此处检查机器人,然后输入/send
命令以查看图像可视化示例。
The image below shows the visualization that the bot will send. Now you can just sit back, relax, and wait for the bot to send the report to you everyday :)
下图显示了漫游器将发送的可视化图像。 现在,您可以坐下来放松一下,然后等待机器人每天将报告发送给您:)
You can visit my GitHub to get the code, and please do not hesitate to connect and leave a message in my Linkedin profile if you want to ask about anything.
您可以访问我的GitHub以获得代码,如果您有任何疑问,请随时联系并在Linkedin个人资料中留言。
Please leave a comment if you think there are any errors in my code or writing.
如果您认为我的代码或写作有任何错误,请发表评论。
If you have interest in data science or machine learning, you might want to read my post on building sentiment analyzer.
如果您对数据科学或机器学习感兴趣,则可能需要阅读我的关于构建情感分析器的文章 。
Once again, thank you and good luck! :)
再次感谢您,祝您好运! :)
翻译自: https://www.freecodecamp.org/news/how-to-build-a-bot-to-automate-your-mindless-tasks-using-python-and-google-bigquery-a34faf7fb74/
bigquery使用教程
相关文章:

5S后返回首页
1 <!DOCTYPE html>2 <html>3 <head>4 <title>5S后返回首页</title> 5 <meta http-equiv"Content-Type" content"text/html; charsetgkb"/> 6 </head>7 <body>8 <h2>操作成功</h2>…
TypeScript 1
TypeScript 的由来 TypeScript 是 JavaScript 的一个超集,支持 ECMAScript 6 标准。 TypeScript 由微软开发的自由和开源的编程语言。 TypeScript 设计目标是开发大型应用,它可以编译成纯 JavaScript,编译出来的 JavaScript 可以运行在任何…

大龄屌丝自学笔记--Java零基础到菜鸟--028
泛型,for循环增强应用,静态导入,可变参数,asList() 1、泛型 约束了数据类型,格式为 <数据类型>,如:ArrayList<int> aListnew ArrayList<int>(); 泛型通配符:<?…

c# typescript_在任何IDE中从C#,Java或Python代码获取TypeScript接口的简单方法
c# typescriptby Leonardo Carreiro莱昂纳多卡雷罗(Leonardo Carreiro) 在任何IDE中从C#,Java或Python代码获取TypeScript接口的简单方法 (The easy way to get TypeScript interfaces from C#, Java, or Python code in any IDE) Who has never experi…

js里的document对象大全(DOM操作)
什么是DOM document object model 的简称,意思为文档对象模型。主要用来对文档中的html节点进行操作。 Dom的操作简单示例: <div id"t1"><div><input type"file" /> <input type"button" value"…

【制作镜像】BCEC制作镜像
如要制作的新镜像已存在标准版本镜像,即linux发行版本相同(此处指CentOS6.5 64位),可利用BCEC制作。 在BCEC创建centos6.5系统的可联外网的虚机,ssh到此虚机,用yum方式安装所需的功能: yum&…

Ant Design Pro 组件事件绑定 Input onChange
Input 组件的 onChange 事件绑定语法 render() {this.shop_change e > {const { value } e.target;console.log(shop_change,value)};return (<Input placeholder"" onChange{this.shop_change}></Input>)}

软件访问转向本地_我是如何从完整的初学者转向软件开发人员的,以及如何做到的...
软件访问转向本地by Madison Kanna麦迪逊卡纳(Madison Kanna) 我是如何从完整的初学者转向软件开发人员的,以及如何做到的 (How I went from complete beginner to software developer — and how you can too) Two years ago, I was right where you are today.两…

.NET笔试题集(五)
转载于:http://www.cnblogs.com/ForEvErNoME/archive/2012/09/15/2684938.html 1.什么是受管制的代码? 答:unsafe:非托管代码。不经过CLR运行。 2.net Remoting 的工作原理是什么? 答:服务器端向客户端发送…

devServer proxy跨域 设置代理 proxy
概念 什么是同源策略 同源策略是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。 所谓同源是指…

转帖 开源游戏服务器调研
汇总贴 2013年优秀的开源引擎与开源游戏项目 http://mobile.51cto.com/aengine-431122.htm http://www.oschina.net/search?scopeproject&q%E6%89%8B%E6%B8%B8 当前的几种开源游戏服务端介绍 http://www.oschina.net/question/1986738_224669 用户贴,使用过后…

websockets_如何将WebSockets与AWS API Gateway和Lambda一起使用来构建实时应用程序
websocketsby Janitha Tennakoon通过詹妮莎特纳库恩 如何将WebSockets与AWS API Gateway和Lambda一起使用来构建实时应用程序 (How to build real-time applications using WebSockets with AWS API Gateway and Lambda) Recently AWS has announced the launch of a widely-r…

JS对象转URL参数
代码: /*** param 将要转为URL参数字符串的对象* key URL参数字符串的前缀* encode true/false 是否进行URL编码,默认为true* idx ,循环第几次,用&拼接* return URL参数字符串*/ var urlEncode (param,idx, key, encode)> {console.log(idx,idx…

Windows下Redis如何永久更改密码
公司使用的是Spring-session-redis 需要给Redis配置一个密码 本来我配置密码的方法是 先打开Redis服务 在采用 命令 CONFIG SET requirepass "密码" AUTH 密码 但是这样配置完密码之后再重启Redis服务密码会重置 也就是说每次打开Redis服务都要重新再配置一下密码 …

CEGUI-----动画
Animation XML files. <AnimationDefinition> <Affector name‘要被改变的属性名’ interpolator‘关键帧之间平滑过度的数值’> //specifies the name of a property that will be affected (have its value changed) as part of the animation <KeyFrame>…

react hooks使用_如何使用Hooks将React类组件转换为功能组件
react hooks使用by Balaganesh Damodaran通过Balaganesh Damodaran 如何使用Hooks将React类组件转换为功能组件 (How to convert React class components to function components using Hooks) Over the course of the past month, I’ve spent a lot of time working with Re…

[精]Odoo 8.0深入浅出开发教程-模块开发基础
參考资料点击这里.构建Odoo模块模块组成业务对象业务对象声明为Python类, 由Odoo自己主动加载.数据文件XML或CSV文件格式, 在当中声明了元数据(视图或工作流)、配置数据(模块參数)、演示数据等.Web控制器处理Web浏览器发来的requests.静态web数据Web用到的图像, CSS或JavaScrip…

Java基础知识强化之IO流笔记41:字符流缓冲流之复制文本文件案例02(使用 [ newLine() / readLine() ] )(重要)...
1. 使用字符流缓冲流的特殊功能 [ newLine() / readLine() ] 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 数据源: a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader 目的地:…

Ant Design Pro 跳转路由 传参数,接收参数
umi/link 通过声明的方式做路由跳转。 例子: import Link from umi/link;export default () => {<div>/* 普通使用 */<Link to="/list">Go to list page</Link>/* 带参数 */<Link to="/list?a=b">Go to list page</Lin…

编写react组件_React组件的“黄金法则”如何帮助您编写更好的代码
编写react组件以及钩子如何发挥作用 (And how hooks come into play) Recently I’ve adopted a new philosophy that changes the way I make components. It’s not necessarily a new idea but rather a subtle new way of thinking.最近,我采用了一种新的理念&a…

js验证函数摘录
/**本文摘自:http://www.cnblogs.com/rob0121/articles/1776298.html* js各种表单数据验证*/ /**************************************************************************************/ /*************************************数字的验证*********************…

React for循环渲染组件
通常你需要在一个组件中渲染列表。或者循环遍历渲染相同的多个组件,下面看看怎么实现: render() {const options = this.state.data.map(d => <Option key={d.value}>{d.text}</Option>);return (<SelectshowSearchvalue={this.state.value}placeholder={t…

让电脑的灵魂跟你走
想必我这个题目一出来,大家就知道我想写的是电脑远程控制了。 电脑远程控制是为了方便人们随时随地访问自己的电脑,从而进行更加灵活高效的工作。最常见的远程控制是我们利用客户端直接进入后台操作命令行界面。也就是终端shell。 电影里面,黑…

您尝试打开的文件_您是否尝试过重新打开软件团队的身份?
您尝试打开的文件by Victoriya Kalmanovich由Victoriya Kalmanovich 您是否尝试过重新打开软件团队的身份? (Have you tried turning your software team’s identity off and on again?) This series portrays my experience as an R&D group leader of a gr…

vijos 1006 晴天小猪历险记之Hill——数字三角形的终极变化
题目链接:https://vijos.org/p/1006 数字三角形原题看这里:http://www.cnblogs.com/huashanqingzhu/p/7326837.html 背景 在很久很久以前,有一个动物村庄,那里是猪的乐园(^_^),村民们勤劳、勇敢…

电磁学讲义6:高斯定理计算电场
高斯定理是电场力平方反比定律和线性叠加原理的直接结果。也可以由高斯定理作为基本规律导出库仑定律。这说明高斯定理和库仑定律是不同形式的表示电荷和电场关系的同一规律。库仑定律可以使我们从电荷分布求出电场分布,高斯定理可以使我们从电场分布求出电荷分布。…

ant table表格整行点击事件并获取当前行的数据
实现效果:点击表格中某一行,或者点击表格中某一行的一个字段,获取当前行的 item 下标数据,并用 Link 标签传参,下一个页面接收的实现。 如果使用 router 跳转路由传参,需要导入 import router from umi/router; 如果用 Link 跳转路由传参,需要导入 import Link from u…

以太坊公链私链_如何使用以太坊构建汽车制造供应链系统
以太坊公链私链by Marcelo Russi Mergulho由Marcelo RussiMergulho 如何使用以太坊构建汽车制造供应链系统 (How to build a car manufacturing supply chain system using Ethereum) Here at Daitan we are always looking for new technologies that can help our clients s…

微信一次性订阅消息
微信一次性订阅消息官方文档:消息管理>发送一次性订阅消息 开发者可以通过一次性订阅消息授权让微信用户授权第三方移动应用(接入说明)或公众号,获得发送一次订阅消息给到授权微信用户的机会。授权微信用户可以不需要关注公众号…

react控制组件的显示或隐藏, 根据state判断元素显示隐藏 , setState不实时生效解决方法
代码实现功能:根据 state 中的值判断子组件显示或隐藏,因为 setState 不是及时生效的,所以不做显示隐藏判断会报错。 render() {// 客户经理循环组件function CommentSpan(props){const numbers props.managers;if(!numbers) return;const l…