How do you programmatically create a Google BigQuery schema in a tuple?
We are trying to create a table in BigQuery. We have code working from a Google example that creates the schema - but the code specifying the schema is in a tuple.
The code looks like this:
table.schema = (
bigquery.SchemaField('Name', 'STRING'),
bigquery.SchemaField('Age', 'INTEGER'),
bigquery.SchemaField('Weight', 'FLOAT'),
)
The problem we face is that we have the header fields we need in a list (i.e. [“Name”, “Age”, “Weight”]) - but as we understand it - tuples are immutable.
As a result, we are unable to do code like this because it throws an immutable object error:
for field in field_list:
table.schema.append(field, ‘STRING’)
How do we create the table schema from a list of headers when the tuple structure is immutable? How do we create all the fields in the object?
python google-bigquery tuples
add a comment |
We are trying to create a table in BigQuery. We have code working from a Google example that creates the schema - but the code specifying the schema is in a tuple.
The code looks like this:
table.schema = (
bigquery.SchemaField('Name', 'STRING'),
bigquery.SchemaField('Age', 'INTEGER'),
bigquery.SchemaField('Weight', 'FLOAT'),
)
The problem we face is that we have the header fields we need in a list (i.e. [“Name”, “Age”, “Weight”]) - but as we understand it - tuples are immutable.
As a result, we are unable to do code like this because it throws an immutable object error:
for field in field_list:
table.schema.append(field, ‘STRING’)
How do we create the table schema from a list of headers when the tuple structure is immutable? How do we create all the fields in the object?
python google-bigquery tuples
Is there a reason not to execute a DDL statement as a query instead? cloud.google.com/bigquery/docs/reference/standard-sql/…
– Elliott Brossard
Nov 17 '18 at 14:18
What do you mean by the header fields are in a list? Do you have an example?
– Willian Fuks
Nov 17 '18 at 14:22
@WillianFuks Updated the question with the example list.
– Praxiteles
Nov 17 '18 at 14:30
Can you show what you tried so far?
– Martin Weitzmann
Nov 17 '18 at 15:51
i am not sure what exactly the problem :o( can you elaborate please?
– Mikhail Berlyant
Nov 17 '18 at 18:12
add a comment |
We are trying to create a table in BigQuery. We have code working from a Google example that creates the schema - but the code specifying the schema is in a tuple.
The code looks like this:
table.schema = (
bigquery.SchemaField('Name', 'STRING'),
bigquery.SchemaField('Age', 'INTEGER'),
bigquery.SchemaField('Weight', 'FLOAT'),
)
The problem we face is that we have the header fields we need in a list (i.e. [“Name”, “Age”, “Weight”]) - but as we understand it - tuples are immutable.
As a result, we are unable to do code like this because it throws an immutable object error:
for field in field_list:
table.schema.append(field, ‘STRING’)
How do we create the table schema from a list of headers when the tuple structure is immutable? How do we create all the fields in the object?
python google-bigquery tuples
We are trying to create a table in BigQuery. We have code working from a Google example that creates the schema - but the code specifying the schema is in a tuple.
The code looks like this:
table.schema = (
bigquery.SchemaField('Name', 'STRING'),
bigquery.SchemaField('Age', 'INTEGER'),
bigquery.SchemaField('Weight', 'FLOAT'),
)
The problem we face is that we have the header fields we need in a list (i.e. [“Name”, “Age”, “Weight”]) - but as we understand it - tuples are immutable.
As a result, we are unable to do code like this because it throws an immutable object error:
for field in field_list:
table.schema.append(field, ‘STRING’)
How do we create the table schema from a list of headers when the tuple structure is immutable? How do we create all the fields in the object?
python google-bigquery tuples
python google-bigquery tuples
edited Nov 18 '18 at 1:59
Praxiteles
asked Nov 17 '18 at 14:05
PraxitelesPraxiteles
2,70132338
2,70132338
Is there a reason not to execute a DDL statement as a query instead? cloud.google.com/bigquery/docs/reference/standard-sql/…
– Elliott Brossard
Nov 17 '18 at 14:18
What do you mean by the header fields are in a list? Do you have an example?
– Willian Fuks
Nov 17 '18 at 14:22
@WillianFuks Updated the question with the example list.
– Praxiteles
Nov 17 '18 at 14:30
Can you show what you tried so far?
– Martin Weitzmann
Nov 17 '18 at 15:51
i am not sure what exactly the problem :o( can you elaborate please?
– Mikhail Berlyant
Nov 17 '18 at 18:12
add a comment |
Is there a reason not to execute a DDL statement as a query instead? cloud.google.com/bigquery/docs/reference/standard-sql/…
– Elliott Brossard
Nov 17 '18 at 14:18
What do you mean by the header fields are in a list? Do you have an example?
– Willian Fuks
Nov 17 '18 at 14:22
@WillianFuks Updated the question with the example list.
– Praxiteles
Nov 17 '18 at 14:30
Can you show what you tried so far?
– Martin Weitzmann
Nov 17 '18 at 15:51
i am not sure what exactly the problem :o( can you elaborate please?
– Mikhail Berlyant
Nov 17 '18 at 18:12
Is there a reason not to execute a DDL statement as a query instead? cloud.google.com/bigquery/docs/reference/standard-sql/…
– Elliott Brossard
Nov 17 '18 at 14:18
Is there a reason not to execute a DDL statement as a query instead? cloud.google.com/bigquery/docs/reference/standard-sql/…
– Elliott Brossard
Nov 17 '18 at 14:18
What do you mean by the header fields are in a list? Do you have an example?
– Willian Fuks
Nov 17 '18 at 14:22
What do you mean by the header fields are in a list? Do you have an example?
– Willian Fuks
Nov 17 '18 at 14:22
@WillianFuks Updated the question with the example list.
– Praxiteles
Nov 17 '18 at 14:30
@WillianFuks Updated the question with the example list.
– Praxiteles
Nov 17 '18 at 14:30
Can you show what you tried so far?
– Martin Weitzmann
Nov 17 '18 at 15:51
Can you show what you tried so far?
– Martin Weitzmann
Nov 17 '18 at 15:51
i am not sure what exactly the problem :o( can you elaborate please?
– Mikhail Berlyant
Nov 17 '18 at 18:12
i am not sure what exactly the problem :o( can you elaborate please?
– Mikhail Berlyant
Nov 17 '18 at 18:12
add a comment |
1 Answer
1
active
oldest
votes
I think you just need this:
from google.cloud import bigquery
headers = [
('Name', 'STRING'),
('Age', 'INTEGER'),
('Weight', 'STRING'),
('any_thing_else', 'STRING'), ...
]
schema = map(lambda n: bigquery.SchemaField(*n), headers)
table.schema = tuple(schema)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351972%2fhow-do-you-programmatically-create-a-google-bigquery-schema-in-a-tuple%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you just need this:
from google.cloud import bigquery
headers = [
('Name', 'STRING'),
('Age', 'INTEGER'),
('Weight', 'STRING'),
('any_thing_else', 'STRING'), ...
]
schema = map(lambda n: bigquery.SchemaField(*n), headers)
table.schema = tuple(schema)
add a comment |
I think you just need this:
from google.cloud import bigquery
headers = [
('Name', 'STRING'),
('Age', 'INTEGER'),
('Weight', 'STRING'),
('any_thing_else', 'STRING'), ...
]
schema = map(lambda n: bigquery.SchemaField(*n), headers)
table.schema = tuple(schema)
add a comment |
I think you just need this:
from google.cloud import bigquery
headers = [
('Name', 'STRING'),
('Age', 'INTEGER'),
('Weight', 'STRING'),
('any_thing_else', 'STRING'), ...
]
schema = map(lambda n: bigquery.SchemaField(*n), headers)
table.schema = tuple(schema)
I think you just need this:
from google.cloud import bigquery
headers = [
('Name', 'STRING'),
('Age', 'INTEGER'),
('Weight', 'STRING'),
('any_thing_else', 'STRING'), ...
]
schema = map(lambda n: bigquery.SchemaField(*n), headers)
table.schema = tuple(schema)
answered Nov 19 '18 at 18:40
khankhan
1,96683052
1,96683052
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351972%2fhow-do-you-programmatically-create-a-google-bigquery-schema-in-a-tuple%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there a reason not to execute a DDL statement as a query instead? cloud.google.com/bigquery/docs/reference/standard-sql/…
– Elliott Brossard
Nov 17 '18 at 14:18
What do you mean by the header fields are in a list? Do you have an example?
– Willian Fuks
Nov 17 '18 at 14:22
@WillianFuks Updated the question with the example list.
– Praxiteles
Nov 17 '18 at 14:30
Can you show what you tried so far?
– Martin Weitzmann
Nov 17 '18 at 15:51
i am not sure what exactly the problem :o( can you elaborate please?
– Mikhail Berlyant
Nov 17 '18 at 18:12