Podstawy danych JSON w Tablesaw

Importowanie danych w Javie

Anthony Markham

VP Quant Developer

Wprowadzenie do JSON

  • JSON: JavaScript Object Notation
    • Lekki format wymiany danych
    • Standardowy format web API
    • Struktura par klucz-wartość

Przykład JSON

Importowanie danych w Javie

Dane tabelaryczne

  • Stałe wiersze i kolumny
  • Tabelaryczne: jednorodne typy danych
| Name  | Age | City    |
|-------|-----|---------|
| Alice | 30  | Boston  |
| Bob   | 25  | Seattle |

JSON

  • Elastyczne struktury zagnieżdżone
  • Mieszane typy danych i hierarchie
[
  {"name": "Alice", 
      "age": 30, 
      "address": {"city": "Boston", 
                  "state": "MA"}},
  {"name": "Bob", 
      "age": 25, 
      "address": {"city": "Seattle", 
                  "state": "WA"}}
]
Importowanie danych w Javie

Odczyt JSON

  • Stosować tylko dla prostych plików JSON
// Simple method to read a JSON file
Table products = Table.read().file("products.json");

$$

import tech.tablesaw.io.json.JsonReader;
import tech.tablesaw.io.json.JsonReadOptions;
  • Oferuje dodatkową konfigurację
// Reading a JSON file using JsonReadOptions
JsonReadOptions options = JsonReadOptions.builder("products.json").build();

Table products = new JsonReader().read(options);
Importowanie danych w Javie

Dostęp do danych JSON

  • Znane metody Tablesaw działają bez zmian
// Access columns from JSON data
StringColumn names = table.stringColumn("name");
DoubleColumn prices = table.doubleColumn("price");

// Perform calculations double avgPrice = prices.mean(); String mostExpensive = table .where(prices.isEqualTo(prices.max())) .stringColumn("name").get(0);
Importowanie danych w Javie

Najlepsze praktyki JSON – walidacja

  • Walidacja struktury JSON przed przetwarzaniem
// Validation and error handling
try {
    JsonReadOptions options = JsonReadOptions.builder("data.json").build();
    Table data = new JsonReader().read(options);
    if (data.rowCount() > 0) {
        // Process data
    }
} catch (Exception e) {
    System.err.println("Error reading JSON: " + e.getMessage());
}
if (data.rowCount() == 100) {
    System.out.println("Table has exactly 100 rows - processing data");
}
Importowanie danych w Javie

Najlepsze praktyki JSON – brakujące wartości

  • Obsługa brakujących/pustych wartości
// Remove rows with any missing values
data = data.dropRowsWithMissingValues();
  • Dopasowanie typów danych do potrzeb analizy
// Convert integer column to double for calculations
data = data.replaceColumn("price", data.intColumn("price").asDoubleColumn());
Importowanie danych w Javie

Czas na ćwiczenia!

Importowanie danych w Javie

Preparing Video For Download...