> ## Documentation Index
> Fetch the complete documentation index at: https://docs.open-metadata.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Usage Workflow Through Query Logs

> Query usage logs to analyze asset popularity, access patterns, and downstream dependencies for optimization.

# Usage Workflow Through Query Logs

To extract usage information, OpenMetadata parses the queries that have run against the database. This query
log information is available from WITHIN the database in the following services:

* [Athena](https://docs.open-metadata.org/v2.1.x/connectors/database/athena.md)
* [BigQuery](https://docs.open-metadata.org/v2.1.x/connectors/database/bigquery.md)
* [Clickhouse](https://docs.open-metadata.org/v2.1.x/connectors/database/clickhouse.md)
* [Databricks](https://docs.open-metadata.org/v2.1.x/connectors/database/databricks.md)
* [Exasol](https://docs.open-metadata.org/v2.1.x/connectors/database/exasol.md)
* [MSSQL](https://docs.open-metadata.org/v2.1.x/connectors/database/mssql.md)
* [PostgreSQL](https://docs.open-metadata.org/v2.1.x/connectors/database/postgres.md)
* [Redshift](https://docs.open-metadata.org/v2.1.x/connectors/database/redshift.md)
* [Snowflake](https://docs.open-metadata.org/v2.1.x/connectors/database/snowflake.md)

If you are using any other database connector, direct execution of Usage Workflow is not possible.
This is mainly because these database connectors does not maintain query execution logs which is required for Usage Workflow.

If you are interested in running the usage workflow for a connector not listed above, this documentation will help
you to execute the Usage Workflow using a query log file. This can be arbitrarily executed for **any** database connector.

## Query Log File

A query log file is a standard CSV file which contains the following information.

> [!TIP]
>
> A standard CSV should be comma separated, and each row represented as a single line in the file.

* **query\_text**: This field contains the literal query that has been executed in the database. It is quite possible
  that your query has commas `,` inside. Then, wrap each query in quotes to not have any clashes
  with the comma as a separator.
* **user\_name (optional)**: Enter the database user name which has executed this query.
* **start\_time (optional)**: Enter the query execution start time in `YYYY-MM-DD HH:MM:SS.ffffff` format. The fractional seconds are required.
* **end\_time (optional)**: Enter the query execution end time in YYYY-MM-DD HH:MM:SS format.
* **aborted (optional)**: This field accepts values as true or false and indicates whether the query was aborted during execution
* **database\_name (optional)**: Enter the database name on which the query was executed.
* **schema\_name (optional)**: Enter the schema name to which the query is associated.

Checkout a sample query log file [here](https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/examples/sample_data/glue/query_log.csv).

```csv theme={null}
query_text,database_name,schema_name
"select * from sales",default,information_schema
"select * from marketing",default,information_schema
"insert into marketing select * from sales",default,information_schema
```

## Redshift Serverless: Export SELECT Queries

The Redshift connector's usage workflow on a Serverless workgroup only captures queries that insert, merge, or delete rows in a table. To include `SELECT` queries, export them to a query log file and run this workflow with that file.

1. Run the following query in your Serverless workgroup. Adjust the time window and filters for your environment:

   ```sql theme={null}
   SELECT
       q.query_text,
       RTRIM(u.usename) AS user_name,
       TO_CHAR(q.start_time, 'YYYY-MM-DD HH24:MI:SS.US') AS start_time,
       q.end_time,
       q.database_name
   FROM SYS_QUERY_HISTORY AS q
       INNER JOIN pg_catalog.pg_user AS u
           ON q.user_id = u.usesysid
   WHERE q.query_type = 'SELECT'
       AND LOWER(q.status) = 'success'
       AND q.user_id > 1
       AND q.start_time >= DATEADD(day, -7, GETDATE())
   ORDER BY q.start_time DESC;
   ```

2. Save the result as a CSV file with a header row. The `query_text` column is required. The `user_name`, `start_time`, `end_time`, `database_name`, and `schema_name` columns are optional. Write `start_time` as `YYYY-MM-DD HH:MM:SS.ffffff`, which the export query does. If the format differs, the workflow stops reading the file and logs a warning. Quote values that contain commas or line breaks.

3. Set `serviceName` to your Redshift service and `queryLogFilePath` to this file in the YAML below, then run the workflow.

For more information about the view, see the [SYS\_QUERY\_HISTORY](https://docs.aws.amazon.com/redshift/latest/dg/SYS_QUERY_HISTORY.html) documentation from AWS.

## Usage Workflow

In order to run a Usage Workflow we need to make sure that Metadata Ingestion Workflow for corresponding service has already been executed. We will follow the steps to create a JSON configuration able to collect the query log file and execute the usage workflow.

### 1. Create a configuration file using template YAML

Create a new file called `query_log_usage.yaml` in the current directory. Note that the current directory should be the openmetadata directory.
Copy and paste the configuration template below into the `query_log_usage.yaml` the file you created.

```yaml theme={null}
source:
  type: query-log-usage
  serviceName: <name>
  sourceConfig:
    config:
      type: DatabaseUsage
      queryLogFilePath: <path to query log file>
processor:
  type: query-parser
  config: {}
stage:
  type: table-usage
  config:
    filename: "/tmp/query-usage"
bulkSink:
  type: metadata-usage
  config:
    filename: "/tmp/query-usage"
workflowConfig:
  openMetadataServerConfig:
    hostPort: <OpenMetadata host and port>
    authProvider: <OpenMetadata auth provider>
```

The `serviceName` should be a service already ingested in OpenMetadata.

* **queryLogFilePath**: Enter the file path of query log csv file.

### 2. Run with the CLI

First, we will need to save the YAML file. Afterward, and with all requirements installed, we can run:

```yaml theme={null}
metadata usage -c <path-to-yaml>
```

Note that from connector-to-connector, this recipe will always be the same. By updating the YAML configuration, you will be able to extract metadata from different sources.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.