🤠

PythonにてFirestoreにデータを投入する

2024/10/21に公開

はじめに

  • FlaskにてFirestoreをDBとしたアプリケーションを作成する必要があり、その際の初期データ投入を行なった際のログをまとめます。(本記事はFlaskは関係ありません)

※firestore自体はすでに構築済みの状況です。

環境構築

仮想環境(venvの作成)

$ cd firestore_schema_setup/
$ python3 -m venv venv(仮想環境名)
$ source venv(仮想環境名)/bin/activate

Firebase Admin SDK用意

firestore_schema_setup/requirements.txt

google-cloud-firestore

install実行

pip install -r requirements.txt

Firebase側の設定

サービスアカウントの鍵をダウンロードし、firestore_key.jsonとしてfirestore_schema_setup直下に配置

Pythonコード作成~実行

  • 今回はfootball data.orgより入手したリヴァプールの選手データを格納します。

firestore_schema_setup/insert.py

import google.cloud.firestore
import json

db = google.cloud.firestore.Client(database={作成したFireStoreの名前})

def insert_players():
    json_open = open('./origin_data/files/players.json', 'r')
    json_load = json.load(json_open)
    players = json_load['squad']

    for player in players:
        player_ref = db.collection('players').document(str(player['id']))
        player_ref.set(player)

def get_all_players():
    results = db.collection('players').stream()
    data_list = [doc.to_dict() for doc in results]

    print(data_list)

if __name__ == '__main__':
    # 選手情報を格納
    insert_players()
    # 格納した選手情報を出力
    get_all_players()
