Link knowledge files to assets in Atlan using KnowledgeFile and the Python SDK (pyatlan). Append, replace, or remove linked files programmatically, and read the files linked to an asset.
KnowledgeFile: Link knowledge files to assets
Use KnowledgeFile in the Atlan Python SDK to programmatically link knowledge files to assets. A linked file appears on the asset's profile, and the Description and README agents read it the next time they enrich that asset. See Link knowledge files to assets for what linking changes in the product.
A knowledge file can be linked to any asset type. The link lives on the asset side, in its knowledgeLinkedFiles relationship.
The knowledge_linked_files attribute is available from pyatlan 11.2.0. The append_knowledge_files, replace_knowledge_files, and remove_knowledge_files methods are available in releases after 11.2.0.
Prerequisites
Before you begin, make sure you have:
- The Python SDK set up with an API token that can update the target assets
- pyatlan 11.2.0 or later (see the note earlier in this page for the linking methods)
- The
qualified_nameor GUID of the asset to link, and the GUIDs of the knowledge files. Knowledge files are uploaded through knowledge folders; find their GUIDs with a search on theKnowledgeFiletype
Append knowledge files to asset
To link more knowledge files to an asset, without changing any of the files already linked to it:
- Python
- Raw REST API
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.assets import KnowledgeFile, Table
client = AtlanClient()
table = client.asset.append_knowledge_files( # (1)
asset_type=Table, # (2)
qualified_name="default/snowflake/1657037873/SAMPLE_DATA/FOOD_BEVERAGE/ORDER_ANALYSIS", # (3)
files=[KnowledgeFile.ref_by_guid(guid="b4113341-251b-4adc-81fb-2420501c30e6"), # (4)
KnowledgeFile.ref_by_guid(guid="b267858d-8316-4c41-a56a-6e9b840cef4a")]
) # (5)
- Use the
asset.append_knowledge_files()method, which constructs the necessary request and calls the necessary APIs to link the files all-in-one. - The
asset_typeof the asset on which to link the files. - The
qualified_nameof the asset on which to link the files.- Note: Alternatively the parameter name
guidcan be specified along with theguidof the asset on which to link the files.
- Note: Alternatively the parameter name
- A list of knowledge file references. Each reference can be to a file by its GUID
or its
qualified_name. At the completion of this code, the files in this list are added to any other files already linked to the asset. Linking a file that's already linked is a no-op. - The
assetreturned by this call is a minimal asset and won't contain any linked files. If you need anassetwhich contains them, retrieve it via theasset.get_by_guidorasset.get_by_qualified_namemethods, or see Read linked knowledge files.
{
"entities": [ // (1)
{
"typeName": "Table", // (2)
"attributes": {
"name": "ORDER_ANALYSIS", // (3)
"qualifiedName": "default/snowflake/1657037873/SAMPLE_DATA/FOOD_BEVERAGE/ORDER_ANALYSIS" // (4)
},
"appendRelationshipAttributes": { // (5)
"knowledgeLinkedFiles": [
{
"typeName": "KnowledgeFile",
"guid": "b4113341-251b-4adc-81fb-2420501c30e6"
},
{
"typeName": "KnowledgeFile",
"guid": "b267858d-8316-4c41-a56a-6e9b840cef4a"
}
]
}
}
]
}
- All assets must be wrapped in an
entitiesarray. - You must provide the exact type name for the asset (case-sensitive).
- You must provide the exact name of the asset (case-sensitive).
- You must provide the exact
qualifiedNameof the asset (case-sensitive). - Provide the files to link under
appendRelationshipAttributes.knowledgeLinkedFiles. Each reference must include thetypeName(alwaysKnowledgeFile) andguidof the file. Files already linked to the asset stay linked, so you don't need to retrieve the asset first.
Replace knowledge files on asset
To replace all the knowledge files linked to an asset, meaning any not specified in the request are unlinked from the asset:
- Python
- Raw REST API
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.assets import KnowledgeFile, Table
client = AtlanClient()
table = client.asset.replace_knowledge_files( # (1)
asset_type=Table, # (2)
qualified_name="default/snowflake/1657037873/SAMPLE_DATA/FOOD_BEVERAGE/ORDER_ANALYSIS", # (3)
files=[KnowledgeFile.ref_by_guid(guid="b4113341-251b-4adc-81fb-2420501c30e6")] # (4)
) # (5)
- Use the
asset.replace_knowledge_files()method, which constructs the necessary request and calls the necessary APIs to replace the linked files all-in-one. - The
asset_typeof the asset on which to replace the files. - The
qualified_nameof the asset on which to replace the files.- Note: Alternatively the parameter name
guidcan be specified along with theguidof the asset.
- Note: Alternatively the parameter name
- A list of knowledge file references. After the completion of this code, only the files in this list are linked to the asset. Pass an empty list to unlink every file from the asset.
- The
assetreturned by this call is a minimal asset and won't contain any linked files.
{
"entities": [ // (1)
{
"typeName": "Table", // (2)
"attributes": {
"name": "ORDER_ANALYSIS", // (3)
"qualifiedName": "default/snowflake/1657037873/SAMPLE_DATA/FOOD_BEVERAGE/ORDER_ANALYSIS" // (4)
},
"relationshipAttributes": { // (5)
"knowledgeLinkedFiles": [
{
"typeName": "KnowledgeFile",
"guid": "b4113341-251b-4adc-81fb-2420501c30e6"
}
]
}
}
]
}
- All assets must be wrapped in an
entitiesarray. - You must provide the exact type name for the asset (case-sensitive).
- You must provide the exact name of the asset (case-sensitive).
- You must provide the exact
qualifiedNameof the asset (case-sensitive). - Provide the full set of files to link under
relationshipAttributes.knowledgeLinkedFiles. After the completion of this call, only the files in this list are linked to the asset. An empty array unlinks every file.
Remove knowledge files from asset
To unlink some knowledge files from an asset, without unlinking all of the files on the asset:
- Python
- Raw REST API
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.assets import KnowledgeFile, Table
client = AtlanClient()
table = client.asset.remove_knowledge_files( # (1)
asset_type=Table, # (2)
qualified_name="default/snowflake/1657037873/SAMPLE_DATA/FOOD_BEVERAGE/ORDER_ANALYSIS", # (3)
files=[KnowledgeFile.ref_by_guid(guid="b4113341-251b-4adc-81fb-2420501c30e6")] # (4)
)
- Use the
asset.remove_knowledge_files()method, which constructs the necessary request and calls the necessary APIs to unlink the files all-in-one. - The
asset_typeof the asset from which to unlink the files. - The
qualified_nameof the asset from which to unlink the files.- Note: Alternatively the parameter name
guidcan be specified along with theguidof the asset.
- Note: Alternatively the parameter name
- A list of knowledge file references, by GUID. At the completion of this code, the files in this list are unlinked from the asset. Other linked files stay linked. Unlinking a file doesn't delete the knowledge file.
{
"entities": [ // (1)
{
"typeName": "Table", // (2)
"attributes": {
"name": "ORDER_ANALYSIS", // (3)
"qualifiedName": "default/snowflake/1657037873/SAMPLE_DATA/FOOD_BEVERAGE/ORDER_ANALYSIS" // (4)
},
"removeRelationshipAttributes": { // (5)
"knowledgeLinkedFiles": [
{
"typeName": "KnowledgeFile",
"guid": "b4113341-251b-4adc-81fb-2420501c30e6"
}
]
}
}
]
}
- All assets must be wrapped in an
entitiesarray. - You must provide the exact type name for the asset (case-sensitive).
- You must provide the exact name of the asset (case-sensitive).
- You must provide the exact
qualifiedNameof the asset (case-sensitive). - Provide the files to unlink under
removeRelationshipAttributes.knowledgeLinkedFiles. Each reference must include thetypeName(alwaysKnowledgeFile) andguidof the file.
Read linked knowledge files
Linked files are a relationship on the asset, so you read them the same way as any other relationship: include knowledge_linked_files on a search, or retrieve the asset.
- Python
- Raw REST API
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.assets import Asset, KnowledgeFile, Table
from pyatlan.model.fluent_search import FluentSearch
client = AtlanClient()
request = (
FluentSearch()
.where(FluentSearch.active_assets()) # (1)
.where(FluentSearch.asset_type(Table))
.where(Table.QUALIFIED_NAME.eq("default/snowflake/1657037873/SAMPLE_DATA/FOOD_BEVERAGE/ORDER_ANALYSIS"))
.include_on_results(Asset.KNOWLEDGE_LINKED_FILES) # (2)
.include_on_relations(KnowledgeFile.NAME) # (3)
).to_request()
for table in client.asset.search(request):
for file in table.knowledge_linked_files or []: # (4)
print(file.guid, file.name)
- Restrict the search to active assets.
- Include the
knowledge_linked_filesrelationship on each result. - Include the file's
nameon the relatedKnowledgeFileobjects, so you don't need a second lookup to display them. knowledge_linked_filesisNonewhen the asset has no linked files.
{
"dsl": {
"query": {
"bool": {
"filter": [
{ "term": { "__state": { "value": "ACTIVE" } } },
{ "term": { "__typeName.keyword": { "value": "Table" } } },
{ "term": { "qualifiedName": { "value": "default/snowflake/1657037873/SAMPLE_DATA/FOOD_BEVERAGE/ORDER_ANALYSIS" } } }
]
}
}
},
"attributes": ["knowledgeLinkedFiles"], // (1)
"relationAttributes": ["name"] // (2)
}
- Request the
knowledgeLinkedFilesrelationship on each result. - Request the
nameof each related knowledge file.
See also
- Link knowledge files to assets: the same link from the product UI, and how linked files change enrichment
- Knowledge folders and files: what knowledge files are and who can upload them
- Link terms and assets: the term-linking methods these mirror
- Search assets: find knowledge files and assets by GUID or
qualified_name