Hadoop 2.6 introduced the DFSInotifyEventInputStream , which you can use to do this. You can get its instance from HdfsAdmin , and then just call .take() or .poll() to get all the events. Event types include deletion, addition, and creation, which should cover what you are looking for.
Here is a basic example. Make sure that you run it as the hdfs user, as the administrator interface requires root HDFS.
public static void main( String[] args ) throws IOException, InterruptedException, MissingEventsException { HdfsAdmin admin = new HdfsAdmin( URI.create( args[0] ), new Configuration() ); DFSInotifyEventInputStream eventStream = admin.getInotifyEventStream(); while( true ) { EventBatch events = eventStream.take(); for( Event event : events.getEvents() ) { System.out.println( "event type = " + event.getEventType() ); switch( event.getEventType() ) { case CREATE: CreateEvent createEvent = (CreateEvent) event; System.out.println( " path = " + createEvent.getPath() ); break; default: break; } } } }
Here is a blog post that covers it in more detail:
http://johnjianfang.blogspot.com/2015/03/hdfs-6634-inotify-in-hdfs.html?m=1
kichik
source share