scb-java-client

Java client for the SCB (Statistiska centralbyrån) API

License

License

Categories

Categories

Java Languages CLI User Interface
GroupId

GroupId

com.github.dannil
ArtifactId

ArtifactId

scb-java-client
Last Version

Last Version

3.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

scb-java-client
Java client for the SCB (Statistiska centralbyrån) API
Project URL

Project URL

https://github.com/dannil/scb-java-client
Source Code Management

Source Code Management

https://github.com/dannil/scb-java-client

Download scb-java-client

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.dannil/scb-java-client/ -->
<dependency>
    <groupId>com.github.dannil</groupId>
    <artifactId>scb-java-client</artifactId>
    <version>3.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.dannil/scb-java-client/
implementation 'com.github.dannil:scb-java-client:3.0.0'
// https://jarcasting.com/artifacts/com.github.dannil/scb-java-client/
implementation ("com.github.dannil:scb-java-client:3.0.0")
'com.github.dannil:scb-java-client:jar:3.0.0'
<dependency org="com.github.dannil" name="scb-java-client" rev="3.0.0">
  <artifact name="scb-java-client" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.dannil', module='scb-java-client', version='3.0.0')
)
libraryDependencies += "com.github.dannil" % "scb-java-client" % "3.0.0"
[com.github.dannil/scb-java-client "3.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-core jar 2.10.3
com.fasterxml.jackson.core : jackson-databind jar 2.10.3
org.slf4j : slf4j-api jar 1.8.0-beta4

test (6)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.6.0
org.junit.jupiter : junit-jupiter-engine jar 5.6.0
org.junit.platform : junit-platform-launcher jar 1.6.0
org.mockito : mockito-core jar 3.3.3
info.debatty : java-string-similarity jar 1.2.1
org.apache.logging.log4j : log4j-slf4j18-impl jar 2.13.1

Project Modules

There are no modules declared in this project.

scb-java-client

Build Status GitHub Actions CI Maven Central Javadocs Sonar Quality Gate Sonar Tech Debt Sonar Coverage

Java client for the SCB (Swedish: Statistiska centralbyrån, English: Statistics Sweden) API. The goal of this project is to provide an easy and intuitive way for developers to interface their applications with the SCB API without having to know the intricate workings or writing own logic to handle the process.

If you have an improvement, feel free to make a pull request or start an issue if you'd like feedback.

Requirements

  • Java 11 or newer

Installation

Maven

<dependency>
  <groupId>com.github.dannil</groupId>
  <artifactId>scb-java-client</artifactId>
  <version>3.0.0</version>
</dependency>

Gradle

compile 'com.github.dannil:scb-java-client:3.0.0'

SBT

libraryDependencies += "com.github.dannil" % "scb-java-client" % "3.0.0"

Usage

This demonstrates the typical usage of the client.

// Create the client
SCBClient client = new SCBClient();

// Retrieve some client(s) matching the table(s) you want to fetch information from
AgricultureLivestockClient livestockClient = client.agriculture().livestock();
PublicFinancesGovernmentDebtClient debtClient = client.publicFinances().governmentDebt();

// Retrieve all livestock by county data
List<ResponseModel> livestockData = livestockClient.getLivestockByCounty();

// Retrieve all government debt data
List<ResponseModel> debtData = debtClient.getGovernmentDebt();

// You may also want to skip the explicit creation of the client and fetch data
// directly from the method call.
List<ResponseModel> densityData = client.population().statistics().density().getDensity();

The client also supports selecting specific values directly from the SCB API.

// Specify the criterion for the information we want to retrieve
List<String> regions = Arrays.asList("00", "01", "0114");
List<String> types = Arrays.asList("01", "02");

// Defining an input as null means that we want to retrieve all information
// for this input. 
// 
// Example: if the information from the API specifies that it has information 
// for the years 2011 through 2015 (for a specific table), the inputs underneath 
// this row would result in an equivalent response from the API.
//
// List<Integer> years = Arrays.asList(2011, 2012, 2013, 2014, 2015);
// List<Integer> years = null;
// 
// This makes it easy to be selective when we have a case where we 
// want to retrieve all information for one input and at the same time 
// restrict the input for another (for example the years 2012 and 2014).
List<Integer> years = null;

// Retrieve all area statistics using the selected values
List<ResponseModel> areas = client.environment().landAndWaterArea().getArea(regions, types, years);

If you're interested in fetching all JSON data from a particular table that hasn't been implemented as a client or if you want the response to be the original unformatted JSON, you may use the method getRawData().

// Specify the table to retrieve from. The response will be a JSON string containing all the
// available data from our specified table.
String json = client.getRawData("BE/BE0101/BE0101A/BefolkningNy");

The method getRawData() also accepts an input which are used to filter what data should be fetched. The available inputs can be viewed in plaintext from the URL endpoint of the table or through the client. The feature of retrieving the inputs through the client is accessible from the SCBClient class as demonstrated below.

// Fetch all available inputs for a table. Every key in the map corresponds to an available input
// parameter and the values for a specific key are all the available values for a specific input.
// The contents of availableInputs can then be analyzed and used for fetching more specific data.
Map<String, Collection<String>> availableInputs = client.getInputs("BE/BE0101/BE0101A/BefolkningNy");

// Specifies the criterion for the information we want to retrieve, in this case:
// 		The contents code BE0101N1 (the total population, so the API knows what information we want)
//		The regions 00 (whole country), 01 (Stockholm County) and 0114 (Upplands Väsby)
//		The relationship statuses OG (unmarried) and G (married)
//		The ages 45 and 50
//		The genders are null; we want to retrieve information for all genders
//		The years 2011 and 2012
Map<String, Collection<?>> inputs = new HashMap<>();
inputs.put("ContentsCode", Arrays.asList("BE0101N1"));
inputs.put("Region", Arrays.asList("00", "01", "0114"));
inputs.put("Civilstand", Arrays.asList("OG", "G"));
inputs.put("Alder", Arrays.asList(45, 50));
inputs.put("Kon", null);
inputs.put("Tid", Arrays.asList(2011, 2012));

// Specify the table to retrieve from and our inputs to this table. The response will be a JSON
// string containing the data that matched our criterion.
String json = client.getRawData("BE/BE0101/BE0101A/BefolkningNy", inputs);

Sometimes you may want to convert your raw JSON data to a concrete Java bean. This is possible with the GenericModel class, which supplies various methods for retrieving the separate entries from the JSON.

// Fetch all data from a table and construct a GenericModel using this data.
String json = client.getRawData("BE/BE0101/BE0101A/BefolkningNy");
GenericModel m = new GenericModel(json);

// Retrieve all the entries into a collection.
Collection<Map<String, Object>> entries = m.getEntries();

// Retrieve all the entries having the code "alder" with either value 45 or 50 and the code 
// "region" with value 01.
Map<String, Collection<String>> inputs = new HashMap<>();
inputs.put("alder", Arrays.asList("45", "50"));
inputs.put("region", Arrays.asList("01"));

entries = m.getEntries(inputs);

// Retrieve all the entries having the code "alder" with value 45.
entries = m.getEntries("alder", "45");

Versions

Version
3.0.0
2.6.0
2.5.0
2.4.1
2.4.0
2.3.0
2.2.0
2.1.0
2.0.0
1.2.0
1.1.0
1.0.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.1
0.1.0
0.0.4
0.0.3
0.0.2
0.0.1