เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การเขียนไฟล์ CSV แบบกำหนดรูปแบบ

หลังจากทำความสะอาดข้อมูลแล้ว ต้องส่งออกข้อมูลเพื่อให้ผู้ใช้งานกลุ่มต่าง ๆ นำไปใช้ต่อ ทีม Analytics ต้องการไฟล์ CSV แบบมาตรฐาน ระบบในยุโรปใช้เครื่องหมายเซมิโคลอนเป็นตัวคั่น และเมนเฟรมรุ่นเก่าต้องการไฟล์ที่ไม่มีหัวคอลัมน์ ตัวเลือกการส่งออกที่ยืดหยุ่นช่วยให้ข้อมูลทำงานร่วมกับระบบที่หลากหลายได้อย่างราบรื่น

คลาส Table และ CsvWriteOptions ถูก import ให้แล้ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การนำเข้าข้อมูลใน 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());
        }
    }
}
แก้ไขและรันโค้ด