Review Board 1.7.22


FLUME-1689: BodyTextSerializer should allow an option not to add a newline to each serialized event

Review Request #7939 - Created Nov. 7, 2012 and submitted

Mike Percy
FLUME-1689
Reviewers
Flume
flume-git
Small patch to enable either configurable appending of a newline, as done today, or not. Also added docs.
Added / improved unit tests.
flume-ng-core/src/main/java/org/apache/flume/serialization/BodyTextEventSerializer.java
Revision dc291cd New Change
[20] 19 lines
[+20]
20

    
   
20

   
21
import java.io.IOException;
21
import java.io.IOException;
22
import java.io.OutputStream;
22
import java.io.OutputStream;
23
import org.apache.flume.Context;
23
import org.apache.flume.Context;
24
import org.apache.flume.Event;
24
import org.apache.flume.Event;
25
import org.apache.flume.conf.Configurable;

   
26
import org.slf4j.Logger;
25
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
26
import org.slf4j.LoggerFactory;
28

    
   
27

   
29
/**
28
/**
30
 * This class simply writes the body of the event to the output stream
29
 * This class simply writes the body of the event to the output stream
31
 * and appends a newline after each event.
30
 * and appends a newline after each event.
32
 */
31
 */
33
public class BodyTextEventSerializer implements EventSerializer, Configurable {
32
public class BodyTextEventSerializer implements EventSerializer {
34

    
   
33

   
35
  private final static Logger logger =
34
  private final static Logger logger =
36
      LoggerFactory.getLogger(BodyTextEventSerializer.class);
35
      LoggerFactory.getLogger(BodyTextEventSerializer.class);
37

    
   
36

   

    
   
37
  // for legacy reasons, by default, append a newline to each event written out

    
   
38
  private final String APPEND_NEWLINE = "appendNewline";

    
   
39
  private final boolean APPEND_NEWLINE_DFLT = true;

    
   
40

   
38
  private final OutputStream out;
41
  private final OutputStream out;

    
   
42
  private final boolean appendNewline;
39

    
   
43

   
40
  private BodyTextEventSerializer(OutputStream out) {
44
  private BodyTextEventSerializer(OutputStream out, Context ctx) {

    
   
45
    this.appendNewline = ctx.getBoolean(APPEND_NEWLINE, APPEND_NEWLINE_DFLT);
41
    this.out = out;
46
    this.out = out;
42
  }
47
  }
43

    
   
48

   
44
  @Override
49
  @Override
45
  public void configure(Context context) {

   
46
    // noop

   
47
  }

   
48

    
   

   
49
  @Override

   
50
  public boolean supportsReopen() {
50
  public boolean supportsReopen() {
51
    return true;
51
    return true;
52
  }
52
  }
53

    
   
53

   
54
  @Override
54
  @Override
[+20] [20] 12 lines
[+20] [+] public void beforeClose() {
67
  }
67
  }
68

    
   
68

   
69
  @Override
69
  @Override
70
  public void write(Event e) throws IOException {
70
  public void write(Event e) throws IOException {
71
    out.write(e.getBody());
71
    out.write(e.getBody());

    
   
72
    if (appendNewline) {
72
    out.write('\n');
73
      out.write('\n');
73
  }
74
    }

    
   
75
  }
74

    
   
76

   
75
  @Override
77
  @Override
76
  public void flush() throws IOException {
78
  public void flush() throws IOException {
77
    // noop
79
    // noop
78
  }
80
  }
79

    
   
81

   
80
  public static class Builder implements EventSerializer.Builder {
82
  public static class Builder implements EventSerializer.Builder {
81

    
   
83

   
82
    @Override
84
    @Override
83
    public EventSerializer build(Context context, OutputStream out) {
85
    public EventSerializer build(Context context, OutputStream out) {
84
      BodyTextEventSerializer s = new BodyTextEventSerializer(out);
86
      BodyTextEventSerializer s = new BodyTextEventSerializer(out, context);
85
      s.configure(context);

   
86
      return s;
87
      return s;
87
    }
88
    }
88

    
   
89

   
89
  }
90
  }
90

    
   
91

   
91
}
92
}
flume-ng-core/src/test/java/org/apache/flume/serialization/TestBodyTextEventSerializer.java
Revision e3cb255 New Change
 
flume-ng-doc/sphinx/FlumeUserGuide.rst
Revision e37d0d1 New Change
 
  1. flume-ng-core/src/main/java/org/apache/flume/serialization/BodyTextEventSerializer.java: Loading...
  2. flume-ng-core/src/test/java/org/apache/flume/serialization/TestBodyTextEventSerializer.java: Loading...
  3. flume-ng-doc/sphinx/FlumeUserGuide.rst: Loading...