From 0d2a1cf9c8078c27f02721196918d91bc4b32e3d Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 22 Oct 2023 22:18:31 +0800 Subject: add MongoDB to ClickHouse data migration script --- migration/mongodb_to_clickhouse.py | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 migration/mongodb_to_clickhouse.py diff --git a/migration/mongodb_to_clickhouse.py b/migration/mongodb_to_clickhouse.py new file mode 100644 index 0000000..caf8424 --- /dev/null +++ b/migration/mongodb_to_clickhouse.py @@ -0,0 +1,54 @@ +#!/usr/bin/python3 +''' +This script exports data from Blame MongoDB to TSV format. +You can redirect the output to clickhouse-client to migrate to new Blame which uses ClickHouse. +''' + +import uuid + +import pymongo + +from sys import stderr + +MG_URL = 'mongodb://@localhost:27017/' # Change your credential here + +stdout = open(1, "w", buffering=1024 * 1024 * 1024) # 1GB buffer, you can reduce this + + +def read_mongo(): + conn = pymongo.MongoClient(MG_URL) + table = conn['blame']['survival'] + n = 0 + for i, doc in enumerate(table.find()): + print( + doc['action_type'], + doc['game_version'], + doc['object_id'], + doc['object_pos']['world'], + int(doc['object_pos']['x']), + int(doc['object_pos']['y']), + int(doc['object_pos']['z']), + doc['object_type'], + doc['subject_id'], + doc['subject_pos']['world'], + doc['subject_pos']['x'], + doc['subject_pos']['y'], + doc['subject_pos']['z'], + uuid.UUID(bytes=doc['subject_uuid']), + doc['timestamp_millis'], + sep='\t', + file=stdout, + ) + n += 1 + if n == 10000: + print('Rows written:', i+1, file=stderr) + n = 0 + print('OK', file=stderr) + + +def main(): + read_mongo() + + +if __name__ == '__main__': + main() -- cgit v1.2.3