Gộp dữ liệu lồng nhau
Nhóm HR lưu trữ dữ liệu nhân viên trong hai tệp JSON riêng: một tệp chứa thông tin cơ bản và một tệp khác chứa các thuộc tính bổ sung. Để tạo hồ sơ nhân viên đầy đủ, bạn cần nối hai bảng này bằng một định danh chung.
Inner join sẽ kết hợp các hàng từ hai bảng khi cột nối khớp nhau — giống như hợp nhất hai danh sách khách theo tên. Các lớp JsonReader, JsonReadOptions, và Table đã được import sẵn cho bạn.
Bài tập này là một phần của khóa học
Nhập dữ liệu trong Java
Hướng dẫn bài tập
- Đặt tên bảng cho tệp JSON thứ nhất.
- Hoàn thiện chuỗi builder cho tệp thứ hai.
- Nối hai bảng theo cột
"id".
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
public class NestedEmployees {
public static void main(String[] args) {
// Set table name for first JSON
JsonReadOptions opts1 = JsonReadOptions
.builder("employees.json")
.____("Main")
.build();
Table main = new JsonReader().read(opts1);
// Complete builder chain for second JSON
JsonReadOptions opts2 = JsonReadOptions
.builder("employees_extra.json")
.tableName("Extra")
.____();
Table extra = new JsonReader().read(opts2);
System.out.println("Main columns: " + main.columnNames());
System.out.println("Extra columns: " + extra.columnNames());
// Join tables on id column
Table joined = main.____("id").____(extra);
System.out.println("\nJoined table (inner join on id):");
System.out.println(joined.print());
}
}