All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.odps.jdbc.utils.RecordConverterCache Maven / Gradle / Ivy

package com.aliyun.odps.jdbc.utils;

import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;

import com.aliyun.odps.data.converter.OdpsRecordConverter;

/**
 * @author dingxin ([email protected])
 */
public class RecordConverterCache {

  static Map> recordConverters;

  public static OdpsRecordConverter get(TimeZone timeZone) {
    if (timeZone == null) {
      timeZone = TimeZone.getDefault();
    }
    if (recordConverters == null) {
      recordConverters = new ConcurrentHashMap<>();
    }
    if (recordConverters.get(timeZone.getID()) == null) {
      TimeZone finalTimeZone = timeZone;
      recordConverters.put(timeZone.getID(), ThreadLocal.withInitial(() -> {
        return OdpsRecordConverter.builder()
            .setStrictMode(false)
            .enableParseNull()
            .timezone(finalTimeZone.getID())
            .build();
      }));
    }
    return recordConverters.get(timeZone.getID()).get();
  }

  /**
   * clear all record converter, should be called when odps driver is de-registered
   * TODO: not called now
   */
  public static void cleanup() {
    for (ThreadLocal converter : recordConverters.values()) {
      converter.remove();
    }
    recordConverters.clear();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy