Hadoop Combat-flume Custom Sink (19)

Source: Internet
Author: User

ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportOrg.apache.flume.Channel;ImportOrg.apache.flume.Context;Importorg.apache.flume.Event;Importorg.apache.flume.EventDeliveryException;Importorg.apache.flume.Transaction;Importorg.apache.flume.conf.Configurable;ImportOrg.apache.flume.sink.AbstractSink;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory; Public classMysinksextendsAbstractsinkImplementsConfigurable {Private Static FinalLogger Logger = Loggerfactory.getlogger (mysinks.class); Private Static FinalString Prop_key_rootpath = "FileName"; PrivateString FileName; @Override PublicStatus process ()throwseventdeliveryexception {//TODO auto-generated Method StubChannel Ch=Getchannel (); Transaction Txn=ch.gettransaction (); Event Event=NULL;        Txn.begin ();  while(true) {Event=Ch.take (); if(Event! =NULL) {                 Break; }        }        Try{logger.debug ("Get event."); String Body=NewString (Event.getbody ()); System.out.println ("Event.getbody ()-----" +body); String Res= Body + ":" + system.currenttimemillis () + "\ r \ n"; File File=NewFile (fileName); FileOutputStream Fos=NULL; Try{fos=NewFileOutputStream (file,true); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            Try{fos.write (res.getbytes ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            Try{fos.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace ();            } txn.commit (); returnStatus.ready; } Catch(throwable th) {txn.rollback (); if(thinstanceofError) {                Throw(Error) th; } Else {                Throw Neweventdeliveryexception (TH); }        } finally{txn.close (); }} @Override Public voidConfigure (Context context) {//TODO auto-generated Method StubFileName =context.getstring (Prop_key_rootpath); }}

Hadoop Combat-flume Custom Sink (19)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.