GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — How to store BigQuery schemas for later use. · Issue #3419 · googleapis/google-cloud-python

How to store BigQuery schemas for later use. #3419

New issue
Copy link
New issue
Copy link
Closed
Closed
How to store BigQuery schemas for later use.#3419
Copy link
Assignees
tswast
Labels
api: bigqueryIssues related to the BigQuery API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@tweeter0830
tweeter0830
opened on May 15, 2017
Issue body actions

Is there an easy way to get the schema from a BigQuery table and save it to disk for later use?

For example, say that I wanted to get the schema of a table that currently exists, save it to disk and later create a table with the same schema.

I see that there is such a thing as a SchemaField in the api, but there doesn't seem to be a clean way to serialize it.

Activity

Next
  1. tseaver
    added
    api: bigqueryIssues related to the BigQuery API.
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    on May 15, 2017
  2. tseaver commented on May 15, 2017

    @tseaver
    tseaver
    on May 15, 2017
    Contributor
    More actions

    @tweeter0830 There is no built-in support for serializing SchemaField instances. The non-public table._build_schema_resource and table._parse_schema_resource functions could be used as the basis for serializing them to JSON.

  3. tweeter0830 commented on May 15, 2017

    @tweeter0830
    tweeter0830
    on May 15, 2017
    Author
    More actions

    Thanks.

  4. tweeter0830
    closed this as completedon May 15, 2017
  5. tseaver
    added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on May 15, 2017
  6. tseaver
    reopened this on May 15, 2017
  7. tseaver commented on May 15, 2017

    @tseaver
    tseaver
    on May 15, 2017
    Contributor
    More actions

    I'll leave the issue open, as making those two functions public / better documented seems like a reasonable enhancement.

  8. tseaver commented on May 15, 2017

    @tseaver
    tseaver
    on May 15, 2017
    Contributor
    More actions

    E.g., I could see adding a schema.SchemaField.from_api_repr classmethod and a corresponding schema.SchemaField.to_api_repr instance method to support your usecase.

    Reacted by Tim Sweña (Swast), Andrey Fedorov and Anthony Torres
  9. lukesneeringer
    removed
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Aug 9, 2017
  10. lukesneeringer
    closed this as completedin #3786on Aug 10, 2017
  11. tseaver commented on Aug 10, 2017

    @tseaver
    tseaver
    on Aug 10, 2017
    Contributor
    More actions

    #3786 doesn't fix this issue.

  12. tseaver
    reopened this on Aug 10, 2017
  13. lukesneeringer commented on Aug 10, 2017

    @lukesneeringer
    lukesneeringer
    on Aug 10, 2017
    Contributor
    More actions

    Why? As soon as it is a dictionary, saving it to disk is trivial (json.dump). Nevermind, I saw your comment on the other issue.

  14. tseaver commented on Aug 10, 2017

    @tseaver
    tseaver
    on Aug 10, 2017
    Contributor
    More actions

    The goal of this issue is to store the entire schema for a table (not a single field) and in a format which is "good" for people (i.e., with the appropriate top-level mapping / key holding that list).

  15. 1 remaining item

  16. chemelnucfin
    assigned
    tseaver
    and
    chemelnucfin
    on Jan 15, 2018
  17. chemelnucfin commented on Jan 22, 2018

    @chemelnucfin
    chemelnucfin
    on Jan 22, 2018
    Contributor
    More actions

    Hello, feature requests will now be tracked in the project Feature Requests. I will close this issue now, please feel free to continue to address any issues/concerns here.

  18. chemelnucfin
    closed this as completedon Jan 22, 2018
  19. theacodes
    unassigned
    chemelnucfin
    on Sep 28, 2018
  20. tswast
    reopened this on Apr 10, 2019
  21. tswast commented on Apr 10, 2019

    @tswast
    tswast
    on Apr 10, 2019
    Contributor
    More actions

    Background

    With the BigQuery command-line tool, it's possible to specify a schema via a JSON file. See: https://cloud.google.com/bigquery/docs/schemas#using_a_json_schema_file

    Schema format:

    [
      {
        "description": "quarter",
        "mode": "REQUIRED",
        "name": "qtr",
        "type": "STRING"
      },
      {
        "description": "sales representative",
        "mode": "NULLABLE",
        "name": "rep",
        "type": "STRING"
      },
      {
        "description": "total sales",
        "mode": "NULLABLE",
        "name": "sales",
        "type": "FLOAT"
      }
    ]
    

    Phase 1

    This feature request is to add a helper function that can load such files and return a list of SchemaField objects.

    # file_or_path = "path/to/schema.json"
    # OR
    # file_or_path = open("path/to/schema.json")
    
    schema_fields = client.schema_from_json(file_or_path)
    
    # Now the value of schema_fields is
    # schema_fields = [
    #      schema.SchemaField(...),
    #      schema.SchemaField(...),
    #      schema.SchemaField(...),
    # ]
    

    Phase 1.b

    Add client.schema_to_json(schema_list, destination) which serializes a list of SchemaFields to JSON and writes it to destination, which is a file-like object or path.

    Phase 2

    Blocked by #7693

    client.schema_from_json should accept GCS paths like file_or_path = "gs://bucket_name/path/to/schema.json". Likewise, client.schema_to_json should accept GCS paths for destination.

    This (optional) feature only works if a storage_client argument is passed in as an additional argument.

    schema_fields = client.schema_from_json(file_or_path, storage_client=storage_client)
    

    Phase 3

    If a gs:// URL is provided but not a storage_client, try to construct one using the BigQuery client's credentials. Fails if the google-cloud-storage library is not installed.

  22. tswast commented on Apr 10, 2019

    @tswast
    tswast
    on Apr 10, 2019
    Contributor
    More actions

    cc @lbristol88

  23. yan-hic commented on Apr 11, 2019

    @yan-hic
    yan-hic
    on Apr 11, 2019
    More actions

    @tswast could you make schema_from_json accept a string too ?

    We have the schema in gcs but need to touch it a bit before using in bigquery.
    This would avoid saving it locally just to use that new function.

    FYI, we do this today: job_config._properties['load']['schema'] = schema_json

  24. tswast commented on Apr 11, 2019

    @tswast
    tswast
    on Apr 11, 2019
    Contributor
    More actions

    In the case of in-memory JSON, I'd prefer you pass in a file-like object, i.e.

    schema_from_json(io.BytesIO(b'[{"name": "col1", "type": "INTEGER"}]'))
    

    Otherwise it gets a little tricky to disambiguate a path from file contents.

  25. yan-hic commented on Apr 11, 2019

    @yan-hic
    yan-hic
    on Apr 11, 2019
    More actions

    Makes sense.

  26. tswast
    assigned
    tswast
    and unassigned
    tseaver
    on Apr 12, 2019
  27. tswast commented on Nov 12, 2019

    @tswast
    tswast
    on Nov 12, 2019
    Contributor
    More actions

    Fixed by #9550 (allow passing JSON version of schema directly to schema setters) and #7761 (adds schema_from_json and schema_to_json helpers).

    Closing despite missing GCS integration, as that wasn't in the original request.

  28. tswast
    closed this as completedon Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @tswast
    tswast

Labels

api: bigqueryIssues related to the BigQuery API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.