MapReduce Tasks for message monitoring
The main use of Java with the message class to implement the MapReduce task monitoring, if the MapReduce task error sent an error message. MapReduce error message is obtained through the log in HDFs, the error log is in JSON format, where JSON is converted into XML format sent to the message. The specific code is as follows
import java.io.bufferedreader;import java.io.bytearrayoutputstream;import java.io.ioexception; import java.io.inputstreamreader;import java.io.printstream;import java.net.uri;import java.util.properties;import java.util.stringtokenizer;import javax.mail.authenticator;import javax.mail.message;import javax.mail.messagingexception;import javax.mail.passwordauthentication; import javax.mail.session;import javax.mail.transport;import javax.mail.internet.internetaddress;import javax.mail.internet.mimemessage;import Net.sf.json.jsonarray;import net.sf.json.jsonobject;import net.sf.json.xml.xmlserializer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.filestatus;import org.apache.hadoop.fs.filesystem;import Org.apache.hadoop.fs.path;import org.apache.hadoop.mapreduce.job;public class email&nbsP { private static final string username = "[email Protected] ";//the name of the user who sent the message private static final string password = "123456789";//password for the user name that sent the message private static final string email _host = "smtp.qq.com";//mail Server Host public static void main ( String args[]) { try { sendemail ("Test mail", "test message content!") ", " [email protected] "); System.out.println ("email ok !"); } catch (messagingexception e) { // todo auto-generated catch block&nbsP; e.printstacktrace (); } } /** * @category Send mail method, this method to send the MapReduce task error message, the specific error message through the error log in HDFs to get * @param to Target mailbox (can be multiple mailboxes, separated by, number) * @param job Get jobid * @param time access error log path via timestamp through the job of MapReduce * @throws exception */ public static void senderrmail (string to, job job, string time) throws Exception { string subject = job.getjobname (); string message = geterr (job, time); loginmail lm = new loginmail (Username, password); // Create Session properties props = new properties (); props.put ("Mail.smtp.auth", "true"); props.put ("Mail.smtp.host", email_host); session session = session.getdefaultinstance (PROPS,&NBSP;LM); // Create message Message msg = new mimemessage (session); // Set Send Source Address msg.setfrom (new internetaddress (USERNAME)); // Multi-user decomposition stringtokenizer st = new stringtokenizer (to, ","); string[] Recipients = new string[st.counttokens ()]; int rc = 0; while (St.hasmoretokens ()) recipients[rc++] = st.nexttoken (); InternetAddress[] addressTo = new internetaddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressto[i] = new internetaddress (Recipients[i]); } msg.setrecipients (Message.recipienttype.to, addressto); // set up message topics and send messages msg.setsubject (subject); msg.setcontent (message, " Text/html;charset=utf-8 "); transport.send (msg); } /** * @category Custom theme content delivery, The message content here is not necessarily mapreduce, you can fill in * @param subject topics * @param body content * @param to target mailbox * @throws MessagingException */ public static void sendemail (string subject, string body, String to) &NBSP;&NBSP;&NBsp; throws messagingexception { loginmail lm = new loginmail (USERNAME, PASSWORD); // Create session properties props = new properties (); Props.put ("Mail.smtp.auth", "true"); props.put (" Mail.smtp.host ", email_host); session session = session.getdefaultinstance (PROPS,&NBSP;LM); // created message message msg = new mimemessage (session); // set Send Source Address msg.setfrom (new internetaddress (USERNAME)); // Multi-user decomposition stringtokenizer st = new stringtokenizer (to, ","); string[] recipients = new string[ St.counttokens ()]; int rc = 0; while (St.hasmoretokens ()) recipients[rc++] = st.nexttoken (); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { Addressto[i] = new internetaddress (Recipients[i]); } msg.setrecipients (Message.recipienttype.to, addressto); // set the message subject and send the message msg.setsubject (subject); msg.setcontent (body, "Text/html;charset=utf-8"); transport.send (msg); } /** * @category Get log files * @param job * @param time * @return fsdatainputstream * @throws ioexception */ public static fsdatainputstream getfile (Job job, String Time) throws IOException { string year = time.substring (0, 4); string month = time.substring (4, 6); String day = time.substring (6, 8); string dst = "hdfs://192.168.1.100:9000/tmp/hadoop-yarn/staging/history/done/" + year + "/" + month + "/" + day + "/000000"; filesystem fs = filesystem.get (Uri.create (DST), new configuration ()); filestatus[] status = fs.liststatus (new Path (DST)); &NBSP;&NBSP;&NBSP;&NBsp; fsdatainputstream in = null; for (int i = 0; i < status.length; i++) { if (Status[i].getpath (). GetName () .contains (Job.getjobid (). toString ()) && status[i].getpath (). GetName (). EndsWith (" Jhist ")) { in = new fsdatainputstream (Fs.open (Status[i].getpath ())); } } return in; } /** * @ category parsing File class capacity is xml * @param job * @param time * @return xml * @throws IOException * @throws InterruptedException */ public static string geterr (Job job, string time) throws IOException, interruptedexception { fsdatainputstream in = getfile (job, time); thread t1 = new thread (); while (in == NULL) { &Nbsp; t1.sleep (20000);//Because the log for each job in HDFs is not generated in real time, So you need to check HDFs every 20 seconds whether the job log has generated t1.join (); in = getfile (Job, time) ; } Bufferedreader br = new bufferedreader (New inputstreamreader (in)); String line = ""; jsonobject jo; jsonarray jsa = new Jsonarray (); string xml = ""; xmlserializer xmlserializer = new xmlserializer (); while (Line = br.readline ()) != null) { if (Line.touppercase () indexOf ("Error". toUpperCase ()) > -1) { jo = jsonobject.fromobject (line); jsa.add (Jo); } } xml = xmlserializer.write (JSA); in.close (); br.close (); return xml; } /** * @category Gets the exception content in Try-catch &NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;* @param e Exception * @return Exception content */ public static string getexception (Exception e) { ByteArrayOutputStream out = new Bytearrayoutputstream (); printstream pout = new printstream (out); e.printstacktrace (pout); string ret = new string (Out.toByteArray ()); pout.close (); try { out.close (); } catch (Exception ex) { } return ret; }}class loginmail extends Authenticator { private string username; private string password; public string getusername () { return username; } Public void setusername (String username) { this.username = username; } public string GetPassword () { return password; } public void setpassword (String password) { this.password = password; } @Override &NBSP;&NBSp; protected passwordauthentication getpasswordauthentication () { return new passwordauthentication (Username, password); } public loginmail (string username, string Password) { this.username = username; this.password = password; }}
MapReduce Tasks for message monitoring