/**
* Insert records into the table without running a load job.
*
* Example:
* ```
* $rows = [
* [
* 'insertId' => '1',
* 'data' => [
* 'city' => 'Detroit',
* 'state' => 'MI'
* ]
* ],
* [
* 'insertId' => '2',
* 'data' => [
* 'city' => 'New York',
* 'state' => 'NY'
* ]
* ]
* ];
*
* $insertResponse = $table->insertRows($rows);
*
* if (!$insertResponse->isSuccessful()) {
* foreach ($insertResponse->failedRows() as $row) {
* print_r($row['rowData']);
*
* foreach ($row['errors'] as $error) {
* echo $error['reason'] . ': ' . $error['message'] . PHP_EOL;
* }
* }
* }
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll Tabledata insertAll API Documentation.
* @see https://cloud.google.com/bigquery/streaming-data-into-bigquery Streaming data into BigQuery.
*
* @param array $rows The rows to insert. Each item in the array must
* contain a `data` key which is to hold a key/value array with data
* matching the schema of the table. Optionally, one may also provide
* an `insertId` key which will be used to
* [ensure data consistency](https://cloud.google.com/bigquery/streaming-data-into-bigquery#dataconsistency).
* @param array $options [optional] {
* Configuration options.
*
* @type bool $skipInvalidRows Insert all valid rows of a request, even
* if invalid rows exist. The default value is `false`, which
* causes the entire request to fail if any invalid rows exist.
* **Defaults to** `false`.
* @type bool $ignoreUnknownValues Accept rows that contain values that
* do not match the schema. The unknown values are ignored.
* The default value is `false`, which treats unknown values as errors.
* **Defaults to** `false`.
* @type string $templateSuffix If specified, treats the destination
* table as a base template, and inserts the rows into an instance
* table named "{destination}{templateSuffix}". BigQuery will
* manage creation of the instance table, using the schema of the
* base template table. See
* [Creating tables automatically using template tables](https://cloud.google.com/bigquery/streaming-data-into-bigquery#template-tables)
* for considerations when working with templates tables.
* }
* @return InsertResponse
* @throws \InvalidArgumentException
* @codingStandardsIgnoreEnd
*/
public function insertRows(array $rows, array $options = [])
{
foreach ($rows as $row) {
if (!isset($row['data'])) {
throw new \InvalidArgumentException('A row must have a data key.');
}
$row['json'] = $row['data'];
unset($row['data']);
$options['rows'][] = $row;
}
return new InsertResponse($this->connection->insertAllTableData($this->identity + $options), $options['rows']);
}