应为begin\u对象,但在第1行第6列为字符串

a0x5cqrl  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(247)

我的java代码

public class Recipe {

public static class TokenizerMapper  extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
    Gson gson = new Gson();
    public void map(Object key, Text value, Context context ) throws IOException, InterruptedException {
        Roo roo=gson.fromJson(value.toString(), Roo.class);

        if (roo.manner_of_death != null) {
        word.set(roo.manner_of_death);
        } else  {
            word.set("none");
        }
        context.write(word, one);
    }
}

public static class IntSumReducer
        extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }
        result.set(sum);
        context.write(key, result);
    }
}

public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

    if (otherArgs.length != 2) {
        System.err.println("Usage: recipe <in> <out>");
        System.exit(2);
    }
    @SuppressWarnings("deprecation")
    Job job = new Job(conf, "Recipe");

    job.setJarByClass(Recipe.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
   // FileInputFormat.addInputPath(job, new Path("hdfs://127.0.0.1:9000/in"));
   // FileOutputFormat.setOutputPath(job, new Path("hdfs://127.0.0.1:9000/out"));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
   // job.submit();
}
}

 class Id
{
    public String oid;
}

class Roo
{
    public Id _id ;
    public String resident_status;
    public String month_of_death;
    public String sex;
    public String marital_status; 
    public String manner_of_death;
    public String autopsy;
    public String race;
}

我的json

{
"_id" : ObjectId("5bfc49155fa79a44dca1f9b9"),
"resident_status" : "1",
"month_of_death" : "06",
"sex" : "M",
"marital_status" : "M",
"manner_of_death" : "7",
"autopsy" : "N",
"race" : "02"
}
{                                                    
"_id" : ObjectId("5bfc49155fa79a44dca1f56c"),    
"resident_status" : "1",                         
"month_of_death" : "03",                         
"sex" : "F",                                     
"marital_status" : "D",                          
"manner_of_death" : "7",                         
"autopsy" : "N",                                 
"race" : "01"                                    
}

除id外,所有字段均为字符串
我的错误
18/11/26 18:02:55 info mapreduce.job:task id:attempt\u 1543189350698\u 0010\u m\u000000\u 0,status:failed error:com.google.gson.jsonsyntaxception:java.io.eofexception:com.google.gson.fromjson(gson)第1行第3列输入结束。java:813)
18/11/26 18:02:55 info mapreduce.job:任务id:尝试\u 1543189350698 \u 0010 \u m \u000001 \u 0,状态:失败错误:com.google.gson.jsonsyntaxception:java.lang.illegalstateexception:应为begin \u对象,但在第1行为字符串

2nbm6dog

2nbm6dog1#

当你这么做的时候,

class Id
{
    public String oid;
}

class Roo
{
    public Id _id ;

你告诉gson它正试图解析这种类型的对象

{                                                    
    "_id" : {
       "oid" : "5bfc49155fa79a44dca1f56c"
    }, 
    ...
}

这不是您所拥有的,而且您没有有效的json,因为 ObjectId 没有引号。
第二个问题-mapreduce默认为读取单行数据,如果输入文件只有两行这样就可以了,但是json仍然应该是有效的

{ "_id" : ... }
{ "_id" : ... }

相关问题