1

i am trying to read sample Json file through Apache Spark, during this process i observed one thing is that you need to keep entire json object into single line. If i keep entire json object into single line,code is working well otherwise getting exception.

This is my json data:

    [
    {
        "id": 2,
        "name": "An ice sculpture",
        "price": 12.50,
        "tags": ["cold", "ice"],
        "dimensions": {
            "length": 7.0,
            "width": 12.0,
            "height": 9.5
        },
        "warehouseLocation": {
            "latitude": -78.75,
            "longitude": 20.4
        }
    },
    {
        "id": 3,
        "name": "A blue mouse",
        "price": 25.50,
        "dimensions": {
            "length": 3.1,
            "width": 1.0,
            "height": 1.0
        },
        "warehouseLocation": {
            "latitude": 54.4,
            "longitude": -32.7
        }
    }
]

This is my code:

SparkSession session = new SparkSession.Builder().appName("JsonRead").master("local").getOrCreate();
        Dataset<Row> json = session.read().json("/Users/mac/Desktop/a.json");
        json.select("tags").show();

In case of small datasets its okay, is any other way to process large json datasets?

2
  • What exception do you get? Commented Nov 14, 2016 at 11:10
  • Exception in thread "main" org.apache.spark.sql.AnalysisException: cannot resolve 'tags' given input columns: [_corrupt_record]; Commented Nov 14, 2016 at 11:14

1 Answer 1

2

see the document: http://spark.apache.org/docs/2.0.1/sql-programming-guide.html#json-datasets

JSON Datasets

Note that the file that is offered as a json file is not a typical JSON file. Each line must contain a separate, self-contained valid JSON object. As a consequence, a regular multi-line JSON file will most often fail.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.