# Tree Join Transformation

The *Tree Join* Transformation object in the Astera Data Stack enables users to create complex, hierarchical data structures such as EDI or XML documents with ease.

Unlike the standard relational join which combines left and right elements to create a new record, the *Tree Join* Transformation object allows users to create collection and member nodes. It also enables users to join datasets in parent-child hierarchies using a key field. It is a set-level transformation that operates on a group of records.

{% embed url="<https://youtu.be/akQVzwSl85g>" %}

## **Use Case**

In this use case, we have two different source datasets. The first data contains information about *Customers* and has fields such as *CustomerName*, *CustomerID*, *Address*, etc.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FMazGOuRfq0F3yUA85h5h%2Fimage.png?alt=media&#x26;token=76590965-85a8-4a9c-9aef-8461e813360e" alt=""><figcaption></figcaption></figure>

The second dataset contains details of *Orders* placed by customers. It includes fields such as *OrderID*, *CustomerID*, *RequiredDate*, *ShippedDate,* and other shipping details.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FDytJPZ2EtDQ8BHfypzBH%2Fimage.png?alt=media&#x26;token=67908014-a01d-4673-9be1-eb694212fce9" alt=""><figcaption></figcaption></figure>

We will join these two datasets using the *Tree Join* Transformation object and create a hierarchical dataset in which all orders placed by a customer along with the order details are represented in a parent-child hierarchy.

Contrary to the regular *Join* transformation that joins two datasets in a flat layout, the *Tree Join* transformation joins data from two different datasets into a hierarchical data structure.

In this use case, each record from the first dataset that contains *Customer* details will be a parent node, and under the parent node, the details of *Orders* placed by that customer will be returned in a child node.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FJv4Sn3kgkYavVSaidKAs%2Fimage.png?alt=media&#x26;token=5eb7cf60-2296-43ec-848d-fbad95179d45" alt=""><figcaption></figcaption></figure>

## **Using the Tree Join Transformation**

1. To get the *Tree Join* Transformation object from the *Toolbox*, go to *Toolbox > Transformations > Tree Join* and drag-and-drop the *Tree Join* Transformation object onto the designer.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F5rSLNDuw6LGfv7M2NKjy%2FTree%20Join%20Transformation%20Gif%201.gif?alt=media&#x26;token=7f2acc44-8ed5-4fbb-af88-06eb08d50bc1" alt=""><figcaption></figcaption></figure>

2. Now map fields from the *Customer* source dataset to the *TreeJoin* object.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FgimZlnasM8AH5sJyVZio%2FTree%20Join%20Transformation%20Gif%202.gif?alt=media&#x26;token=b7baf5ee-0e34-449a-ba89-b9416b7fdd7b" alt=""><figcaption></figcaption></figure>

3. Right-click on the *TreeJoin* object header and go to *Properties* from the context menu. In the *Tree Join* *Layout Builder* window, you can see the fields from the *Customer* dataset listed under the root node.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FvCYeiu0JPez8a03hSm9H%2Fimage.png?alt=media&#x26;token=0ad2bd73-66f2-440b-a9b4-934e27b222d2" alt=""><figcaption></figcaption></figure>

4. Next, click on the *TreeJoin* node, and you will see that the small icons or buttons at the top of the screen will become active. If you click on the icon, you will get two options:

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FTZNWZCd5BArN0sXfvQkL%2FTree%20Join%20Transformation%20Gif%203.gif?alt=media&#x26;token=35584612-6476-4109-8e10-e3e9b3e0cb81" alt=""><figcaption></figcaption></figure>

* *Add Member Object* – To add a new member node to your layout
* *Add a Collection Object* – To add a new collection node under the parent node. It will return all corresponding records as a collection under the parent node.

In this case, we will *Add a Member Object* to create a separate record for each order placed by a customer, under a separate customer record node.

5. *Add a Member Object* to this root node. A dialogue box will open to name your member object.

In this case, let’s name it ‘*Orders*’ and click *OK*. A member object has been added to our parent node.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F2cIFG6ia3TXh15Xbwa2i%2FTree%20Join%20Transformation%20Gif%204.gif?alt=media&#x26;token=e83bd358-75bc-4af6-b92a-a75df463d29c" alt=""><figcaption></figcaption></figure>

