Google BigQuery是一个完全托管的企业级数据仓库,提供超大规模数据分析能力。它使用SQL语法并能够处理PB级数据,采用无服务器架构,用户无需管理基础设施。
composer require google/cloud-bigquery
创建服务账号并下载JSON密钥文件,设置环境变量:
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
require 'vendor/autoload.php';
use Google\Cloud\BigQuery\BigQueryClient;
function runQuery($projectId, $query) {
$bigQuery = new BigQueryClient([
'projectId' => $projectId,
]);
$queryJobConfig = $bigQuery->query($query);
$queryResults = $bigQuery->runQuery($queryJobConfig);
foreach ($queryResults as $row) {
print_r($row);
}
}
// 使用示例
$projectId = 'your-project-id';
$query = 'SELECT name, count FROM `bigquery-public-data.usa_names.usa_1910_current` LIMIT 10';
runQuery($projectId, $query);
function runParameterizedQuery($projectId) {
$bigQuery = new BigQueryClient([
'projectId' => $projectId,
]);
$query = 'SELECT name, count
FROM `bigquery-public-data.usa_names.usa_1910_current`
WHERE gender = @gender AND state = @state
LIMIT 10';
$queryParameters = [
'gender' => 'F',
'state' => 'CA'
];
$jobConfig = $bigQuery->query($query)
->parameters($queryParameters);
$queryResults = $bigQuery->runQuery($jobConfig);
foreach ($queryResults as $row) {
print_r($row);
}
}
原因:服务账号密钥文件路径错误或权限不足 解决:检查密钥文件路径和环境变量设置,确保服务账号有足够权限
原因:查询处理时间过长或数据量过大 解决:优化查询语句,添加LIMIT子句,考虑使用分区表
原因:结果集过大 解决:使用分页或流式处理结果
$queryResults = $bigQuery->runQuery($jobConfig);
$iterator = $queryResults->getIterator();
$iterator->rewind();
while ($iterator->valid()) {
$row = $iterator->current();
print_r($row);
$iterator->next();
}
原因:PHP和BigQuery数据类型转换问题 解决:明确指定数据类型或进行类型转换
function insertRows($projectId, $datasetId, $tableId) {
$bigQuery = new BigQueryClient([
'projectId' => $projectId,
]);
$dataset = $bigQuery->dataset($datasetId);
$table = $dataset->table($tableId);
$rows = [
['name' => 'Alice', 'age' => 30],
['name' => 'Bob', 'age' => 25]
];
$insertResponse = $table->insertRows($rows);
if ($insertResponse->failedRows()) {
print_r($insertResponse->failedRows());
} else {
echo 'Data inserted successfully';
}
}
function streamInsert($projectId, $datasetId, $tableId) {
$bigQuery = new BigQueryClient([
'projectId' => $projectId,
]);
$dataset = $bigQuery->dataset($datasetId);
$table = $dataset->table($tableId);
$row = [
'insertId' => uniqid(),
'data' => ['name' => 'Charlie', 'age' => 28]
];
$table->insertRow($row);
}
通过以上方法,您可以在PHP应用程序中高效地使用Google BigQuery进行数据查询和分析。
没有搜到相关的文章