Created
January 31, 2020 17:43
-
-
Save anuriq/db879b3670c8bd020c967a1c509107b4 to your computer and use it in GitHub Desktop.
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
#!/var/lib/trove/venv/1.6.5/bin/python | |
def LOG(msg): | |
print msg | |
class Connection(object): | |
"""ClickHouse client wrapper""" | |
def __init__(self, username): | |
self.username = username | |
self._client = None | |
super(Connection, self).__init__() | |
def __enter__(self): | |
"""Import in runtime for exclude clickhouse_driver from guest | |
agent requirements | |
""" | |
from clickhouse_driver import Client | |
self._client = Client('localhost', user=self.username) | |
return self | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
self._client.disconnect() | |
def execute(self, *args, **kwargs): | |
return self._client.execute(*args, **kwargs) | |
def list_nonreplica_tables(admin): | |
q = ("SELECT database, name " | |
"FROM system.tables WHERE database != 'system' AND (database, name) " | |
"NOT IN (SELECT database, table FROM system.replicas)") | |
with Connection(admin) as client: | |
return client.execute(q) | |
def freeze_table(admin, table): | |
q = ("SELECT DISTINCT partition FROM system.parts WHERE " | |
"database=%(db)s AND table=%(tb)s") | |
with Connection(admin) as client: | |
partitions = client.execute(q, {'db': table[0], 'tb': table[1]}) | |
for p in partitions: | |
try: | |
query = "ALTER TABLE {db}.{tb} FREEZE PARTITION {p}".format( | |
db=table[0], tb=table[1], p=p[0]) | |
client.execute(query) | |
except Exception as e: | |
LOG("Cannot freeze partition! Error on query: %s" % query) | |
raise e | |
def main(): | |
tables = list_nonreplica_tables('os_admin') | |
for table in tables: | |
freeze_table('os_admin', table) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment