插入 WKT 数据
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用 WKT 数据来流式插入到 GEOGRAPHY 列中。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code demonstrates how to stream insert Well-Known Text (WKT) data into a GEOGRAPHY column within a BigQuery table.\u003c/p\u003e\n"],["\u003cp\u003eThe example utilizes the Shapely library in Python and the RGeo library in Ruby to generate WKT data representing a line between LAX and JFK airports.\u003c/p\u003e\n"],["\u003cp\u003eBefore running, you need to set up Application Default Credentials for authentication and follow the BigQuery quickstart instructions for client libraries.\u003c/p\u003e\n"],["\u003cp\u003eThe provided examples show that the target table needs to have an existing column with the GEOGRAPHY data type, for which WKT data can be inserted.\u003c/p\u003e\n"],["\u003cp\u003eWKT data can either be defined directly or generated using the included libraries.\u003c/p\u003e\n"]]],[],null,["Streaming insert into GEOGRAPHY column with WKT data.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Working with geospatial data](/bigquery/docs/geospatial-data)\n\nCode sample \n\nPython\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 import shapely.geometry\n import shapely.wkt\n\n bigquery_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 # This example uses a table containing a column named \"geo\" with the\n # GEOGRAPHY data type.\n table_id = \"my-project.my_dataset.my_table\"\n\n # Use the Shapely library to generate WKT of a line from LAX to\n # JFK airports. Alternatively, you may define WKT data directly.\n my_geography = shapely.geometry.LineString(\n [(-118.4085, 33.9416), (-73.7781, 40.6413)]\n )\n rows = [\n # Convert data into a WKT string.\n {\"geo\": shapely.wkt.dumps(my_geography)},\n ]\n\n # table already exists and has a column\n # named \"geo\" with data type GEOGRAPHY.\n errors = bigquery_client.https://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)\n if errors:\n raise RuntimeError(f\"row insert failed: {errors}\")\n else:\n print(f\"wrote 1 row to {table_id}\")\n\nRuby\n\n\nBefore trying this sample, follow the Ruby 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 Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-bigquery/latest/Google/Cloud/Bigquery.html).\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 require \"google/cloud/bigquery\"\n require \"rgeo\"\n\n def insert_geography_wkt dataset_id = \"your_dataset_id\", table_id = \"your_table_id\"\n bigquery = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery-analytics_hub/latest/Google-Cloud-Bigquery.html.https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery.html\n dataset = bigquery.dataset dataset_id\n table = dataset.table table_id\n\n # Use the RGeo library to generate WKT of a line from LAX to\n # JFK airports. Alternatively, you may define WKT data directly.\n factory = RGeo::Geographic.spherical_factory\n my_line = factory.line_string([factory.point(-118.4085, 33.9416), factory.point(-73.7781, 40.6413)])\n row_data = [\n # Convert data into a WKT string: \"LINESTRING (-118.4085 33.9416, -73.7781 40.6413)\"\n { geo: my_line.as_text }\n ]\n\n # Table already exists and has a column named \"geo\" with data type GEOGRAPHY.\n response = table.insert row_data\n\n if response.success?\n puts \"Inserted GEOGRAPHY WKT row successfully\"\n else\n puts \"GEOGRAPHY WKT row insert failed: #{response.error_rows.https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery-reservation-v1/latest/Google-Cloud-Bigquery-Reservation-V1-SplitCapacityCommitmentResponse.html&.errors}\"\n end\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]