2016-11-17 88 views
1

我想從BigQuery的應答,而不循環特性F,VBigQuery的PHP客戶端庫GetRows的(DataTable中映射列)

$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"]; 

如何?可能嗎?任何人都可以幫我找到解決方案嗎?

+0

你有一個率低。重要的是,您必須使用投票下方發佈答案左側的勾號標記接受的答案。這會增加你的速度。看看這個工程通過visinting這個鏈接:http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work#5235 – Pentium10

+0

好吧,我知道。 –

+0

不錯,但你有幾個問題,你沒有采取完整的行動,修改那些 – Pentium10

回答

1

我使用這個查詢爲例:

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

首先,你需要從「名」屬性建立一個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(); 
} 
+0

這正是我想要做的,謝謝! –