tREST use case example- Use Rest API in Talend

The tREST component serves as a REST Web service client that sends HTTP requests to a REST Web service provider and gets the responses.

This scenario describes a simple Job that invokes a REST Web service to get a user record from GitHub and then retrieve the user information. When executed, the Job displays relevant information on the Run console.

Note: For this demonstration we are going consume GitHub API so make sure you have account with Auth2 authentication.

Scenario 1: list down GitHub user Followers 

API URL:

https://api.github.com/users/{username}/followers

Parameter: {Username} = git hub user name or login name

  • Add tREST component from pallet to job designer canvas and configure as follows.
    • URL= “https://api.github.com/users/mralexgray/followers”
    • HTTP Method= “GET”
  • Add tLogRow after tREST and connect with main connection.
  • Synch Schema on tLogRow.
  • Run the Job, it will show you result in JSON format see the below Image.
tREST Followers Example API fetch result
tREST Followers Example API fetch result

You can see it is so simple to consume Git Hub API using Talend but it is giving raw result in JSON format, which we can parse it using tExtractJSONField component.

Modify above job to parse the result.

  • Remove tLogRow and add tExtractJSONField component after tREST and connect with main connection. 
  • Add following Schema to tExtractJSONField component.
login
id
avatar_url
gravatar_id
url
html_url
followers_url
following_url
gists_url
starred_url
subscriptions_url
organizations_url
repos_url
events_url
received_events_url
type
site_admin
  •  Use same column name as XPath Query e.g. login=”login”
  • JSON Field=”Body”
  • Loop XPath Query=”/”
  • Below image shows the complete setting.
tExtractJSONField Setting for tREST json result parse
tExtractJSONField Setting for tREST json result parse

Now we are half way done, just need to add tLogRow to see the output on console.

  • Add tLogRow after tExtractJSONField and connect with main connection.
  • Save and run the job it will show you below output on run console.
tREST:- GItHub API Followers complet job
tREST:- GItHub API Followers complete job

 

About dwetl

One thought on “tREST use case example- Use Rest API in Talend

  1. Awesome.
    How do I call rest API that requires Authentication?
    I tried putting the Authorization and content-type in HTTP headers but that didn’t work.

Leave a Reply

Your email address will not be published. Required fields are marked *