6. Click *OK*, to close the properties window. Now map the *Orders* dataset to the member node that we created in the previous step to complete the layout.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FJm9Y8Obx6uIzzzKM3s4o%2Fimage.png?alt=media&#x26;token=3edf7672-674a-45a6-bee8-79f379060984" alt=""><figcaption></figcaption></figure>

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2Fpli4EmhpI7HecphISJ62%2FTree%20Join%20Transformation%20Gif%205.gif?alt=media&#x26;token=d0386d8b-e819-46dc-804e-c4bf80b50e0c" alt=""><figcaption></figcaption></figure>

7. Go to the properties of the *TreeJoin* object again. We have already created the layout, so we will proceed to the next window.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2Fg3X3exrPxu06dfsWatJb%2Fimage.png?alt=media&#x26;token=e4bcabbb-8032-4988-9384-27aa324dda35" alt=""><figcaption></figcaption></figure>

8. In the *TreeJoin Transformation Properties* window, we must specify the Join Key.

The join key is a common field or a common identifier in both datasets which will identify and join records in a tree-like structure. The parent and child fields are the same fields which are common in both the source datasets and serve as a key identifier to join records.

* *Parent Field* – Join the field from the first dataset
* *Child Field* – Same field as the parent field, selected from the second dataset.

In this case, the *CustomerID* field is common in both datasets, so we will use it as the join key.

9. Click on the *Parent* *field* dropdown button. Expand the *TreeJoin* node and select the *CustomerID* field.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F3CPQyslOmtUtlDivl2Uy%2FTree%20Join%20Transformation%20Gif%206.gif?alt=media&#x26;token=9852d969-007b-43ea-a239-bafe586ef050" alt=""><figcaption></figcaption></figure>

10. Click on the *Child* field column and expand the *TreeJoin* root node. Scroll down to your member node, expand this node, and select the *CustomerID* field from the second dataset.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FQF5h02x2nrLIl0DYPpzd%2FTree%20Join%20Transformation%20Gif%207.gif?alt=media&#x26;token=192d6c5e-0dee-496a-9f57-5c36065f4f63" alt=""><figcaption></figcaption></figure>

Let’s discuss the other options on the properties window:

* *Join In Database* – This lets you join the tables in the database itself rather than in memory. However, it applies only when both tables are sourced from the same database.
* *Case Sensitive* – To process and join records on a case-sensitive basis.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FNcPhFvqODMqGm2sRa57Q%2Fimage.png?alt=media&#x26;token=ffd57f23-c21d-45ec-a3ad-b285a8584c3b" alt=""><figcaption></figcaption></figure>

11. We have our layout and the *TreeJoin* properties ready, click *OK*.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FIPAflukqrZ39PT0wySVi%2Fimage.png?alt=media&#x26;token=56a8eed6-8300-49ec-ad07-5499340e5b3b" alt=""><figcaption></figcaption></figure>

12. Right-click on the *TreeJoin* object and select *Preview Output*.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FdeGpICEX3vGC3iau2R7j%2Fimage.png?alt=media&#x26;token=86313fd4-5d0e-484f-9cd9-82bbd6991b7d" alt=""><figcaption></figcaption></figure>

The *TreeJoin* object has returned the customer records in parent nodes. Upon expanding the node, you can see the order placed by the customer listed as its member unit under the parent node.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FCqL8V13n0pqpgTPhbd9X%2FTree%20Join%20Transformation%20Gif%208.gif?alt=media&#x26;token=b2e58bd8-463b-4491-8cf9-5c38fc4448da" alt=""><figcaption></figcaption></figure>

If we choose to *Add a Collection Object* in the *Layout Builder*, all the records for orders placed by a customer will be returned in a collection under a single parent node for each customer.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FVbcep4nZDlJujUGxIf0J%2FTree%20Join%20Transformation%20Gif%209.gif?alt=media&#x26;token=f7e98a64-dcbb-4387-9aa9-6622c22ee182" alt=""><figcaption></figcaption></figure>

13. The joined dataset can now be written to a desired destination. In this case, we will write it to an [XML File Destination](https://documentation.astera.com/astera-data-stack-v8/dataflows/destinations/xml-json-file-destination) object.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FU2y8F1DyV2PnhwxSviNg%2Fimage.png?alt=media&#x26;token=61bd1237-f9dd-4a9e-8121-ee498789c3b4" alt=""><figcaption></figcaption></figure>

This concludes using the *Tree Join* Transformation object in Astera Data Stack.

Download datasets used in the use case from the following link:

{% file src="<https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2Fgit-blob-83ae41ee5e46577077b2c6ba35dae82ef03bddc4%2FNorthwind.zip?alt=media>" %}
