com.aliyun.datahub.client.impl.batch.BatchConverterFactory Maven / Gradle / Ivy
The newest version!
package com.aliyun.datahub.client.impl.batch;
import com.aliyun.datahub.client.exception.DatahubClientException;
import com.aliyun.datahub.client.impl.batch.arrow.ArrowDeserializer;
import com.aliyun.datahub.client.impl.batch.arrow.ArrowSerializer;
import com.aliyun.datahub.client.impl.batch.avro.AvroDeserializer;
import com.aliyun.datahub.client.impl.batch.avro.AvroSerializer;
import com.aliyun.datahub.client.impl.batch.binary.BinaryBatchDeserializer;
import com.aliyun.datahub.client.impl.batch.binary.BinaryBatchSerializer;
import com.aliyun.datahub.client.impl.batch.header.BatchHeader;
public class BatchConverterFactory {
public static BatchSerializer getSerializer(BatchType protocol) {
switch (protocol) {
case BINARY:
return new BinaryBatchSerializer();
case ARROW:
return new ArrowSerializer();
case AVRO:
return new AvroSerializer();
default:
throw new DatahubClientException("Unknown protocol type: " + protocol);
}
}
public static BatchDeserializer getDeserializer(BatchType protocol) {
switch (protocol) {
case BINARY:
return new BinaryBatchDeserializer();
case ARROW:
return new ArrowDeserializer();
case AVRO:
return new AvroDeserializer();
default:
throw new DatahubClientException("Unknown protocol type: " + protocol);
}
}
public static BatchDeserializer getDeserializer(byte[] bytes) {
BatchHeader header = BatchHeader.parseHeader(bytes);
return getDeserializer(header);
}
public static BatchDeserializer getDeserializer(BatchHeader header) {
if (header.getVersion() < 1) {
return new BinaryBatchDeserializer();
}
switch (header.getDataType()) {
case ARROW:
return new ArrowDeserializer();
case AVRO:
return new AvroDeserializer();
default:
break;
}
throw new DatahubClientException("Unknown batch, version: " + header.getVersion() + ", type: " + header.getDataType());
}
}