使用Node.js编程定义表模式

时间:2019-05-23 12:53:22

标签: node.js typescript google-bigquery

我试图通过以下方式使用nodejs“ @ google-cloud / bigquery”:“ ^ 3.0.0”自动定义创建表:

    const bigqueryClient = new BigQuery();
    schema = `driverId:string, passengerIds:(repeated string), pickedUp:(repeated string), createdat: integer, id:string, point:geography`;
    try {
            const table = await bigqueryClient.dataset(DATASET).table(tableName);
            const insertOptions: InsertRowsOptions = {
                autoCreate: true,
                schema: schema,
             }
            const response: InsertRowsResponse = await table.insert(JSON.parse(JSON.stringify(document)), insertOptions);
            console.log(`insert completed: ${JSON.stringify(response)}`);
        }
        catch(err) {
            // An API error or partial failure occurred.
            console.error(`failed to insert: ${JSON.stringify(err)}`);
        }

问题是,如果使用原始类型,就没有问题,但是我正在努力寻找如何正确声明可重复类型以及记录类型的方法。有人可以给我一些指导吗?

1 个答案:

答案 0 :(得分:1)

好吧,终于找到了解决方法,只需将模式作为以python示例中完成方式定义的对象传递即可,例如:

 schema = [
    {
        "name": "driverId",
        "type": "STRING",
        "mode": "NULLABLE"
    },
    {
        "name": "passengerIds",
        "type": "STRING",
        "mode": "REPEATED"
    },
    {
        "name": "pickedUp",
        "type": "STRING",
        "mode": "REPEATED"
    },
    {
        "name": "createdat",
        "type": "integer",
        "mode": "NULLABLE"
    },
    {
        "name": "id",
        "type": "STRING",
        "mode": "NULLABLE"
    },
    {
        "name": "POINT",
        "type": "GEOGRAPHY",
        "mode": "NULLABLE"
    }
]