Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here. Want to know more about what has changed? Check out the Community News blog.

How does a SerDe deserialize method get the appropriate custom Writable?

Highlighted

How does a SerDe deserialize method get the appropriate custom Writable?

New Contributor

How does a SerDe deserialize method get the appropriate custom Writable?

In my custom SerDe the deserialize method always returns a Text instead of customWritable?

the table names and the field names in my customwritable are exactly the same...

 

public class StockWritableSerDe implements SerDe {

:

:

public Object deserialize(Writable field) throws SerDeException {

if (field instanceof StockPriceWritable) {
StockPriceWritable stock = (StockPriceWritable) field;
row.set(0, stock.getSymbol());
row.set(1, stock.getDates());
row.set(2, stock.getOpen());
row.set(3, stock.getHigh());
row.set(4, stock.getLow());
row.set(5, stock.getClose());
row.set(6, stock.getVolume());
row.set(7, stock.getAdjClose());
}

else

{

 throw new SerDeException(this.getClass().getName()
 + " unexpected Writable type " + field.getClass().getName());

}

}

  :

  :

}

 

----------------------------------------------------------------------------------------------------------------------------------

public class StockPriceWritable implements WritableComparable<StockPriceWritable>, Cloneable {
String symbol;
String dates;
double open;
double high;
double low;
double close;
int volume;
double adjClose;

public StockPriceWritable() {
}

public StockPriceWritable(String symbol,
String dates,
double open,
double high,
double low,
double close,
int volume,
double adjClose) {
this.symbol = symbol;
this.dates = dates;
this.open = open;
this.high = high;
this.low = low;
this.close = close;
this.volume = volume;
this.adjClose = adjClose;
}

@Override
public void write(DataOutput out) throws IOException {
WritableUtils.writeString(out, symbol);
WritableUtils.writeString(out, dates);
out.writeDouble(open);
out.writeDouble(high);
out.writeDouble(low);
out.writeDouble(close);
out.writeInt(volume);
out.writeDouble(adjClose);
}

@Override
public void readFields(DataInput in) throws IOException {
symbol = WritableUtils.readString(in);
dates = WritableUtils.readString(in);
open = in.readDouble();
high = in.readDouble();
low = in.readDouble();
close = in.readDouble();
volume = in.readInt();
adjClose = in.readDouble();
}

@Override
public int compareTo(StockPriceWritable passwd) {
return CompareToBuilder.reflectionCompare(this, passwd);
}

@Override
public String toString() {
return ToStringBuilder
.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}

public String getSymbol() {
return symbol;
}

public void setSymbol(String symbol) {
this.symbol = symbol;
}

public String getDates() {
return dates;
}

public void setDates(String dates) {
this.dates = dates;
}

public double getOpen() {
return open;
}

public void setOpen(double open) {
this.open = open;
}

public double getHigh() {
return high;
}

public void setHigh(double high) {
this.high = high;
}

public double getLow() {
return low;
}

public void setLow(double low) {
this.low = low;
}

public double getClose() {
return close;
}

public void setClose(double close) {
this.close = close;
}

public int getVolume() {
return volume;
}

public void setVolume(int volume) {
this.volume = volume;
}

public double getAdjClose() {
return adjClose;
}

public void setAdjClose(double adjClose) {
this.adjClose = adjClose;
}

public static StockPriceWritable fromLine(String line)
throws IOException {
CSVParser parser = new CSVParser();
String[] parts = parser.parseLine(line);

StockPriceWritable stock = new StockPriceWritable(
//<co id="ch03_comment_seqfile_write3"/>
parts[0], parts[1], Double.valueOf(parts[2]),
Double.valueOf(parts[3]),
Double.valueOf(parts[4]),
Double.valueOf(parts[5]),
Integer.valueOf(parts[6]),
Double.valueOf(parts[7])
);
return stock;
}
}