Skip to content

Instantly share code, notes, and snippets.

@mylamour
Last active May 9, 2022 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mylamour/ce3d66e99dbeddda4c0ba4e6db87ed87 to your computer and use it in GitHub Desktop.
Save mylamour/ce3d66e99dbeddda4c0ba4e6db87ed87 to your computer and use it in GitHub Desktop.
you should have a permition write to destination.
import psycopg2
import psycopg2.extras
import sys
import json
import uuid
def main():
conn_string = "host='ec2-54-xxx-yyy-zzz.cn-north-1.compute.amazonaws.com.cn' " \
"dbname='musicbrainz' " \
"user='postgres' " \
"password=''"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor('cursor_unique_name', cursor_factory=psycopg2.extras.DictCursor)
SQL= """
select recording.gid as in , artist.gid as musicGroupId
from musicbrainz.artist,musicbrainz.recording
where recording.artist_credit = artist.id
;
"""
cursor.execute(SQL)
jsonfile = open('./musicGroupRelation.json', 'w')
row_count = 0
for row in cursor:
row_count += 1
# print row
opt = {}
opt[u"endDate"] = " "
opt[u"description"] = " "
opt[u"beginDate"] = " "
opt[u"label"] = "musicGroup"
opt[u"in"] = row[0]
opt[u"@id"] = str(uuid.uuid1())
opt[u"@type"] = "Relation"
opt[u"out"] = row[1]
# print opt
json.dump(opt,jsonfile)
jsonfile.write('\n')
if __name__ == "__main__":
main()
-- Backup to JSON
COPY (
SELECT ROW_TO_JSON(a)
FROM (
select
artist.id as artist_id,
artist_credit.id as artist_credit_id ,
recording.id as recording_id ,
artist.gid as artist_gid,
recording.gid as recording_gid,
artist.name as artist_name,
artist_credit_name.artist as artist_credit_name_artist ,
artist_credit_name.name as artist_credit_name_name
from musicbrainz.artist,musicbrainz.artist_credit_name,musicbrainz.artist_credit,musicbrainz.recording
where artist.gid='a223958d-5c56-4b2c-a30a-87e357bc121b' limit 100
)a
)
TO '/your/dist/path' with text;
-- Backup To CSV
Copy (Select * From musicbrainz.url) To '/tmp/url.csv' With CSV DELIMITER ',';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment