0
0

More than 1 year has passed since last update.

Python3: Cloud Firestore のデータを ndjson に変換

Last updated at Posted at 2022-04-02

こちらを参考にしました。
コレクションのすべてのドキュメントを取得する

get_firestore_docs.py
#! /usr/bin/python
#
#	get_firestore_docs.py
#
#						Apr/02/2022
# ------------------------------------------------------------------
import sys
import json
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from datetime import date, datetime
# ------------------------------------------------------------------
def json_serial(obj):
	# 日付型の場合には、文字列に変換します
	if isinstance(obj, (datetime, date)):
		return obj.isoformat()
	# 上記以外はサポート対象外.
	raise TypeError ("Type %s not serializable" % type(obj))
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")

project_id='project-apr02-2022'
collection_in='sample_collection'

cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
	'projectId': project_id
})

db = firestore.client()

docs = db.collection(collection_in).stream()

for doc in docs:
	json_str=json.dumps(doc.to_dict(),default=json_serial)
	print(json_str)

sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行スクリプト

export GOOGLE_APPLICATION_CREDENTIALS="key-file-apr02.json"
./get_firestore_docs.py
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0