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

com.alibaba.ververica.connectors.odps.schema.ODPSTableSchema Maven / Gradle / Ivy

There is a newer version: 1.17-vvr-8.0.8
Show newest version
package com.alibaba.ververica.connectors.odps.schema;

import com.aliyun.odps.Column;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.flink.util.Preconditions.checkArgument;

/**
 * ODPS table schema information including column information and isPartition table.
 */
public class ODPSTableSchema implements Serializable {
	private static final long serialVersionUID = -6327923765714170499L;

	private final List columns;

	private final boolean isPartition;

	private final boolean isView;

	private transient Map columnMap;

	public ODPSTableSchema(
			List normalColumns,
			List partitionColumns,
			boolean isView) {

		checkArgument(
				normalColumns != null && !normalColumns.isEmpty(),
				"input normal columns cannot be null or empty!");

		List columnList = new ArrayList<>();
		for (Column column: normalColumns) {
			columnList.add(new ODPSColumn(column.getName(), column.getType()));
		}
		this.isView = isView;

		boolean hasPartitionCols = partitionColumns != null && !partitionColumns.isEmpty();

		if (hasPartitionCols) {
			List partitionColumnList = new ArrayList<>();
			for (Column column: partitionColumns) {
				partitionColumnList.add(new ODPSColumn(column.getName(), column.getType(), true));
			}
			columnList.addAll(partitionColumnList);
		}
		isPartition = !isView && hasPartitionCols;
		this.columns = columnList;
		rebuildColumnMap();
	}

	public List getColumns() {
		return columns;
	}

	public boolean isPartition() {
		return isPartition;
	}

	public boolean isView() {
		return isView;
	}

	public ODPSColumn getColumn(String name) {
		return columnMap.get(name);
	}

	public boolean isPartitionColumn(String name) {
		ODPSColumn column = columnMap.get(name);
		if (column != null) {
			return column.isPartition();
		} else {
			throw new IllegalArgumentException("unknown column " + name);
		}
	}

	private void readObject(ObjectInputStream inputStream) throws IOException,
			ClassNotFoundException {
		inputStream.defaultReadObject();
		rebuildColumnMap();
	}

	private void rebuildColumnMap() {
		Map tmpMap = new HashMap<>();
		for (ODPSColumn column:columns) {
			if (column != null){
				tmpMap.put(column.getName(), column);
			}
		}
		this.columnMap = tmpMap;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy