简体   繁体   English

如何在 Vue.js 中使用 BigQuery API

[英]How to use BigQuery API in Vue.js

I'm fairly new to Vue.js, but I've built some basic CRUD apps using axios.我是 Vue.js 的新手,但我已经使用 axios 构建了一些基本的 CRUD 应用程序。

What I want to do is use Google Cloud BigQuery to pull in raw data and then display or manipulate it in Vue.我想要做的是使用 Google Cloud BigQuery 拉入原始数据,然后在 Vue 中显示或操作它。 My goal is to make a sort of simple data dashboard where you can filter things or display some different insights from a handful of BigQuery queries.我的目标是制作一种简单的数据仪表板,您可以在其中过滤内容或显示一些 BigQuery 查询的不同见解。

I can install BigQuery API as a dependency from Vue GUI.我可以安装 BigQuery API 作为 Vue GUI 的依赖项。 But after that I'm a little lost.但在那之后我有点失落。 How do I import BigQuery into my code?如何将 BigQuery 导入我的代码? How do I run the example code to fetch some public data?如何运行示例代码以获取一些公共数据?

I'm also unsure how to include the google credentials.我也不确定如何包含谷歌凭据。 I currently have this line in vue.config.js, but unsure if this is correct:我目前在 vue.config.js 中有这一行,但不确定这是否正确:

process.env.VUE_APP_GOOGLE_APPLICATION_CREDENTIALS = '/Google_Cloud_Key/Sandbox-f6ae6239297e.json'

Given the lack of any resources out there for doing this, I also wonder, should I not be trying to retrieve data this way?鉴于缺乏任何资源来执行此操作,我还想知道,我不应该尝试以这种方式检索数据吗? Should I make an intermediate API that runs the BigQuery queries and then returns JSON to my Vue app?我应该制作一个运行 BigQuery 查询然后将 JSON 返回到我的 Vue 应用程序的中间 API 吗?

In order to make requests to the BigQuery API, you need to use a Service Account, which belongs to your project, and it is used by the Google BigQuery Node.js client library to make BigQuery API requests.为了向 BigQuery API 发出请求,您需要使用属于您的项目的服务帐户,Google BigQuery Node.js 客户端库使用它来发出 BigQuery API 请求。

First, set an environment variable with your PROJECT_ID which you will use:首先,使用您将使用的PROJECT_ID设置一个环境变量:

export GOOGLE_CLOUD_PROJECT=$(gcloud config get-value core/project)

Next, create a new service account to access the BigQuery API by using:接下来,使用以下命令创建一个新的服务帐户以访问 BigQuery API:

gcloud iam service-accounts create my-bigquery-sa --display-name "my bigquery service account"

Next, create credentials that your code will use to login as your new service account.接下来,创建您的代码将用于作为新服务帐户登录的凭据。 Create these credentials and save it as a JSON file ~/key.json by using the following command:使用以下命令创建这些凭据并将其保存为 JSON 文件~/key.json

gcloud iam service-accounts keys create ~/key.json --iam-account  my-bigquery-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com

Set the GOOGLE_APPLICATION_CREDENTIALS environment variable, which is used by the BigQuery API library, covered in the next step, to find your credentials.设置GOOGLE_APPLICATION_CREDENTIALS环境变量(下一步将介绍 BigQuery API 库使用该变量)以查找您的凭据。 The environment variable should be set to the full path of the credentials JSON file you created.环境变量应设置为您创建的凭证 JSON 文件的完整路径。 Set the environment variable by using the following command:使用以下命令设置环境变量:

export GOOGLE_APPLICATION_CREDENTIALS="/home/${USER}/key.json"

You can read more about authenticating the BigQuery API .您可以阅读有关验证BigQuery API 的更多信息。

The following example shows how to initialize a client and perform a query on a BigQuery public dataset.以下示例展示了如何初始化客户端并对 BigQuery 公共数据集执行查询。 Moreover in the samples/ directory you can find a lot of examples, such as Extract Table JSON, Get Dataset and many more.此外,在samples/目录中,您可以找到很多示例,例如 Extract Table JSON、Get Dataset 等等。

I hope you find the above pieces of information useful.希望以上信息对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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