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  public void writeByte(byte b, String tag) throws IOException {
065    writeLong((long)b, tag);
066  }
067    
068  public void writeBool(boolean b, String tag) throws IOException {
069    printCommaUnlessFirst();
070    String val = b ? "T" : "F";
071    stream.print(val);
072    throwExceptionOnError(tag);
073  }
074    
075  public void writeInt(int i, String tag) throws IOException {
076    writeLong((long)i, tag);
077  }
078    
079  public void writeLong(long l, String tag) throws IOException {
080    printCommaUnlessFirst();
081    stream.print(l);
082    throwExceptionOnError(tag);
083  }
084    
085  public void writeFloat(float f, String tag) throws IOException {
086    writeDouble((double)f, tag);
087  }
088    
089  public void writeDouble(double d, String tag) throws IOException {
090    printCommaUnlessFirst();
091    stream.print(d);
092    throwExceptionOnError(tag);
093  }
094    
095  public void writeString(String s, String tag) throws IOException {
096    printCommaUnlessFirst();
097    stream.print(Utils.toCSVString(s));
098    throwExceptionOnError(tag);
099  }
100    
101  public void writeBuffer(Buffer buf, String tag)
102    throws IOException {
103    printCommaUnlessFirst();
104    stream.print(Utils.toCSVBuffer(buf));
105    throwExceptionOnError(tag);
106  }
107    
108  public void startRecord(Record r, String tag) throws IOException {
109    if (tag != null && !"".equals(tag)) {
110      printCommaUnlessFirst();
111      stream.print("s{");
112      isFirst = true;
113    }
114  }
115    
116  public void endRecord(Record r, String tag) throws IOException {
117    if (tag == null || "".equals(tag)) {
118      stream.print("\n");
119      isFirst = true;
120    } else {
121      stream.print("}");
122      isFirst = false;
123    }
124  }
125    
126  public void startVector(ArrayList v, String tag) throws IOException {
127    printCommaUnlessFirst();
128    stream.print("v{");
129    isFirst = true;
130  }
131    
132  public void endVector(ArrayList v, String tag) throws IOException {
133    stream.print("}");
134    isFirst = false;
135  }
136    
137  public void startMap(TreeMap v, String tag) throws IOException {
138    printCommaUnlessFirst();
139    stream.print("m{");
140    isFirst = true;
141  }
142    
143  public void endMap(TreeMap v, String tag) throws IOException {
144    stream.print("}");
145    isFirst = false;
146  }
147}