카테고리 없음

Talend 데이터 수집 예시

quantumai 2025. 1. 10. 23:30

목표

REST API에서 전자상거래 주문 데이터를 가져와 CSV 파일로 저장하는 워크플로우를 설계하고 구현합니다.


단계별 구현


1단계: Talend 프로젝트 설정

  1. Talend Studio 설치 및 실행.
  2. 새로운 프로젝트 생성:
    • Talend Studio 실행 → Create a New Project 선택 → 프로젝트 이름 입력.

2단계: REST API에서 데이터 가져오기

  1. tRESTClient 컴포넌트 추가
    • Talend Studio의 Palette에서 tRESTClient를 검색하고 캔버스에 추가.
    • 속성 설정:
    • 속성 예시:속성값
      URL https://api.example.com/orders
      HTTP Method GET
      Accept Type application/json
      Headers Authorization: Bearer <API_KEY>

3단계: JSON 데이터 파싱

  1. tExtractJSONFields 컴포넌트 추가
    • tRESTClient의 출력 연결선을 tExtractJSONFields로 연결.
    • JSON 데이터를 개별 필드로 변환.
    • JSON 경로 매핑:필드 이름JSON 경로
      order_id $.order_id
      customer $.customer_name
      amount $.total_amount
      date $.order_date
  2. 스키마 정의:
    • tExtractJSONFields에서 Edit Schema 클릭.
    • 다음 필드를 추가:
      • order_id: Integer
      • customer: String
      • amount: Double
      • date: Date

4단계: 데이터 변환

  1. tMap 컴포넌트 추가
    • tExtractJSONFields의 출력 연결선을 tMap으로 연결.
    • tMap 설정:
      • 필드 매핑:입력 필드출력 필드
        order_id OrderID
        customer Customer
        amount TotalAmount
        date OrderDate
      • Date 변환:
        • TalendDate.parseDate("yyyy-MM-dd", row1.date)를 사용해 날짜 형식 변환.

5단계: 데이터 저장

  1. tFileOutputDelimited 컴포넌트 추가
    • tMap의 출력 연결선을 tFileOutputDelimited로 연결.
    • 속성 설정:
      • File Name: /output/orders_${TalendDate.getDate("yyyyMMdd")}.csv
      • Field Separator: ,
      • Row Separator: \n
      • Header: true
  2. 스키마 설정
    • tFileOutputDelimited에서 Edit Schema 클릭.
    • tMap에서 정의한 출력 필드를 가져옴.

6단계: 워크플로우 실행

  1. 캔버스의 모든 컴포넌트를 연결:
[tRESTClient] --> [tExtractJSONFields] --> [tMap] --> [tFileOutputDelimited]

 

  1. Talend 워크플로우 실행:
    • Run 탭 클릭 → Run Job 버튼 클릭.

결과

  1. CSV 파일 생성:
    • 경로: /output/
    • 파일 이름: orders_YYYYMMDD.csv
  2. CSV 파일 내용 예시:
OrderID,Customer,TotalAmount,OrderDate
101,John Doe,250.75,2025-01-10
102,Jane Smith,120.50,2025-01-09

Talend가 생성한 Java 코드

Talend의 워크플로우는 Java 코드로 변환됩니다. 아래는 일부 생성된 Java 코드 예시입니다:

String restEndpoint = "https://api.example.com/orders";
HttpURLConnection connection = (HttpURLConnection) new URL(restEndpoint).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Authorization", "Bearer <API_KEY>");

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    content.append(inputLine);
}
in.close();
connection.disconnect();

JSONObject json = new JSONObject(content.toString());
JSONArray orders = json.getJSONArray("orders");
for (int i = 0; i < orders.length(); i++) {
    JSONObject order = orders.getJSONObject(i);
    Integer orderId = order.getInt("order_id");
    String customer = order.getString("customer_name");
    Double amount = order.getDouble("total_amount");
    String date = order.getString("order_date");
    
    // CSV 파일에 데이터 쓰기
    // ...
}
 
 
 

Talend 워크플로우 요약

  1. tRESTClient: REST API에서 데이터 가져오기.
  2. tExtractJSONFields: JSON 데이터를 파싱하여 필드 추출.
  3. tMap: 데이터 변환 및 매핑.
  4. tFileOutputDelimited: CSV 파일로 저장.

이 구현은 REST API에서 데이터를 수집하고 CSV 파일로 저장하는 전형적인 데이터 통합 워크플로우입니다.