書式を指定した CSV ファイルの書き出し
データのクリーニングが完了したら、さまざまな利用者向けにエクスポートする必要があります。分析チームは標準的な CSV を必要とし、ヨーロッパのシステムはセミコロン区切りを想定しており、レガシーなメインフレームはヘッダーなしのファイルを要求します。柔軟なエクスポートオプションを活用することで、多様な技術環境にデータをスムーズに統合できます。
Table クラスと CsvWriteOptions クラスはあらかじめインポートされています。
この演習はコースの一部です
Java でのデータインポート
演習の手順
- テーブルを標準 CSV として書き出しましょう。
- セミコロン区切りで書き出しましょう。
- レガシーシステム向けに、ヘッダーなしで書き出しましょう。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
public class WriteCSVFiles {
public static void main(String[] args) {
try {
Table employeePerformance = Table.read().csv("employee_performance.csv");
System.out.println("Original data structure:");
System.out.println(employeePerformance.structure());
System.out.println("First 3 rows:");
System.out.println(employeePerformance.first(3));
// Write the table as a standard CSV
employeePerformance.____.csv("employee_performance_standard.csv");
// Write with a semicolon separator
employeePerformance.write().csv(CsvWriteOptions
.____("employee_performance_semicolon.csv")
.____(';')
.build());
// Write without headers for a legacy system
employeePerformance.write().csv(CsvWriteOptions
.builder("employee_performance_noheader.csv")
.____(____)
.build());
System.out.println("\nFiles successfully written to output directory.");
Table readBack = Table.read().csv("employee_performance_standard.csv");
System.out.println("\nVerification - Reading back semicolon-separated file:");
System.out.println(readBack.first(3));
} catch (Exception e) {
System.err.println("Error processing CSV files: " + e.getMessage());
}
}
}