1.4-Software-Development-Principles

Geavanceerde streams die json gebruiken om de gegevens te laden

Om de streams API te testen op gegevens uit de echte wereld hebben we het volgende gedownload en geparsed JSON bestand:
https://data.overheid.nl/dataset/kabels-json
in een kortere versie van het bestand: parsed json file.

Het is een dataset van de Nederlandse overheid die alle elektriciteitskabels in Den Haag bevat. Noot: De meegeleverde JSON bevat slechts een deel van de hele dataset. Als je de hele hele dataset wilt hebben, download deze dan via bovenstaande URL.

Je moet code schrijven die de JSON objecten inleest in java objecten. Je kunt deze gegevens gebruiken om de volgende vragen te beantwoorden met behulp van de Java Streams API

code die je kunt gebruiken als sjabloon om de vragen te beantwoorden:

package nl.saxion.exercise;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;

public class Streams {

    private DB db;

    public static void main(String[] args) {
        Streams s = new Streams();
        s.loadDatabase();
        s.query();
    }

    private void query() {
        System.out.println("Print the ObjectIds of all features");
        //TODO
//        db.features.stream()

        System.out.println("How many cables have crossection \"3 x 10\"?");
        //TODO
//      long nrCablesWithCrossSection3x10 =
//                db.features.stream()
//
//        System.out.println(nrCablesWithCrossSection3x10);

        System.out.println("What is the total length of all cables with status \"In bedrijf\"");
        //TODO
//        double totalLengthInBedrijf = db.features.stream()
//
//        System.out.println(totalLengthInBedrijf);

        System.out.println("Distribution cables cost 145eur per kilometer. What is the total cost of all cables " +
                "with function 'Distributie'");
        //TODO
//        double costEuro = db.features.stream()
//
//        System.out.println("cost: " + costEuro);

        System.out.println("Print all classes of cables which are modified after 01-07-2016");
        //TODO


        System.out.println("Print per material the amount of features made of this material");
        /**
         * Expected result:
         *      Onbekend 6383
         *      Aluminium 1
         *      Koper 13591
         */
        //TODO

        System.out.println("Determine and print the number of different crossections");
        //TODO

        System.out.println("How many cables belong to the category cables with the largest crosssection");
        //TODO

    }

    private void loadDatabase() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        try {
            //DB taken from https://data.overheid.nl/dataset/kabels-json
            //Licence https://creativecommons.org/licenses/by/4.0/deed.nl
            db = mapper.readValue(new File("kabels-json.json"), DB.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Reading the JSON file

This is done by mapping the JSON data onto a java Object of the type DB. It is your job to implement this class and all other classes needed to map the JSON data structure to java data structures


package nl.saxion.exercise;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.List;

public class DB {

    /* TODO: add properties to map the data in the json file */

    @Override
    public String toString() {
        return "DB";
    }
}