Insert rows without row IDs in a table.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Use the Storage Write API (REST)](https://docs.cloud.google.com/bigquery/docs/write-api-rest)

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryError.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.InsertAllRequest.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.InsertAllResponse.html;
    import com.google.cloud.bigquery.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.TableId.html;
    import com.google.common.collect.ImmutableList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    // Sample to insert rows without row ids in a table
    public class TableInsertRowsWithoutRowIds {

      public static void main(String[] args) {
        // TODO(developer): Replace these variables before running the sample.
        String datasetName = "MY_DATASET_NAME";
        String tableName = "MY_TABLE_NAME";
        tableInsertRowsWithoutRowIds(datasetName, tableName);
      }

      public static void tableInsertRowsWithoutRowIds(String datasetName, String tableName) {
        try {
          // Initialize client that will be used to send requests. This client only needs to be created
          // once, and can be reused for multiple requests.
          final BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
          // Create rows to insert
          Map<String, Object> rowContent1 = new HashMap<>();
          rowContent1.put("stringField", "Phred Phlyntstone");
          rowContent1.put("numericField", 32);
          Map<String, Object> rowContent2 = new HashMap<>();
          rowContent2.put("stringField", "Wylma Phlyntstone");
          rowContent2.put("numericField", 29);
          InsertAllResponse response =
              bigquery.insertAll(
                  InsertAllRequest.newBuilder(TableId.of(datasetName, tableName))
                      // No row ids disable de-duplication, and also disable the retries in the Java
                      // library.
                      .setRows(
                          ImmutableList.of(
                              InsertAllRequest.RowToInsert.of(rowContent1),
                              InsertAllRequest.RowToInsert.of(rowContent2)))
                      .build());

          if (response.hasErrors()) {
            // If any of the insertions failed, this lets you inspect the errors
            for (Map.Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) {
              System.out.println("Response error: \n" + entry.getValue());
            }
          }
          System.out.println("Rows successfully inserted into table without row ids");
        } catch (BigQueryException e) {
          System.out.println("Insert operation not performed \n" + e.toString());
        }
      }
    }

### PHP


Before trying this sample, follow the PHP setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery PHP API
reference documentation](https://docs.cloud.google.com/php/docs/reference/cloud-bigquery/latest/BigQueryClient).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    use Google\Cloud\BigQuery\BigQueryClient;

    /**
     * Insert rows into the given table with explicitly giving row ids.
     *
     * @param string $projectId The project Id of your Google Cloud Project.
     * @param string $datasetId The BigQuery dataset ID.
     * @param string $tableId The BigQuery table ID.
     * @param string $rowData1 Json encoded data to insert.
     * @param string $rowData2 Json encoded data to insert. For eg,
     *    $rowData1 = json_encode([
     *        "field1" => "value1",
     *        "field2" => "value2"
     *    ]);
     *    $rowData2 = json_encode([
     *        "field1" => "value1",
     *        "field2" => "value2"
     *    ]);
     */
    function table_insert_rows_explicit_none_insert_ids(
        string $projectId,
        string $datasetId,
        string $tableId,
        string $rowData1,
        string $rowData2
    ): void {
        $bigQuery = new BigQueryClient([
            'projectId' => $projectId,
        ]);
        $dataset = $bigQuery->dataset($datasetId);
        $table = $dataset->table($tableId);

        $rowData1 = json_decode($rowData1, true);
        $rowData2 = json_decode($rowData2, true);
        // Omitting insert Id's in following rows.
        $rows = [
            ['data' => $rowData1],
            ['data' => $rowData2]
        ];
        $insertResponse = $table->insertRows($rows);

        if ($insertResponse->isSuccessful()) {
            printf('Rows successfully inserted into table without insert ids' . PHP_EOL);
        } else {
            foreach ($insertResponse->failedRows() as $row) {
                foreach ($row['errors'] as $error) {
                    printf('%s: %s' . PHP_EOL, $error['reason'], $error['message']);
                }
            }
        }
    }

### Python


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/bigquery/latest

    # Construct a BigQuery client object.
    client = https://docs.cloud.google.com/python/docs/reference/bigquery/latest.https://docs.cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html()

    # TODO(developer): Set table_id to the ID of table to append to.
    # table_id = "your-project.your_dataset.your_table"

    rows_to_insert = [
        {"full_name": "Phred Phlyntstone", "age": 32},
        {"full_name": "Wylma Phlyntstone", "age": 29},
    ]

    errors = client.https://docs.cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_insert_rows_json(
        table_id, rows_to_insert, row_ids=[None] * len(rows_to_insert)
    )  # Make an API request.
    if errors == []:
        print("New rows have been added.")
    else:
        print("Encountered errors while inserting rows: {}".format(errors))

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigquery).