Insert rows with no IDs
Stay organized with collections
Save and categorize content based on your preferences.
Insert rows without row IDs in a table.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides code samples in Java, PHP, and Python for inserting rows into a BigQuery table without using row IDs.\u003c/p\u003e\n"],["\u003cp\u003eOmitting row IDs disables de-duplication, and in the case of Java, disables retries in the library.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples showcase how to insert multiple rows into a BigQuery table by creating row data in the form of hashmaps or JSON format and sending them as data to insert.\u003c/p\u003e\n"],["\u003cp\u003eThe code examples also demonstrate how to handle potential errors during the insertion process, with code examples illustrating handling and providing error messages if it was unsuccessful.\u003c/p\u003e\n"],["\u003cp\u003eThe document also provides instructions on how to set up authentication for client libraries and links to the relevant API reference documentations for Java, PHP, and Python.\u003c/p\u003e\n"]]],[],null,["# Insert rows with no IDs\n\nInsert rows without row IDs in a table.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Use the legacy streaming API](/bigquery/docs/streaming-data-into-bigquery)\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryError.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.InsertAllRequest.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.InsertAllResponse.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.TableId.html;\n import com.google.common.collect.ImmutableList;\n import java.util.HashMap;\n import java.util.List;\n import java.util.Map;\n\n // Sample to insert rows without row ids in a table\n public class TableInsertRowsWithoutRowIds {\n\n public static void main(String[] args) {\n // TODO(developer): Replace these variables before running the sample.\n String datasetName = \"MY_DATASET_NAME\";\n String tableName = \"MY_TABLE_NAME\";\n tableInsertRowsWithoutRowIds(datasetName, tableName);\n }\n\n public static void tableInsertRowsWithoutRowIds(String datasetName, String tableName) {\n try {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n final BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();\n // Create rows to insert\n Map\u003cString, Object\u003e rowContent1 = new HashMap\u003c\u003e();\n rowContent1.put(\"stringField\", \"Phred Phlyntstone\");\n rowContent1.put(\"numericField\", 32);\n Map\u003cString, Object\u003e rowContent2 = new HashMap\u003c\u003e();\n rowContent2.put(\"stringField\", \"Wylma Phlyntstone\");\n rowContent2.put(\"numericField\", 29);\n InsertAllResponse response =\n bigquery.insertAll(\n InsertAllRequest.newBuilder(TableId.of(datasetName, tableName))\n // No row ids disable de-duplication, and also disable the retries in the Java\n // library.\n .setRows(\n ImmutableList.of(\n InsertAllRequest.RowToInsert.of(rowContent1),\n InsertAllRequest.RowToInsert.of(rowContent2)))\n .build());\n\n if (response.hasErrors()) {\n // If any of the insertions failed, this lets you inspect the errors\n for (Map.Entry\u003cLong, List\u003cBigQueryError\u003e\u003e entry : response.getInsertErrors().entrySet()) {\n System.out.println(\"Response error: \\n\" + entry.getValue());\n }\n }\n System.out.println(\"Rows successfully inserted into table without row ids\");\n } catch (BigQueryException e) {\n System.out.println(\"Insert operation not performed \\n\" + e.toString());\n }\n }\n }\n\n### PHP\n\n\nBefore trying this sample, follow the PHP setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery PHP API\nreference documentation](/php/docs/reference/cloud-bigquery/latest/BigQueryClient).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n use Google\\Cloud\\BigQuery\\BigQueryClient;\n\n /**\n * Insert rows into the given table with explicitly giving row ids.\n *\n * @param string $projectId The project Id of your Google Cloud Project.\n * @param string $datasetId The BigQuery dataset ID.\n * @param string $tableId The BigQuery table ID.\n * @param string $rowData1 Json encoded data to insert.\n * @param string $rowData2 Json encoded data to insert. For eg,\n * $rowData1 = json_encode([\n * \"field1\" =\u003e \"value1\",\n * \"field2\" =\u003e \"value2\"\n * ]);\n * $rowData2 = json_encode([\n * \"field1\" =\u003e \"value1\",\n * \"field2\" =\u003e \"value2\"\n * ]);\n */\n function table_insert_rows_explicit_none_insert_ids(\n string $projectId,\n string $datasetId,\n string $tableId,\n string $rowData1,\n string $rowData2\n ): void {\n $bigQuery = new BigQueryClient([\n 'projectId' =\u003e $projectId,\n ]);\n $dataset = $bigQuery-\u003edataset($datasetId);\n $table = $dataset-\u003etable($tableId);\n\n $rowData1 = json_decode($rowData1, true);\n $rowData2 = json_decode($rowData2, true);\n // Omitting insert Id's in following rows.\n $rows = [\n ['data' =\u003e $rowData1],\n ['data' =\u003e $rowData2]\n ];\n $insertResponse = $table-\u003einsertRows($rows);\n\n if ($insertResponse-\u003eisSuccessful()) {\n printf('Rows successfully inserted into table without insert ids' . PHP_EOL);\n } else {\n foreach ($insertResponse-\u003efailedRows() as $row) {\n foreach ($row['errors'] as $error) {\n printf('%s: %s' . PHP_EOL, $error['reason'], $error['message']);\n }\n }\n }\n }\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Python API\nreference documentation](/python/docs/reference/bigquery/latest).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n\n # Construct a BigQuery client object.\n client = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html()\n\n # TODO(developer): Set table_id to the ID of table to append to.\n # table_id = \"your-project.your_dataset.your_table\"\n\n rows_to_insert = [\n {\"full_name\": \"Phred Phlyntstone\", \"age\": 32},\n {\"full_name\": \"Wylma Phlyntstone\", \"age\": 29},\n ]\n\n errors = client.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html#google_cloud_bigquery_client_Client_insert_rows_json(\n table_id, rows_to_insert, row_ids=[None] * len(rows_to_insert)\n ) # Make an API request.\n if errors == []:\n print(\"New rows have been added.\")\n else:\n print(\"Encountered errors while inserting rows: {}\".format(errors))\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]