BigQuery PHP 客户端库 getRows(数据表中的映射列)

问题描述 投票:0回答:1

我想从没有循环属性 f,v 的 bigquery 获得响应

$response = $bigquery->jobs->query($projectId, $request);
$rows = $response->getRows();

foreach($rows as $data){
            $date = date('Y-m-d H:i:s',$data->f[6]->v);
            $response[$count]["app_id"] = $data->f[0]->v;
            $response[$count]["uid"] = $data->f[1]->v;
            $response[$count]["account"] = $data->f[2]->v;
            $response[$count]["action"] = $data->f[9]->v;
            $response[$count]["ip"] = $data->f[5]->v;
            $response[$count]["status"] = $data->f[11]->v;
            $response[$count]["created_date"] = $date;

}

预计

$response = $bigquery->jobs->query($projectId, $request);
$rows = $response->getRows();
echo $rows[0]["app_id"];

如何做?是否可以?谁能帮我找到解决办法吗?

php google-bigquery
1个回答
2
投票

我使用此查询作为示例:

SELECT repository_url, 
       repository_has_downloads 
FROM   publicdata:samples.github_timeline
LIMIT  10

首先,您需要从“name”属性构建一个

schema_keys
数组。

$fields = $response->getSchema()->getFields();
$schema_keys = array_flip(array_map(function($o){ return $o->name; }, $fields));

这将保存结果列名称==>索引号:

Array
(
    [repository_url] => 0
    [repository_has_downloads] => 1
)

然后当你循环时:

foreach ($rows as $r) {
    // you need to convert the TableRow in TableCell here
    $table_cell = $r->getF();
    // then you can read columns from it like this
    echo $table_cell[$schema_keys['repository_url']]->getV();
    echo $table_cell[$schema_keys['repository_has_downloads']]->getV();
}
© www.soinside.com 2019 - 2024. All rights reserved.