Last active
April 15, 2022 17:54
-
-
Save cookie-s/5150cc6784d5eb6f6a7b24af1aab5602 to your computer and use it in GitHub Desktop.
BigQueryのschemaが少しわかりやすくなるくん
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby bq2json.rb <(bq show --format json 'project:dataset.table') | gron | |
require 'json' | |
schema = JSON.parse(IO.binread(ARGV.shift))['schema'] | |
def dfs obj | |
res = {} | |
obj['fields'].map do |obj| | |
case obj['type'] | |
when 'STRING' | |
res[obj['name']] = 'S' | |
when 'FLOAT' | |
res[obj['name']] = 'F' | |
when 'INTEGER' | |
res[obj['name']] = 'I' | |
when 'BOOLEAN' | |
res[obj['name']] = 'B' | |
when 'TIMESTAMP' | |
res[obj['name']] = 'T' | |
when 'RECORD' | |
res[obj['name']] = dfs obj | |
else | |
raise obj['type'] | |
end | |
end | |
res | |
end | |
puts JSON.dump(dfs(schema)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment