简体   繁体   English

在 Google Bigquery 中创建表 SQL 语法

[英]Create table SQL syntax in Google Bigquery

I've been reading the bigquery documentation since late last night and understand very little of it.自昨晚深夜以来,我一直在阅读bigquery 文档,但对它了解甚少。 It talks about loading data via different methods, but doesn't say how to create the table that I'm going to load data into.它讨论了通过不同的方法加载数据,但没有说明如何创建我要将数据加载到其中的表。 When I use the web UI it expects me to type out the schema.当我使用 web UI 时,它希望我输入模式。 My table has over 400 columns.我的表有 400 多列。 I will not type out hundreds of column names, types and lengths.我不会键入数百个列名称、类型和长度。

I've been uploading hundreds of GB of data in csv format to a google bucket.我一直在将 csv 格式的数百 GB 数据上传到谷歌存储桶。 The csv files do not have column names. csv 文件没有列名。 I have the schema in sql format which I prefer to use.我有我喜欢使用的 sql 格式的模式。

If I try creating a table through a query I get an error already on line 2 that says,如果我尝试通过查询创建一个表,我会在第 2 行收到一条错误消息,上面写着:

"Error: Encountered "" at line 2, column 1."

CREATE TABLE [example-mdi:myData_1.ST] (
`ADDRESS_ID` varchar(9),
`INDIVIDUAL_ID` varchar(2),
`FIRST_NAME` varchar(25),
`LAST_NAME` varchar(2),...

How can I do this or what is the right way?我该怎么做或者什么是正确的方法?

You can use a CREATE TABLE statement to create the table using standard SQL.您可以使用CREATE TABLE语句使用标准 SQL 创建表。 In your case the statement would look something like this:在您的情况下,该语句将如下所示:

CREATE TABLE `example-mdi.myData_1.ST` (
  `ADDRESS_ID` STRING,
  `INDIVIDUAL_ID` STRING,
  `FIRST_NAME` STRING,
  `LAST_NAME` STRING,
  ...
);

Mikhail is right and gets credit for the answer.米哈伊尔是对的,并因答案而受到赞誉。 If you're as slow as me you're going to want more details, because after he pointed me the right way it still took a while to figure out what he's talking about and how to get it done.如果你和我一样慢,你会想要更多细节,因为在他向我指出正确的方法之后,仍然需要一段时间才能弄清楚他在说什么以及如何完成它。

When you're at the create table user interface click the "edit as text" link.当您在创建表用户界面时,单击“编辑为文本”链接。

在此处输入图片说明

In the text box input that pops up you'll enter something like:在弹出的文本框输入中,您将输入如下内容:

ADDRESS_ID:string,
INDIVIDUAL_ID:string,
First_name:string,
Last_name:string...

Hyphens are not permitted.不允许使用连字符。

When you create table in Web UI - you can enter schema field by field ( Edit as Fields mode - default mode) or you can enter schema as a text ( Edit as Text mode)在 Web UI 中创建表时 - 您可以逐个字段输入架构( Edit as Fields模式 - 默认模式),也可以将架构作为文本输入( Edit as Text模式)
So, if you already have your schema in sql format you can just use it (you will might need to slightly adjust it to conform with BigQuery)因此,如果您已经拥有 sql 格式的架构,则可以使用它(您可能需要稍微调整它以符合 BigQuery)

See more about creating tables with different clients (in We bUI section above option is not presented, so that's why you missed it I think)查看有关使用不同客户端创建表的更多信息(在上面的 We bUI 部分中未提供选项,因此我认为这就是您错过它的原因)

PS As of today, BigQuery doe not support DDL - so CREATE TABLE is not available PS 截至今天,BigQuery 不支持 DDL - 所以CREATE TABLE不可用

Update更新

As of today - Jan 17, 2018 - BigQuerydata definition language support is now in Beta截至今天 - 2018 年 1 月 17 日 - BigQuerydata definition language支持现在处于Beta

As of April 2021, complex tables can be fully created directly using standard SQL (see the specific syntax guide ).截至 2021 年 4 月,可以使用标准 SQL 直接完全创建复杂表(请参阅特定语法指南)。

CREATE TABLE IF NOT EXISTS `project.dataset.table_name`
(
    someName STRING
    , dateTime TIMESTAMP NOT NULL -- REQUIRED or non-null column
    , index INT64 -- INT64 for INTEGER column
    , longitude FLOAT64 -- FLOAT64 for FLOAT column
    , arr ARRAY< -- declaring Array. This ARRAY is of datatype STRUCT
        STRUCT< -- declaring STRUCT
            a FLOAT64 -- the individual STRUCT members do not need the STRUCT column name again
            , b STRING
        >
    >
);

This is all new to me and I'm trying to insert a table and this is the code I'm using but I am getting the error code: Syntax error: Expected end of input but got keyword CREATE at [2:1]这对我来说是全新的,我正在尝试插入一个表,这是我正在使用的代码,但我收到错误代码:语法错误:预期输入结束但在 [2:1] 处获得关键字 CREATE

Drop Table if exists new-project-369802.Covid.PercentPopulationVaccinated Create Table new-project-369802.Covid.PercentPopulationVaccinated ( Continent STRING(255), Location STRING(255), Date datetime, Population numeric, New_Vaccinations numeric, RollingPeopleVaccinated numeric ) INSERT INTO new-project-369802.Covid.PercentPopulationVaccinated SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(cast(vac.new_vaccinations as int)) OVER(Partition BY dea.location ORDER BY dea.location, dea.date) AS RollingPeopleVaccinated FROM new-project-369802.Covid.covid_deaths AS dea JOIN new-project-369802.Covid.covid_vaccinations AS vac ON dea.location = vac.location and dea.date = vac.date WHERE dea.continent is not null如果存在new-project-369802.Covid.PercentPopulationVaccinated创建表new-project-369802.Covid.PercentPopulationVaccinated (大陆字符串(255),位置字符串(255),日期日期时间,人口数字,New_Vaccinations 数字,RollingPeopleVaccinated 数字)插入new-project-369802.Covid.PercentPopulationVaccinated SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations, SUM(cast(vac.new_vaccinations as int)) OVER(Partition BY dea.location ORDER BY dea.location, dea.date) AS RollingPeopleVaccinated FROM new-project-369802.Covid.covid_deaths AS dea JOIN new-project-369802.Covid.covid_vaccinations AS vac ON dea.location = vac.location and dea.date = vac. dea.continent 不是 null 的日期

SELECT *,(rolling_count_vaccinated/population)*100 AS 
rolling_count_percentage
FROM `new-project-369802.Covid.PercentPopulationVaccinated`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM