Thursday, 28 April 2016

Talend integration with slack


Problem - You need to post message to your slack channel webhook using talend. This message could be something like sending a summary of your load job, its success or failure.

Solution - to post a message to slack one of the way is using payload.

payload={"channelname": "#yourchannelname", "username": "anyusernameyoucanput", "text": "message to be posted", "icon_emoji": ":ghost:"}'

you can then post this payload using below
curl -X POST --data-urlencode 'payload={"channelname": "#yourchannelname", "username": "anyusernameyoucanput", "text": "message to be posted", "icon_emoji": ":ghost:"}'

in Talend  you can use below in tJava component to do above.
here there are some context.* (context variables used which you have to define in your job)
Once the context parameters are defined, you can run the job and if you have entered things correctly you should be able to post message to slack.

String strSlackPayload ="";
strSlackPayload += "{\"channel\": \"#";
strSlackPayload += context.slack_channel_name+"\", \"username\": \""+context.slack_user_name+"\"";
strSlackPayload += ", \"text\": \"";
strSlackPayload += "message to be sent";
strSlackPayload += "\", \"icon_emoji\": \":white_check_mark:\"}";

String line;
StringBuffer jsonString = new StringBuffer();

URL url = new URL(context.slack_webhook_url);

//escape the double quotes in json string
String payload=strSlackPayload;

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
        jsonString.append(line);
}
br.close();
connection.disconnect();
    

Talend batch job wrapper


Problem - You have multiple jobs which you want to run in a batch in a sequence and with dependencies established between them.

Solution - you can very well call talend jobs inside another talend job. Lets say you have Job1, job2....job10 and you want to run them in  your load batch as below

START -->job1--->Job2--->job3......-->job9--->job10

Here job1 should only run once job2 is done.

To do above you have to create a new talend job - which we can call batch_load job.
inside this job you have to drag and drop your job1...job10 from repository panel.

then you have to establish triggers OnSubJobOK between them.
once you have done that, your batch job is ready to be exported and then deployed and executed.


batch_job with multiple jobs called from it

you can set what to do when child job fails