firestore_schema_setup/players.json
{
    "area": {
        "id": 2072,
        "name": "England",
        "code": "ENG",
        "flag": "https://crests.football-data.org/770.svg"
    },
    "id": 64,
    "name": "Liverpool FC",
    "shortName": "Liverpool",
    "tla": "LIV",
    "crest": "https://crests.football-data.org/64.png",
    "address": "Anfield Road Liverpool L4 0TH",
    "website": "http://www.liverpoolfc.tv",
    "founded": 1892,
    "clubColors": "Red / White",
    "venue": "Anfield",
    "runningCompetitions": [
        {
            "id": 2021,
            "name": "Premier League",
            "code": "PL",
            "type": "LEAGUE",
            "emblem": "https://crests.football-data.org/PL.png"
        },
        {
            "id": 2001,
            "name": "UEFA Champions League",
            "code": "CL",
            "type": "CUP",
            "emblem": "https://crests.football-data.org/CL.png"
        }
    ],
    "coach": {
        "id": 121014,
        "firstName": "Arne",
        "lastName": "Slot",
        "name": "Arne Slot",
        "dateOfBirth": "1978-09-17",
        "nationality": "Netherlands",
        "contract": {
            "start": "2024-06",
            "until": "2027-06"
        }
    },
    "squad": [
        {
            "id": 1795,
            "name": "Alisson",
            "position": "Goalkeeper",
            "dateOfBirth": "1992-10-02",
            "nationality": "Brazil"
        },
        {
            "id": 102046,
            "name": "Caoimhin Kelleher",
            "position": "Goalkeeper",
            "dateOfBirth": "1998-11-23",
            "nationality": "Ireland"
        },
        {
            "id": 152579,
            "name": "Vítězslav Jaroš",
            "position": "Goalkeeper",
            "dateOfBirth": "2001-07-23",
            "nationality": "Czech Republic"
        },
        {
            "id": 175845,
            "name": "Tyler Morton",
            "position": "Midfield",
            "dateOfBirth": "2002-10-31",
            "nationality": "England"
        },
        {
            "id": 230517,
            "name": "Trey Nyoni",
            "position": "Attacking Midfield",
            "dateOfBirth": "2007-06-30",
            "nationality": "England"
        },
        {
            "id": 176718,
            "name": "Jarell Quansah",
            "position": "Centre-Back",
            "dateOfBirth": "2003-01-29",
            "nationality": "England"
        },
        {
            "id": 175865,
            "name": "Conor Bradley",
            "position": "Right-Back",
            "dateOfBirth": "2003-07-09",
            "nationality": "Northern Ireland"
        },
        {
            "id": 124824,
            "name": "Harvey Elliott",
            "position": "Central Midfield",
            "dateOfBirth": "2003-04-04",
            "nationality": "England"
        },
        {
            "id": 81793,
            "name": "Ryan Gravenberch",
            "position": "Central Midfield",
            "dateOfBirth": "2002-05-16",
            "nationality": "Netherlands"
        },
        {
            "id": 45681,
            "name": "Alexis Mac Allister",
            "position": "Central Midfield",
            "dateOfBirth": "1998-12-24",
            "nationality": "Argentina"
        },
        {
            "id": 28612,
            "name": "Darwin Núñez",
            "position": "Centre-Forward",
            "dateOfBirth": "1999-06-24",
            "nationality": "Uruguay"
        },
        {
            "id": 22396,
            "name": "Luis Díaz",
            "position": "Left Winger",
            "dateOfBirth": "1997-01-13",
            "nationality": "Colombia"
        },
        {
            "id": 16347,
            "name": "Dominik Szoboszlai",
            "position": "Central Midfield",
            "dateOfBirth": "2000-10-25",
            "nationality": "Hungary"
        },
        {
            "id": 9542,
            "name": "Ibrahima Konaté",
            "position": "Centre-Back",
            "dateOfBirth": "1999-05-25",
            "nationality": "France"
        },
        {
            "id": 7873,
            "name": "Curtis Jones",
            "position": "Central Midfield",
            "dateOfBirth": "2001-01-30",
            "nationality": "England"
        },
        {
            "id": 7869,
            "name": "Virgil van Dijk",
            "position": "Centre-Back",
            "dateOfBirth": "1991-07-08",
            "nationality": "Netherlands"
        },
        {
            "id": 7868,
            "name": "Andrew Robertson",
            "position": "Left-Back",
            "dateOfBirth": "1994-03-11",
            "nationality": "Scotland"
        },
        {
            "id": 7867,
            "name": "Trent Alexander-Arnold",
            "position": "Right-Back",
            "dateOfBirth": "1998-10-07",
            "nationality": "England"
        },
        {
            "id": 7862,
            "name": "Joe Gomez",
            "position": "Centre-Back",
            "dateOfBirth": "1997-05-23",
            "nationality": "England"
        },
        {
            "id": 7459,
            "name": "Cody Gakpo",
            "position": "Left Winger",
            "dateOfBirth": "1999-05-07",
            "nationality": "Netherlands"
        },
        {
            "id": 7383,
            "name": "Kostas Tsimikas",
            "position": "Left-Back",
            "dateOfBirth": "1996-05-12",
            "nationality": "Greece"
        },
        {
            "id": 4092,
            "name": "Diogo Jota",
            "position": "Centre-Forward",
            "dateOfBirth": "1996-12-04",
            "nationality": "Portugal"
        },
        {
            "id": 3754,
            "name": "Mohamed Salah",
            "position": "Right Winger",
            "dateOfBirth": "1992-06-15",
            "nationality": "Egypt"
        },
        {
            "id": 3269,
            "name": "Wataru Endō",
            "position": "Defensive Midfield",
            "dateOfBirth": "1993-02-09",
            "nationality": "Japan"
        },
        {
            "id": 1780,
            "name": "Federico Chiesa",
            "position": "Left Winger",
            "dateOfBirth": "1997-10-25",
            "nationality": "Italy"
        }
    ],
    "staff": [],
    "lastUpdated": "2022-02-10T19:30:22Z"
}

Firestoreを確認

マネジメントコンソールから確認

データが格納されていることを確認できました。

備考

最初にFirebase Admin SDK(firebase_admin)を利用して実装しようとしていたのですがFirestoreの名前が(default)でないとFirestoreを操作できず、、、

https://stackoverflow.com/questions/70468635/why-does-the-firebase-api-call-for-projects-databases-get-return-an-error-404
(上記投稿も解決できていない模様)

どなたか解決方法を知っている方いらっしゃればご教授ください、、

Discussion