
## Link data domain and assets

URL: https://docs.atlan.com/product/capabilities/build-apps/sdks/python/how-tos/link-domains

> Add, retrieve, and remove assets from data domains programmatically using the Atlan Python SDK (pyatlan).

# Link domain and assets

Link your asset to a domain for easy discovery and governance.

## Add asset to domain

You can add an asset to a domain or update an existing domain by updating the asset's `domainGUIDs`.
In the example below, we're adding a Table (`MARKETING_SALES`) to the domain (`Marketing`).

### dbt

```yaml showLineNumbers title="Add an asset to a domain"
models:
 - name: MARKETING_SALES # (1)
 meta:
 atlan:
 domainName: "Marketing" # (2)
```

1. You must give the name of the object.
2. You can specify the domain as a human-readable name. Each asset can be assigned to only one domain. You can also replace an existing domain assignment by updating the `domainName` to a different domain.

### Java

```java showLineNumbers title="Add an asset to a domain"
DataDomain domain = DataDomain.findByName(client, "Marketing").get(0); // (1)

Table table = Table.updater( // (2)
 "default/snowflake/1726834662/RAW/WIDEWORLDIMPORTERS/MARKETING_SALES",
 "MARKETING_SALES")
 .domainGUID(domain.getGuid()) // (3)
 .build();

AssetMutationResponse response = table.save(client); // (4)
```

1. You can retrieve a data domain by its human-readable `name` using the `findByName()` method. Because this operation will look up the domain in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.
2. Use the `updater()` method of an asset by providing its `qualifiedName` and `name`.
3. To add the asset to the domain, assign the `guid` of the domain to the `domainGUID` attribute.
4. Finally, call the `save()` method to apply the changes in Atlan. Because this operation will persist the asset in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.

### Python

```python showLineNumbers title="Add an asset to a domain"
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.assets import Table, 

client = AtlanClient()

domain = client.asset.find_domain_by_name("Marketing") # (1)

table = Table.updater( # (2)
 qualified_name="default/snowflake/1726834662/RAW/WIDEWORLDIMPORTERS/MARKETING_SALES",
 name="MARKETING_SALES",
)
table.domain_g_u_i_ds = [domain.guid] # (3)

client.asset.save(table) # (4)
```

1. You can retrieve a data domain by its human-readable `name` using the `find_domain_by_name()` method.
2. Use the `updater()` method of an asset by providing its `qualifiedName` and `name`.
3. To add the asset to the domain, assign the `guid` of the domain to the `asset.domain_g_u_i_ds` attribute.
4. Finally, call the `save()` method to apply the changes in Atlan.

### Kotlin

```kotlin showLineNumbers title="Add an asset to a domain"
val domain = DataDomain.findByName(client, "Marketing")[0] // (1)

val table = Table.updater( // (2)
 "default/snowflake/1726834662/RAW/WIDEWORLDIMPORTERS/MARKETING_SALES",
 "MARKETING_SALES")
 .domainGUID(domain.getGuid()) // (3)
 .build()

val response = table.save(client) // (4)
```

1. You can retrieve a data domain by its human-readable `name` using the `findByName()` method. Because this operation will look up the domain in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.
2. Use the `updater()` method of an asset by providing its `qualifiedName` and `name`.
3. To add the asset to the domain, assign the `guid` of the domain to the `asset.domainGUID` attribute.
4. Finally, call the `save()` method to apply the changes in Atlan. Because this operation will persist the asset in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.

### Raw REST API

```json showLineNumbers title="POST /api/meta/entity/bulk"
{
 "entities": [
 {
 "typeName": "Table", // (1)
 "attributes": {
 "qualifiedName": "default/snowflake/1726834662/RAW/WIDEWORLDIMPORTERS/MARKETING_SALES", // (2)
 "name": "MARKETING_SALES", // (3)
 "domainGUIDs": [
 "db711647-99a9-4c50-93c5-fab0b992a0cc" // (4)
 ]
 }
 }
 ]
 }
```

1. You need to specify the `typeName` of the asset;
for this example, we're updating the `domainGUIDs` for a Snowflake table.
2. You need to specify the `qualifiedName` of the asset.
3. You need to specify the `name` of the asset.
4. To add the asset to the domain, assign the `guid` of the domain to the `domainGUIDs` property.

## Retrieve Assets by Domain

You can retrieve all assets associated with a domain by filtering on the `domainGUIDs`.
In the example below, we retrieve all assets linked to the (`Marketing`) domain.

### Java

```java showLineNumbers title="Retrieve assets from a domain"
String domainName = "Marketing";
DataDomain domain = DataDomain.findByName(client, domainName).get(0); // (1)
String domainGuid = domain.getGuid();
List result = client.assets.select()
 .where(Asset.DOMAIN_GUIDS.eq(domainGuid)) // (2)
 .includeOnResults(Asset.NAME)
 .includeOnResults(Asset.QUALIFIED_NAME) // (3)
 .stream()
 .toList();

result.forEach(asset -> { // (4)
 System.out.println("Asset Name: " + asset.getName());
 System.out.println("Qualified Name: " + asset.getQualifiedName());
});
```

1. You can retrieve a data domain by its human-readable `name` using the `findByName()` method. Because this operation will look up the domain in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.
2. Query all assets linked to the domain using client.assets.select().where(Asset.DOMAIN_GUIDS.eq(domainGuid)).
3. Include specific attributes (for example, Asset.NAME, Asset.QUALIFIED_NAME) in the results using .includeOnResults().
4. Process the retrieved assets and print any specific attributes you need, such as name and qualifiedName in this example.

### Python

