com.aliyun.datahub.client.impl.batch.avro.AvroSchemaCache Maven / Gradle / Ivy
The newest version!
package com.aliyun.datahub.client.impl.batch.avro;
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.avro.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.concurrent.ExecutionException;
public class AvroSchemaCache {
private final static Logger LOGGER = LoggerFactory.getLogger(AvroSchemaCache.class);
private static Schema blobArrowSchema;
private static volatile LoadingCache schemaCache;
static {
try {
blobArrowSchema = AvroUtils.genAvroSchema(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 AvroUtils.genAvroSchema(recordSchema);
}
});
LOGGER.info("Init avro schema cache success");
} catch (Throwable e) {
LOGGER.error("Init avro 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());
}
}
}