Guava Table: 다차원 데이터 처리 방법

다차원 데이터 구조 설계

일반적인 2차원 테이블은 행과 열의 교차점을 기준으로 데이터를 저장하는 구조입니다. 그러나 복잡한 분류나 멀티 인덱스가 필요한 경우, 더 높은 차원의 데이터 관리가 필요합니다. Guava의 Table API는 기본적으로 2차원 구조만 지원하지만, 복합적인 데이터 모델을 위해 다른 컬렉션과 결합하여 다차원 구조를 구현할 수 있습니다.

1. 중첩 테이블을 통한 3차원 데이터 표현

테이블의 각 셀에 또 다른 테이블을 넣어 다차원 구조를 형성할 수 있습니다. 예를 들어 시간, 지역, 지표를 3개 축으로 활용할 수 있습니다:

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

public class ThreeDimensionalTest {
    public static void main(String[] args) {
        // 연도-지역-지표 구조
        Table> salesData = 
            HashBasedTable.create();
        
        // 2024년 데이터
        Table yearData = HashBasedTable.create();
        yearData.put("Sales", 1500);
        yearData.put("Profit", 300);
        
        // 북미 지역 추가
        Table northData = HashBasedTable.create();
        northData.put("Sales", 1000);
        northData.put("Profit", 200);
        
        // 남미 지역 추가
        Table southData = HashBasedTable.create();
        southData.put("Sales", 1200);
        southData.put("Profit", 300);
        
        // 3차원 구조 생성
        salesData.put("2024", "North", northData);
        salesData.put("2024", "South", southData);
        
        // 데이터 조회
        System.out.println("2024년 북미 영업실적: " + 
            salesData.get("2024", "North").get("Sales"));
    }
}

2. 다중 차원 탐색 전략

복합 구조에서 데이터를 접근할 때, 테이블의 행/열을 순회하며 하위 구조를 탐색하는 방식을 사용할 수 있습니다:

public void traverseData(Table> data) {
    for (String year : data.rowKeySet()) {
        System.out.println("연도: " + year);
        for (String region : data.columnKeySet()) {
            Table regionData = data.get(year, region);
            System.out.println("  지역: " + region);
            for (Map.Entry entry : regionData.entrySet()) {
                System.out.println("    " + entry.getKey() + ": " + entry.getValue());
            }
        }
    }
}

3. Map과 결합한 고차원 구조

4차원 이상의 데이터를 처리하려면 Map과 테이블을 중첩할 수 있습니다. 예를 들어 연도-월-지역-지표 구조를 만들 수 있습니다:

import java.util.HashMap;
import java.util.Map;

public class FourDimensionalTest {
    public static void main(String[] args) {
        // 연도 -> 월 -> 지역 -> 지표
        Map>>> 
            quarterlyData = new HashMap<>();
        
        // 2024년 데이터
        Map>> yearData = 
            new HashMap<>();
        
        // 1월 데이터
        Table> monthData = 
            HashBasedTable.create();
        
        // 북미 지역
        Table northMetrics = HashBasedTable.create();
        northMetrics.put("Sales", 1500);
        northMetrics.put("Profit", 300);
        
        // 남미 지역
        Table southMetrics = HashBasedTable.create();
        southMetrics.put("Sales", 1200);
        southMetrics.put("Profit", 300);
        
        // 월별 데이터 저장
        monthData.put("North", northMetrics);
        monthData.put("South", southMetrics);
        
        // 연도 데이터 저장
        yearData.put("2024", monthData);
        
        // 최상위 데이터 저장
        quarterlyData.put("2024", yearData);
        
        // 데이터 조회
        System.out.println("2024년 1월 북미 영업실적: " + 
            quarterlyData.get("2024").get("2024").get("North").get("Sales"));
    }
}

태그: java Guava MultiDimensionalData NestedCollections DataStructures

6월 9일 21:06에 게시됨