You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flume.apache.org by ar...@apache.org on 2011/10/12 18:55:16 UTC

svn commit: r1182479 - in /incubator/flume/branches/flume-728/flume-ng-core/src: main/java/org/apache/flume/sink/hdfs/ test/java/org/apache/flume/sink/hdfs/

Author: arvind
Date: Wed Oct 12 16:55:16 2011
New Revision: 1182479

URL: http://svn.apache.org/viewvc?rev=1182479&view=rev
Log:
SQOOP-777. Support text output for HDFS sink.

(Prasad Mujumdar via Arvind Prabhakar)

Added:
    incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSTextFormatter.java
Modified:
    incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSFormatterFactory.java
    incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/sink/hdfs/TestHDFSEventSink.java

Modified: incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSFormatterFactory.java
URL: http://svn.apache.org/viewvc/incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSFormatterFactory.java?rev=1182479&r1=1182478&r2=1182479&view=diff
==============================================================================
--- incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSFormatterFactory.java (original)
+++ incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSFormatterFactory.java Wed Oct 12 16:55:16 2011
@@ -30,11 +30,14 @@ public class HDFSFormatterFactory {
 
   static final String hdfsWritableFormat = "Writable";
   static final String hdfsAvroFormat = "Avro";
+  static final String hdfsTextFormat = "Text";
 
   static FlumeFormatter getFormatter(String formatType) throws IOException {
-    if (formatType == hdfsWritableFormat)
+    if (formatType == hdfsWritableFormat) {
       return new HDFSWritableFormatter();
-    else
+    } else if (formatType == hdfsTextFormat) {
+      return new HDFSTextFormatter();
+    } else
       throw new IOException("Incorrect formatter type");
   }
 

Added: incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSTextFormatter.java
URL: http://svn.apache.org/viewvc/incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSTextFormatter.java?rev=1182479&view=auto
==============================================================================
--- incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSTextFormatter.java (added)
+++ incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/sink/hdfs/HDFSTextFormatter.java Wed Oct 12 16:55:16 2011
@@ -0,0 +1,53 @@
+package org.apache.flume.sink.hdfs;
+
+import org.apache.flume.Event;
+import org.apache.flume.sink.FlumeFormatter;
+//import org.apache.hadoop.io.BytesWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.LongWritable;
+
+
+public class HDFSTextFormatter implements FlumeFormatter {
+
+  private Text makeText(Event e) {
+    Text textObject = new Text();
+    textObject.set(e.getBody(), 0, e.getBody().length);
+    return textObject;
+  }
+
+  @Override
+  public Class<LongWritable> getKeyClass() {
+    return LongWritable.class;
+  }
+
+  @Override
+  public Class<Text> getValueClass() {
+    return Text.class;
+  }
+
+  @Override
+  public Object getKey(Event e) {
+    // Write the data to HDFS
+    String timestamp = e.getHeaders().get("timestamp");
+    long eventStamp;
+
+    if (timestamp == null) {
+      eventStamp = System.currentTimeMillis();
+    } else {
+      eventStamp = Long.valueOf(timestamp);
+    }
+    LongWritable longObject = new LongWritable(eventStamp);
+    return longObject;
+  }
+
+  @Override
+  public Object getValue(Event e) {
+    return makeText(e);
+  }
+
+  @Override
+  public byte[] getBytes(Event e) {
+    return makeText(e).getBytes();
+  }
+
+}

Modified: incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/sink/hdfs/TestHDFSEventSink.java
URL: http://svn.apache.org/viewvc/incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/sink/hdfs/TestHDFSEventSink.java?rev=1182479&r1=1182478&r2=1182479&view=diff
==============================================================================
--- incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/sink/hdfs/TestHDFSEventSink.java (original)
+++ incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/sink/hdfs/TestHDFSEventSink.java Wed Oct 12 16:55:16 2011
@@ -148,4 +148,83 @@ public class TestHDFSEventSink {
      * System.exit(1); }
      */
   }
+
+  @Test
+  public void testTextAppend() throws InterruptedException, LifecycleException,
+      EventDeliveryException, IOException {
+
+    final long txnMax = 25;
+    final long rollCount = 3;
+    final long batchSize = 2;
+    final String fileName = "FlumeData";
+
+    // clear the test directory
+    Configuration conf = new Configuration();
+    FileSystem fs = FileSystem.get(conf);
+    Path dirPath = new Path(testPath);
+    fs.delete(dirPath, true);
+    fs.mkdirs(dirPath);
+
+    Context context = new Context();
+
+    context.put("hdfs.path", testPath + "/%Y-%m-%d/%H");
+    context.put("hdfs.filePrefix", fileName);
+    context.put("hdfs.txnEventMax", String.valueOf(txnMax));
+    context.put("hdfs.rollCount", String.valueOf(rollCount));
+    context.put("hdfs.batchSize", String.valueOf(batchSize));
+    context.put("hdfs.writeFormat","Text");
+    context.put("hdfs.fileType", "DataStream");
+
+    Configurables.configure(sink, context);
+
+    Channel channel = new MemoryChannel();
+    Configurables.configure(channel, context);
+
+    sink.setChannel(channel);
+    sink.start();
+
+    Transaction txn = channel.getTransaction();
+
+    Calendar eventDate = Calendar.getInstance();
+
+    // push the event batches into channel
+    for (int i = 1; i < 4; i++) {
+      txn.begin();
+      for (int j = 1; j <= txnMax; j++) {
+        Event event = new SimpleEvent();
+        eventDate.clear();
+        eventDate.set(2011, i, i, i, 0); // yy mm dd
+        event.getHeaders().put("timestamp",
+            String.valueOf(eventDate.getTimeInMillis()));
+        event.getHeaders().put("hostname", "Host" + i);
+
+        event.setBody(("Test." + i + "." + j).getBytes());
+        channel.put(event);
+      }
+      txn.commit();
+
+      // execute sink to process the events
+      sink.process();
+    }
+
+    sink.stop();
+
+    /*
+     * 
+     * // loop through all the files generated and check their contains
+     * FileStatus[] dirStat = fs.listStatus(dirPath); Path fList[] =
+     * FileUtil.stat2Paths(dirStat);
+     * 
+     * try { for (int cnt = 0; cnt < fList.length; cnt++) { SequenceFile.Reader
+     * reader = new SequenceFile.Reader(fs, fList[cnt], conf); LongWritable key
+     * = new LongWritable(); BytesWritable value = new BytesWritable();
+     * 
+     * while (reader.next(key, value)) { logger.info(key+ ":" +
+     * value.toString()); } reader.close(); } } catch (IOException ioe) {
+     * System.err.println("IOException during operation: " + ioe.toString());
+     * System.exit(1); }
+     */
+  }
+
+  
 }