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

com.aliyun.datahub.client.impl.batch.arrow.ArrowSchemaCache Maven / Gradle / Ivy

The newest version!
package com.aliyun.datahub.client.impl.batch.arrow;

import com.aliyun.datahub.client.exception.DatahubClientException;
import com.aliyun.datahub.client.impl.batch.BatchConstants;
import com.aliyun.datahub.client.model.RecordSchema;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.arrow.vector.types.pojo.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.concurrent.ExecutionException;

public class ArrowSchemaCache {
    private final static Logger LOGGER = LoggerFactory.getLogger(ArrowSchemaCache.class);
    private static Schema blobArrowSchema;
    private static LoadingCache schemaCache;

    static {
        try {
            blobArrowSchema = ArrowUtils.genArrowSchema(null);
            schemaCache = CacheBuilder.newBuilder()
                                  .expireAfterAccess(Duration.ofSeconds(BatchConstants.SCHEMA_EXPIRE_TIMEOUT))
                                  .initialCapacity(BatchConstants.INITIAL_CAPACITY)
                                  .maximumSize(BatchConstants.MAX_CAPACITY)
                                  .build(new CacheLoader() {
                                      @Override
                                      public Schema load(RecordSchema recordSchema) throws Exception {
                                          return ArrowUtils.genArrowSchema(recordSchema);
                                      }
                                  });
            LOGGER.info("Init arrow schema cache success");
        } catch (Exception e) {
            LOGGER.error("Init arrow schema cache failed", e);
        }
    }

    public static Schema getSchema(RecordSchema recordSchema) {
        try {
            return recordSchema != null ? schemaCache.get(recordSchema) : blobArrowSchema;
        } catch (ExecutionException e) {
            throw new DatahubClientException(e.getMessage());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy