Listing 8. Sample Pig UDF to Write the Output in LZO Format

/**
 * Write the LZO file line by line, passing each
 * line as a single-field Tuple to Pig.
 */
public class LzoTextStorer extends LzoBaseStoreFunc {
  private static final TupleFactory tupleFactory_ =
TupleFactory.getInstance();

  protected enum LzoTextLoaderCounters { LinesRead }

  public LzoTextStorer() {}

    @Override
    public OutputFormat getOutputFormat() throws IOException {
            return new TextOutputFormat<WritableComparable, Text>();
    }

    @Override
    public void putNext(Tuple tuple) throws IOException {
            if (writer == null)
                    System.out.println("Writer is null");

            int numElts = tuple.size();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < numElts; i++) {
          String field;
          try {
            field = String.valueOf(tuple.get(i));
          } catch (ExecException ee) {
            throw ee;
          }
          sb.append(field);

          if (i == numElts - 1) {
            // Last field in tuple.
            sb.append('\n');
          } else {
            sb.append('\t');
          }
        }

        Text text = new Text(sb.toString());
            try {
            writer.write(NullWritable.get(), text);
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
    }
}

