IBM Cloud Docs
导出和导入查询历史记录

导出和导入查询历史记录

Presto协调器将查询历史记录存储在 system.runtime.queries 表中。 但 system.runtime.queries 表会在重新启动Presto 时被截断,导致查询历史丢失。 为缓解这一问题,可以将查询历史记录导出为 csv 文件,还可以将查询历史记录从system.runtime.queries表导入非系统表。

建议定期导出查询历史记录以避免丢失查询历史记录。

要导入和导出查询历史记录,必须安装PrestoCLI。 更多信息,请参阅 连接Presto服务器

导出查询历史记录

要导出查询历史记录,请运行以下命令。

export PRESTO_PASSWORD=<your api_key>
./presto --server https://<port:host> --catalog system \
--schemaruntime --execute "select * from queries" \
--user ibmlhapikey --output-format CSV_HEADER > history.csv --password

此命令生成 CSV 文件,其中包含导出的查询历史记录。

  • 示例
./presto --server https://8dac613f-ba5b-4c3c-8c96-
ce8de101f7cf.cdc406pd09pasng7elgg.databases.appdomain.cloud:30929 \
--execute "select * from system.runtime.queries" --output-format CSV_HEADER \
--user ibmlhapikey output-format CSV > history.csv --password

导入查询历史记录

  1. 要导入查询历史记录,请在具有写访问权的目录中创建模式。

    create schema <non-system-catalog.schema-name> with (location=' s3a://<bucket-name>/<schema-name>')
    
    • 示例
    ./presto --server https://8dac613f-ba5b-4c3c-8c96-\
    ce8de101f7cf.cdc406pd09pasng7elgg.databases.appdomain.cloud:30929 \
    --execute "create schema hive_data.query_history with \
    (location='s3a://8dac613f-ba5b-4c3c-8c96-ce8de101f7cf-customer/query_history')" \
    --user ibmlhapikey --password
    
  2. 在同一目录中创建表。

    此表必须具有与 system.runtime.queries 表相同的元数据。 使用 CREATE TABLE AS SELECT 语句来创建此表。

    create table <non-system-table-name> as select * from system.runtime.queries where 1=0;
    

    where 1=0 条件确保不从表中选择任何行,从而导致结果集为空。

    • 示例
    ./presto --server https://8dac613f-ba5b-4c3c-8c96-ce8de101f7cf.cdc406pd09pasng7elgg.databases.appdomain.cloud:30929
    --execute "create table hive_data.query_history.queries as select * from system.runtime.queries where 1=0"
    --user ibmlhapikey --password
    
  3. 要将查询历史记录导入到刚刚创建的表中,请定期运行以下查询。

    INSERT INTO <non-system-table-name>
    SELECT *
    FROM system.runtime.queries
    WHERE query_id NOT IN (SELECT query_id FROM <non-system-table-name>);
    
    • 示例
    ./presto --server \
    https://8dac613f-ba5b-4c3c-8c96-ce8de101f7cf.cdc406pd09pasng7elgg.databases.appdomain.cloud:3092 \
    –-execute "insert into hive_data.query_history.queries select * from system. runtime.queries \
    where query_id not in (select query_id from hive_data.query_history.queries)"
    --user ibmlhapikey --password
    
  4. 要从两个表中检索查询历史记录,请使用以下语句。

    select * from <non-system-table-name> union select * from `system.runtime.queries` order by created;
    
    • 示例
    ./presto --server \
    https://23b06b14-342b-4ed2-841d-7f02ca9ae788.cdc406pd09pasng7elgg.databases.appdomain.cloud:31530 \
    --execute " select * from hive_data.query_history.queries union \
    select * from system.runtime.queries order by created " \
    --output-format=CSV_HEADER --user ibmlhapikey --password