How do you programmatically create a Google BigQuery schema in a tuple?












0















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?










share|improve this question

























  • 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
















0















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?










share|improve this question

























  • 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














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















0














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)





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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)





    share|improve this answer




























      0














      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)





      share|improve this answer


























        0












        0








        0







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 18:40









        khankhan

        1,96683052




        1,96683052






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()