001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.record;
020
021import java.io.IOException;
022import java.util.TreeMap;
023import java.util.ArrayList;
024import java.io.PrintStream;
025import java.io.OutputStream;
026import java.io.UnsupportedEncodingException;
027
028import org.apache.hadoop.classification.InterfaceAudience;
029import org.apache.hadoop.classification.InterfaceStability;
030
031/**
032 * @deprecated Replaced by <a href="https://hadoop.apache.org/avro/">Avro</a>.
033 */
034@Deprecated
035@InterfaceAudience.Public
036@InterfaceStability.Stable
037public class CsvRecordOutput implements RecordOutput {
038
039  private PrintStream stream;
040  private boolean isFirst = true;
041    
042  private void throwExceptionOnError(String tag) throws IOException {
043    if (stream.checkError()) {
044      throw new IOException("Error serializing "+tag);
045    }
046  }
047 
048  private void printCommaUnlessFirst() {
049    if (!isFirst) {
050      stream.print(",");
051    }
052    isFirst = false;
053  }
054    
055  /** Creates a new instance of CsvRecordOutput */
056  public CsvRecordOutput(OutputStream out) {
057    try {
058      stream = new PrintStream(out, true, "UTF-8");
059    } catch (UnsupportedEncodingException ex) {
060      throw new RuntimeException(ex);
061    }
062  }
063    
064  @Override
065  public void writeByte(byte b, String tag) throws IOException {
066    writeLong((long)b, tag);
067  }
068    
069  @Override
070  public void writeBool(boolean b, String tag) throws IOException {
071    printCommaUnlessFirst();
072    String val = b ? "T" : "F";
073    stream.print(val);
074    throwExceptionOnError(tag);
075  }
076    
077  @Override
078  public void writeInt(int i, String tag) throws IOException {
079    writeLong((long)i, tag);
080  }
081    
082  @Override
083  public void writeLong(long l, String tag) throws IOException {
084    printCommaUnlessFirst();
085    stream.print(l);
086    throwExceptionOnError(tag);
087  }
088    
089  @Override
090  public void writeFloat(float f, String tag) throws IOException {
091    writeDouble((double)f, tag);
092  }
093    
094  @Override
095  public void writeDouble(double d, String tag) throws IOException {
096    printCommaUnlessFirst();
097    stream.print(d);
098    throwExceptionOnError(tag);
099  }
100    
101  @Override
102  public void writeString(String s, String tag) throws IOException {
103    printCommaUnlessFirst();
104    stream.print(Utils.toCSVString(s));
105    throwExceptionOnError(tag);
106  }
107    
108  @Override
109  public void writeBuffer(Buffer buf, String tag)
110    throws IOException {
111    printCommaUnlessFirst();
112    stream.print(Utils.toCSVBuffer(buf));
113    throwExceptionOnError(tag);
114  }
115    
116  @Override
117  public void startRecord(Record r, String tag) throws IOException {
118    if (tag != null && ! tag.isEmpty()) {
119      printCommaUnlessFirst();
120      stream.print("s{");
121      isFirst = true;
122    }
123  }
124    
125  @Override
126  public void endRecord(Record r, String tag) throws IOException {
127    if (tag == null || tag.isEmpty()) {
128      stream.print("\n");
129      isFirst = true;
130    } else {
131      stream.print("}");
132      isFirst = false;
133    }
134  }
135    
136  @Override
137  public void startVector(ArrayList v, String tag) throws IOException {
138    printCommaUnlessFirst();
139    stream.print("v{");
140    isFirst = true;
141  }
142    
143  @Override
144  public void endVector(ArrayList v, String tag) throws IOException {
145    stream.print("}");
146    isFirst = false;
147  }
148    
149  @Override
150  public void startMap(TreeMap v, String tag) throws IOException {
151    printCommaUnlessFirst();
152    stream.print("m{");
153    isFirst = true;
154  }
155    
156  @Override
157  public void endMap(TreeMap v, String tag) throws IOException {
158    stream.print("}");
159    isFirst = false;
160  }
161}