Skip to main content

Running SQL queries on an asset

To run SQL queries on an asset:

Running SQL query on an asset
QueryRequest query = QueryRequest.creator( // (1)
"SELECT * FROM \"PACKAGETYPES\" LIMIT 50;",
"default/snowflake/1705755637"
)
.defaultSchema("RAW.WIDEWORLDIMPORTERS_WAREHOUSE") // (2)
.build();
QueryResponse response = client.queries.stream(query); // (3)
  1. To create a minimal query object, use the QueryRequest creator method and provide the following arguments:
    • SQL query to run.
    • unique name of the connection to use for the query.
  2. You must provide default schema name to use for unqualified objects in the SQL, in the form DB.SCHEMA.
  3. You can now execute the query using the stream() method.

Use API token to run queries

You can also grant permission to run SQL queries on an asset using an API token, if you want. (This must be explicitly granted, as it'sn't possible by default.) You can even mask certain information through data policies on purposes linked to the API token.

API token permissions

Before executing queries on an asset using an API token, make sure that the token is linked to a persona with a data policy that permits queries for that specific asset.

Running SQL query on an asset with API token
AuthPolicy data = Purpose.createDataPolicy( // (1)
"Mask the data", // (2)
purpose.getGuid(), // (3)
AuthPolicyType.DATA_MASK, // (4)
null,
List.of(token.getApiTokenUsername()), // (5)
false
)
.policyMaskType(DataMaskingType.REDACT) // (6)
.build();
AssetMutationResponse response = client.assets.save(List.of(data), false); // (7)

try (AtlanClient tokenClient = new AtlanClient(client.getBaseUrl(), token.getAttributes().getAccessToken()))
  1. Use the createDataPolicy() method to start building a data policy with the minimal required information.
  2. You must give the policy a name.
  3. You must provide the GUID of the Purpose to attach this policy to.
  4. Specify the type of policy (granting, denying or masking the data of assets with the tags in the purpose).
  5. Set the policy user to the API token.
  6. Set the type of masking to REDACT to redact the tagged elements in the query response.
  7. To then add the policy to the purpose in Atlan, call the save() method with the policy object you've built.
  8. Create a new AtlanClient set up to use the new API token.
  9. To create a minimal query object, use the QueryRequest creator method and provide the following arguments:
    • SQL query to run.
    • unique name of the connection to use for the query.
  10. You must provide default schema name to use for unqualified objects in the SQL, in the form DB.SCHEMA.
  11. You can now execute the query using the stream() method.
Was this page helpful?