BigQuery + Angular + Firebase Functions 结果受超大尺寸限制
创始人
2024-12-12 03:01:34
0

在BigQuery + Angular + Firebase Functions中,由于BigQuery具有超大尺寸限制,当查询结果超过限制时,可以采取以下解决方法之一:

  1. 分页查询:将查询结果分为多个较小的页面,使用BigQuery的limit和offset参数来限制每个页面的大小。在Angular中,可以使用分页组件来处理分页逻辑,并通过Firebase Functions来执行不同页面的查询。
// Angular Component

import { Component } from '@angular/core';
import { AngularFireFunctions } from '@angular/fire/functions';

@Component({
  selector: 'app-results',
  templateUrl: './results.component.html',
  styleUrls: ['./results.component.css']
})
export class ResultsComponent {
  currentPage: number = 1;
  pageSize: number = 10;
  totalResults: number = 0;
  results: any[] = [];

  constructor(private functions: AngularFireFunctions) {}

  ngOnInit() {
    this.loadResults();
  }

  loadResults() {
    const query = this.functions.httpsCallable('queryBigQuery');
    query({ page: this.currentPage, pageSize: this.pageSize }).subscribe(
      (response: any) => {
        this.totalResults = response.totalResults;
        this.results = response.results;
      },
      (error: any) => {
        console.error(error);
      }
    );
  }

  nextPage() {
    this.currentPage++;
    this.loadResults();
  }

  previousPage() {
    this.currentPage--;
    this.loadResults();
  }
}
// Firebase Functions

const { BigQuery } = require('@google-cloud/bigquery');

exports.queryBigQuery = functions.https.onCall(async (data, context) => {
  const page = data.page || 1;
  const pageSize = data.pageSize || 10;

  const bigquery = new BigQuery();
  const query = `SELECT * FROM myTable LIMIT ${pageSize} OFFSET ${(page - 1) * pageSize}`;
  const options = {
    query: query,
    location: 'US',
  };

  const [rows] = await bigquery.query(options);

  const totalResults = rows.length; // Total results for pagination
  const results = rows.map(row => row); // Modify this based on your actual results structure

  return {
    totalResults: totalResults,
    results: results
  };
});
  1. 使用BigQuery导出功能:如果查询结果非常庞大,无法通过分页查询来处理,可以考虑使用BigQuery的导出功能,将查询结果导出到Google Cloud Storage(GCS)中。然后,可以使用Angular和Firebase Functions从GCS读取导出的文件并进行处理。
// Angular Component

import { Component } from '@angular/core';
import { AngularFireStorage } from '@angular/fire/storage';
import { AngularFireFunctions } from '@angular/fire/functions';

@Component({
  selector: 'app-results',
  templateUrl: './results.component.html',
  styleUrls: ['./results.component.css']
})
export class ResultsComponent {
  downloadUrl: string = '';
  loading: boolean = false;

  constructor(private storage: AngularFireStorage, private functions: AngularFireFunctions) {}

  ngOnInit() {
    this.executeQuery();
  }

  executeQuery() {
    const query = this.functions.httpsCallable('executeBigQuery');
    query({}).subscribe(
      (response: any) => {
        this.downloadUrl = response.downloadUrl;
      },
      (error: any) => {
        console.error(error);
      }
    );
  }

  downloadResults() {
    this.loading = true;

    const ref = this.storage.refFromURL(this.downloadUrl);
    ref.getDownloadURL().subscribe(
      (url: string) => {
        window.open(url, '_blank');
        this.loading = false;
      },
      (error: any) => {
        console.error(error);
        this.loading = false;
      }
    );
  }
}
// Firebase Functions

const { BigQuery } = require('@google-cloud/bigquery');

exports.executeBigQuery = functions.https.onCall(async (data, context) => {
  const bigquery = new BigQuery();
  const query = `SELECT * FROM myTable`;
  const options = {
    query: query,
    location: 'US',
  };

  const [job] = await bigquery.createQueryJob(options);

  // Wait for the job to complete
  await job.getQueryResults();

  const [files] = await job.getQueryResults({ destinationFormat: 'CSV', gzip: true });
  const [file] = files;

  const bucketName = file.metadata.bucket;
  const fileName = file.metadata.name;
  const downloadUrl = `https://

相关内容

热门资讯

Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...
Aksnginxdomainb... 在AKS集群中,可以使用Nginx代理服务器实现根据域名进行路由。以下是具体步骤:部署Nginx i...
AddSingleton在.N... 在C#中创建Singleton对象通常是通过私有构造函数和静态属性来实现,例如:public cla...
Alertmanager中的基... Alertmanager中可以使用repeat_interval选项指定在一个告警重复发送前必须等待...