Ожидается BEGIN_OBJECT, но в строке 1 столбца 6 указано значение STRING. - PullRequest
0 голосов
/ 26 ноября 2018

Мой код 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"                                    
}                                                    

Все поля являются строковыми, кроме идентификатора

Моя ошибка

18/11/26 18:02:55 ИНФОРМАЦИЯ mapreduce.Job: идентификатор задачи: try_1543189350698_0010_m_000000_0, статус: СБОЙ Ошибка: com.google.gson.JsonSyntaxException:java.io.EOFException: конец ввода в строке 1 столбца 3 на com.google.gson.Gson.fromJson (Gson.java:813)

18/11/26 18:02:55 INFO mapreduce.Задание: Идентификатор задачи: try_1543189350698_0010_m_000001_0, Статус: СБОЙ Ошибка: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Ожидаемый BEGIN_OBJECT, но в строке 1

.

1 Ответ

0 голосов
/ 26 ноября 2018

Когда вы делаете это,

 class Id
{
    public String oid;
}

class Roo
{
    public Id _id ;

Вы говорите Gson, что он пытается разобрать объект этого типа

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

Это не то, что у вас есть, и выВ любом случае у меня нет действительного JSON, потому что ObjectId не имеет кавычек.


Вторая проблема - MapReduce по умолчанию считывает отдельные строки данных, что было бы хорошо, если бы ваш входной файл был только двумя строками, но JSON по-прежнему должен быть действительным

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