【问题标题】:How to insert geography type data using bigquery insert method with nodejs如何使用带有 nodejs 的 bigquery 插入方法插入地理类型数据
【发布时间】:2020-07-31 14:22:35
【问题描述】:

我正在使用 Node 库与 BigQuery 集成。想知道如何使用 Table.insert 方法将 Geography 数据类型值存储在 Table 中。我尝试了一些类似下面的东西,但给出了错误

const options = {
datasetId: 'mydataset',
tableId: 'mytable', 
rows: [
  {
    rec1:"raj",
    rec2:"geography::Point('-6', '6', 4326)"
  }
] }

const table = await bigqueryClient
.dataset(options.datasetId)
.table(options.tableId) 
table.insert(options.rows, (err, resp) => {
if (err) {
  console.log('*****err', err)
} else {
  console.log('*****resp', resp)
}  })

或者实现这一点的唯一方法是在 BigQuery 客户端上使用查询方法,查询格式如下?

insert `bigqueryproject1-279307.mydataset.mytable` (rec1,rec2) values ('raj', ST_GEOGPOINT(-6, 6) )

【问题讨论】:

    标签: node.js google-bigquery


    【解决方案1】:

    首先尝试使用架构创建表。类似this

    const schema = [
        {
        name: 'rec1',
        type: 'STRING',
        },
        {
        name: 'rec2',
        type: 'GEOGRAPHY',
        }
    ];
        
    const options = {
        schema: schema,
    };
    
    const [table] = await bigquery
        .dataset('mydataset')
        .createTable('mytable', options);
    

    然后尝试插入具有 GEOG 数据类型的数据。我建议使用 geojson 格式的 geom 数据,而 wkt 格式有时无法正确导入(根据我的经验)

    {    
        "type": "Point",
        "coordinates": [
            -6,
            6
        ]
    }
    

    【讨论】:

    • 嗨 Jozef,您的意思是像这样传递价值? const options = { datasetId: 'mycreatedataset1', tableId: 'newTabelwithAllDatatype', rows: [ { rec1:"raj", rec11:{ "type": "Feature", "properties": {}, "geometry": { " type": "Point", "coordinates": [ -6, 6] } } } ] } 同时创建新行?
    • 是的,类似这样,但是将您的 JSON 字符串化并像字符串一样使用它
    • 我得到以下错误“无法转换值 '{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\ ":\"Point\",\"coordinates\":[-6,6]}}' 到 GEOGRAPHY:GeoJSON 阅读器接受 Geometry 对象,而不是 Feature 或 FeatureCollection 对象","re​​ason":"invalid"}],"行":{"id":"sidd","location":"{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\ ":\"点\",\"坐标\":[-6,6]}}"}}],"名称"
    • 我的错,我更正了我的答案。您只需要几何内部的 json
    • 使用这种格式可以正常工作 { "type": "GeometryCollection", "geometries": [{ "type": "Point", "coordinates": [-70, 50] }] }
    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多