Reputation: 10324
I want to store some Pig variable to Hadoop SequenceFile, so as to run external MapReduce jobs.
Assume that my data has the (chararray, int) schema:
(hello,1)
(test,2)
(example,3)
I wrote this storing function:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.OutputFormat;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.pig.StoreFunc;
import org.apache.pig.data.Tuple;
public class StoreTest extends StoreFunc {
private String storeLocation;
private RecordWriter writer;
private Job job;
public StoreTest(){
}
@Override
public OutputFormat getOutputFormat() throws IOException {
//return new TextOutputFormat();
return new SequenceFileOutputFormat();
}
@Override
public void setStoreLocation(String location, Job job) throws IOException {
this.storeLocation = location;
this.job = job;
System.out.println("Load location is " + storeLocation);
FileOutputFormat.setOutputPath(job, new Path(location));
System.out.println("Out path " + FileOutputFormat.getOutputPath(job));
}
@Override
public void prepareToWrite(RecordWriter writer) throws IOException {
this.writer = writer;
}
@Override
public void putNext(Tuple tuple) throws IOException {
try {
Text k = new Text(((String)tuple.get(0)));
IntWritable v = new IntWritable((Integer)tuple.get(1));
writer.write(k, v);
} catch (InterruptedException ex) {
Logger.getLogger(StoreTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and this Pig code:
register MyUDFs.jar;
x = load '/user/pinoli/input' as (a:chararray,b:int);
store x into '/user/pinoli/output/' using StoreTest();
However, the storing fails and I get this error:
ERROR org.apache.pig.tools.pigstats.PigStats - ERROR 0: java.io.IOException: wrong key class: org.apache.hadoop.io.Text is not class org.apache.hadoop.io.LongWritable
Is there any way to fix that??
Upvotes: 0
Views: 760
Reputation: 10650
The problem is that you didn't set the output key/value class. You can do this in the setStoreLocation()
method:
@Override
public void setStoreLocation(String location, Job job) throws IOException {
this.storeLocation = location;
this.job = job;
this.job.setOutputKeyClass(Text.class); // !!!
this.job.setOutputValueClass(IntWritable.class); // !!!
...
}
I guess you want to use your storer with different key/value types. In that case you can pass their types to the constructor. E.g:
private Class<? extends WritableComparable> keyClass;
private Class<? extends Writable> valueClass;
...
public StoreTest() {...}
@SuppressWarnings({ "unchecked", "rawtypes" })
public StoreTest(String keyClass, String valueClass) {
try {
this.keyClass = (Class<? extends WritableComparable>) Class.forName(keyClass);
this.valueClass = (Class<? extends Writable>) Class.forName(valueClass);
}
catch (Exception e) {
throw new RuntimeException("Invalid key/value type", e);
}
}
...
@Override
public void setStoreLocation(String location, Job job) throws IOException {
this.storeLocation = location;
this.job = job;
this.job.setOutputKeyClass(keyClass);
this.job.setOutputValueClass(valueClass);
...
}
Then, set the correct types in the Pig script:
register MyUDFs.jar;
DEFINE myStorer StoreTest('org.apache.hadoop.io.Text', 'org.apache.hadoop.io.IntWritable');
x = load '/user/pinoli/input' as (a:chararray,b:int);
store x into '/user/pinoli/output/' using myStorer();
Upvotes: 1