通用数据记录无法转换为Avro

时间:2015-06-22 18:17:24

标签: java hadoop mapreduce avro mapper

我最近运行了 Sqoop 作业,然后使用 AvroTools 提取avro文件的架构,然后将其编译为 Java 上课。每当我尝试使用Avro对象时,我都会收到以下ClassCastException:

  

java.lang.Exception:java.lang.ClassCastException:   org.apache.avro.generic.GenericData $ Record无法转换为   com.xxx.xxx.patient_avro.PatientAvro at   org.apache.hadoop.mapred.LocalJobRunner $ Job.runTasks(LocalJobRunner.java:462)         在   org.apache.hadoop.mapred.LocalJobRunner $ Job.run(LocalJobRunner.java:522)     引起:java.lang.ClassCastException:   org.apache.avro.generic.GenericData $ Record无法转换为   com.xxx.xxx.patient_avro.PatientAvro at   com.xxx.xxx.knn_mapreduce.KNNMapper.map(KNNMapper.java:28)at   com.xxx.xxx.knn_mapreduce.KNNMapper.map(KNNMapper.java:14)at   org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)at   org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)at at   org.apache.hadoop。   mapred.MapTask.run(MapTask.java:341)

我的映射器:

public class KNNMapper extends Mapper<AvroKey<PatientAvro>, NullWritable, LongWritable, PatientWritable> {

PatientWritable patient;
LongWritable providerKey;
Integer patientKey;
Integer lengthOfStay;
Integer msDrgGroup;
Integer age;

@Override
public void map(AvroKey<PatientAvro> patientAvro, NullWritable value, Context context) throws IOException, InterruptedException {



    patientKey = patientAvro.datum().getPatientKey();
    lengthOfStay = patientAvro.datum().getLengthOfStay();
    msDrgGroup = patientAvro.datum().getMsDrg();
    age = patientAvro.datum().getAge();
    patient = new PatientWritable();
    patient.set(new Long(patientKey), new Double((double) lengthOfStay), new Double((double) msDrgGroup), new Double((double)age));

    providerKey = new LongWritable(patientAvro.datum().getProviderKey());
    context.write(providerKey, patient);
}
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

Sqoop自动创建Java和.avsc文件。你是分开创建的吗?所以如果你只是使用生成的Java,你应该没问题。