```python showLineNumbers title="Retrieve assets from a domain"
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.assets import Asset, Readme
from pyatlan.model.fluent_search import CompoundQuery, FluentSearch

client = AtlanClient()

domain_name = "Marketing"
domain = client.asset.find_domain_by_name(domain_name) # (1)

domain_guid = domain.guid
response = ( # (2)
 FluentSearch()
 .select()
 .where(Asset.DOMAIN_GUIDS.eq(domain_guid))
 .include_on_results(Asset.NAME) # (3)
 .include_on_results(Asset.QUALIFIED_NAME)
 .execute(client=client)
)
for asset in response: # (4)
 print(f"Name: {asset.name}, Qualified Name: {asset.qualified_name}")

```

1. You can retrieve a data domain by its human-readable `name` using the `find_domain_by_name()` method.
2. Query all assets linked to the domain using FluentSearch.select().where(Asset.DOMAIN_GUIDS.eq(domain_guid)).
3. Include specific attributes (for example, Asset.NAME, Asset.QUALIFIED_NAME) in the results using .include_on_results().
4. Process the retrieved assets and print any specific attributes you need, such as name and qualifiedName in this example.

### Kotlin

```kotlin showLineNumbers title="Retrieve assets from a domain"
val domainName = "Marketing"
val domain = DataDomain.findByName(client, domainName)[0] // (1)
val domainGuid = domain.guid
val result = client.assets //(2)
 .select()
 .where(Asset.DOMAIN_GUIDS.eq(domainGuid))
 .includeOnResults(Asset.NAME) // (3)
 .includeOnResults(Asset.QUALIFIED_NAME)
 .stream()
 .toList()
for (assets in result)
```

1. You can retrieve a data domain by its human-readable `name` using the `findByName()` method. Because this operation will look up the domain in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.
2. Query all assets linked to the domain using FluentSearch.search().where(Asset.DOMAIN_GUIDS.eq(domainGuid)).
3. Include specific attributes (for example, Asset.NAME, Asset.QUALIFIED_NAME) in the results using .includeOnResults().
4. Process the retrieved assets and print any specific attributes you need, such as name and qualifiedName in this example.

## Remove asset from domain

You can remove an asset from a domain by updating the asset's `domainGUIDs`.
In the example below, we're removing a table (`MARKETING_SALES`) asset from the existing linked domain.

### dbt

```yaml showLineNumbers title="Remove an asset from a domain"
models:
 - name: MARKETING_SALES # (1)
 meta:
 atlan:
 domainName: "" # (2)
```

1. You must give the name of the object.
2. To remove the asset from the domain, set the `domainName` to an empty string.

### Java

```java showLineNumbers title="Remove an asset from a domain"
Table table = Table.updater( // (1)
 "default/snowflake/1726834662/RAW/WIDEWORLDIMPORTERS/MARKETING_SALES",
 "MARKETING_SALES")
 .nullField(Table.DOMAIN_GUIDS.getAtlanFieldName()) // (2)
 .build();

AssetMutationResponse response = table.save(client); // (3)
```

1. Use the `updater()` method of an asset by providing its `qualifiedName` and `name`.
2. To remove the asset from the domain, set the `domainGUIDs` field as a `nullField` on the builder.
3. Finally, call the `save()` method to apply the changes in Atlan. Because this operation will persist the asset in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.

### Python

```python showLineNumbers title="Remove an asset from a domain"
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.assets import Table, 

client = AtlanClient()

table = Table.updater( # (1)
 qualified_name="default/snowflake/1726834662/RAW/WIDEWORLDIMPORTERS/MARKETING_SALES",
 name="MARKETING_SALES",
)
table.domain_g_u_i_ds = [] # (2)

client.asset.save(table) # (3)
```

1. Use the `updater()` method of an asset by providing its `qualifiedName` and `name`.
2. To remove the asset from the domain, assign the `domain_g_u_i_ds` of the asset to an empty list ie.`[]`.
3. Finally, call the `save()` method to apply the changes in Atlan.

### Kotlin

```kotlin showLineNumbers title="Remove an asset from a domain"
val table = Table.updater( // (1)
 "default/snowflake/1726834662/RAW/WIDEWORLDIMPORTERS/MARKETING_SALES",
 "MARKETING_SALES")
 .nullField(Table.DOMAIN_GUIDS.atlanFieldName) // (2)
 .build()

val response = table.save(client) // (3)
```

1. Use the `updater()` method of an asset by providing its `qualifiedName` and `name`.
2. To remove the asset from the domain, set the `domainGUIDs` field as a `nullField` on the builder.
3. Finally, call the `save()` method to apply the changes in Atlan. Because this operation will persist the asset in Atlan, you must [provide it an `AtlanClient`](https://docs.atlan.com/llms/platform/python/set-up-sdk/llms.txt) through which to connect to the tenant.

### Raw REST API

```json showLineNumbers title="POST /api/meta/entity/bulk"
{
 "entities": [
 {
 "typeName": "Table", // (1)
 "attributes": {
 "qualifiedName": "default/snowflake/1726834662/RAW/WIDEWORLDIMPORTERS/MARKETING_SALES", // (2)
 "name": "MARKETING_SALES", // (3)
 "domainGUIDs": [] // (4)
 }
 }
 ]
 }
```

1. You need to specify the `typeName` of the asset;
for this example, we're updating the `domainGUIDs` for a Snowflake table.
2. You need to specify the `qualifiedName` of the asset.
3. You need to specify the `name` of the asset.
4. To remove the asset from the domain, assign the `domainGUIDs` property of the asset to the empty array.

---
