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)