繁体   English   中英

java.lang.NoSuchMethodError:org.codehaus.jackson.JsonFactory.enable(Lorg / codehaus / jackson / JsonParser $ Feature;

[英]java.lang.NoSuchMethodError: org.codehaus.jackson.JsonFactory.enable(Lorg/codehaus/jackson/JsonParser$Feature;

在Eclipse中执行Java代码时出现以下错误(我未使用Maven)

Exception in thread "main" java.lang.NoSuchMethodError: org.codehaus.jackson.JsonFactory.enable(Lorg/codehaus/jackson/JsonParser$Feature;)Lorg/codehaus/jackson/JsonFactory;
    at org.apache.avro.Schema.<clinit>(Schema.java:88)
    at org.apache.avro.Schema$Parser.parse(Schema.java:997)
    at com.rishav.avro.AvroExampleWithoutCodeGeneration.serialize(AvroExampleWithoutCodeGeneration.java:36)
    at com.rishav.avro.AvroExampleWithoutCodeGeneration.main(AvroExampleWithoutCodeGeneration.java:94)

我正在使用罐子:

avro-1.8.2.jar
java-jason.jar
jason-simple-1.1.1.jar
org.apache.sling.commons.json-2.0.6-sources.jar
org.apache.sling.launchpad-9
jackson-core-asl-1.1.0.jar
jackson-mapper-asl-1.1.0.jar

第36行->架构schema = new Schema.Parser()。parse(new File(“ StudentActivity.avsc”));

package com.rishav.avro;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;

import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
//import org.apache.avro.io.BinaryDecoder;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;


import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.simple.JSONObject;



public class AvroExampleWithoutCodeGeneration {
    public void serialize() throws JsonParseException, JsonProcessingException, IOException {

        InputStream in = new FileInputStream("StudentActivity.json");

        // create a schema
        Schema schema = new Schema.Parser().parse(new File("StudentActivity.avsc"));**// THIS IS LINE 36**
        // create a record to hold json
        GenericRecord AvroRec = new GenericData.Record(schema);
        // create a record to hold course_details 
        GenericRecord CourseRec = new GenericData.Record(schema.getField("course_details").schema());
        // this file will have AVro output data
        File AvroFile = new File("resources/StudentActivity.avro");
        // Create a writer to serialize the record
        DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema);              
        DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(datumWriter);

        dataFileWriter.create(schema, AvroFile);

        // iterate over JSONs present in input file and write to Avro output file
        ObjectMapper mapper = new ObjectMapper();
        Iterator it= (Iterator) mapper.readValue(new JsonFactory().createJsonParser(in), JSONObject.class); 
        while (it.hasNext())
        {
        //for (Iterator it = mapper.readValues(new JsonFactory().createJsonParser(in), JSONObject.class); it.hasNext();) {

            JSONObject JsonRec = (JSONObject) it.next();
            AvroRec.put("id", JsonRec.get("id"));
            AvroRec.put("student_id", JsonRec.get("student_id"));
            AvroRec.put("university_id", JsonRec.get("university_id"));

            LinkedHashMap CourseDetails = (LinkedHashMap) JsonRec.get("course_details");
            CourseRec.put("course_id", CourseDetails.get("course_id"));
            CourseRec.put("enroll_date", CourseDetails.get("enroll_date"));
            CourseRec.put("verb", CourseDetails.get("verb"));
            CourseRec.put("result_score", CourseDetails.get("result_score"));

            AvroRec.put("course_details", CourseRec);

            dataFileWriter.append(AvroRec);
        }  // end of for loop

        in.close();
        dataFileWriter.close();

    } // end of serialize method

    public void deserialize () throws IOException {
        // create a schema
        Schema schema = new Schema.Parser().parse(new File("resources/StudentActivity.avsc"));
        // create a record using schema
        GenericRecord AvroRec = new GenericData.Record(schema);
        File AvroFile = new File("resources/StudentActivity.avro");
        DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema);
        DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(AvroFile, datumReader);
        System.out.println("Deserialized data is :");
        while (dataFileReader.hasNext()) {
            AvroRec = dataFileReader.next(AvroRec);
            System.out.println(AvroRec);
        }
    }

    public static void main(String[] args) throws JsonParseException, JsonProcessingException, IOException {
        AvroExampleWithoutCodeGeneration AvroEx = new AvroExampleWithoutCodeGeneration();
        AvroEx.serialize();
        AvroEx.deserialize();
    }

}

您可以改为:

Schema schema = new Schema.Parser().parse.newFile("resources/StudentActivity.avsc"); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM