You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/05/29 23:09:58 UTC

[GitHub] [flink] xuefuz commented on a change in pull request #8536: [FLINK-12568][hive] Implement OutputFormat to write Hive tables

xuefuz commented on a change in pull request #8536: [FLINK-12568][hive] Implement OutputFormat to write Hive tables
URL: https://github.com/apache/flink/pull/8536#discussion_r288802922
 
 

 ##########
 File path: flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/batch/connectors/hive/HiveTableOutputFormat.java
 ##########
 @@ -0,0 +1,419 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.batch.connectors.hive;
+
+import org.apache.flink.api.common.io.FinalizeOnMaster;
+import org.apache.flink.api.common.io.InitializeOnMaster;
+import org.apache.flink.api.java.hadoop.common.HadoopInputFormatCommonBase;
+import org.apache.flink.api.java.hadoop.common.HadoopOutputFormatCommonBase;
+import org.apache.flink.api.java.hadoop.mapreduce.utils.HadoopUtils;
+import org.apache.flink.api.java.typeutils.RowTypeInfo;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.catalog.exceptions.CatalogException;
+import org.apache.flink.table.catalog.hive.HMSClientFactory;
+import org.apache.flink.table.dataformat.BaseRow;
+import org.apache.flink.table.dataformat.DataFormatConverters;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.StringUtils;
+
+import org.apache.hadoop.fs.FileStatus;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.common.FileUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.hive.ql.exec.FileSinkOperator;
+import org.apache.hadoop.hive.ql.io.HiveFileFormatUtils;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.serde2.AbstractSerDe;
+import org.apache.hadoop.hive.serde2.SerDeException;
+import org.apache.hadoop.hive.serde2.SerDeUtils;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
+import org.apache.hadoop.io.SequenceFile;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.compress.CompressionCodec;
+import org.apache.hadoop.mapred.FileOutputFormat;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.JobContext;
+import org.apache.hadoop.mapred.JobContextImpl;
+import org.apache.hadoop.mapred.JobID;
+import org.apache.hadoop.mapred.OutputCommitter;
+import org.apache.hadoop.mapred.OutputFormat;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hadoop.mapred.SequenceFileOutputFormat;
+import org.apache.hadoop.mapred.TaskAttemptContext;
+import org.apache.hadoop.mapred.TaskAttemptContextImpl;
+import org.apache.hadoop.mapred.TaskAttemptID;
+import org.apache.hadoop.security.Credentials;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.util.ReflectionUtils;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.OUTDIR;
+
+/**
+ * HiveTableOutputFormat used to write data to hive table, including non-partition and partitioned table.
+ */
+public class HiveTableOutputFormat extends HadoopOutputFormatCommonBase<BaseRow> implements InitializeOnMaster,
+	FinalizeOnMaster {
+
+	private static final Logger LOG = LoggerFactory.getLogger(HiveTableOutputFormat.class);
+
+	private static final long serialVersionUID = 5167529504848109023L;
+
+	private transient JobConf jobConf;
+	private transient String dbName;
+	private transient String tableName;
+	private transient List<String> partitionCols;
+	private transient RowTypeInfo rowTypeInfo;
+	private transient HiveTablePartition hiveTablePartition;
+	private transient Properties tblProperties;
+	private transient boolean overwrite;
+	private transient boolean isPartitioned;
+	private transient boolean isDynamicPartition;
+
+	private transient AbstractSerDe serializer;
+	//StructObjectInspector represents the hive row structure.
+	private transient StructObjectInspector sois;
+	private transient Class<? extends Writable> outputClass;
+	private transient TaskAttemptContext context;
+	private transient DataFormatConverters.DataFormatConverter[] converters;
+
+	// Maps a partition dir name to the corresponding writer. Used for dynamic partitioning.
+	private transient Map<String, HivePartitionWriter> partitionToWriter;
+	// Writer for non-partitioned and static partitioned table
+	private transient HivePartitionWriter staticWriter;
+
+	public HiveTableOutputFormat(JobConf jobConf, String dbName, String tableName, List<String> partitionCols,
+								RowTypeInfo rowTypeInfo, HiveTablePartition hiveTablePartition,
+								Properties tblProperties, boolean overwrite) {
+		super(jobConf.getCredentials());
+
+		Preconditions.checkArgument(!StringUtils.isNullOrWhitespaceOnly(dbName), "DB name is empty");
+		Preconditions.checkArgument(!StringUtils.isNullOrWhitespaceOnly(tableName), "Table name is empty");
+		Preconditions.checkNotNull(rowTypeInfo, "RowTypeInfo cannot be null");
+		Preconditions.checkNotNull(hiveTablePartition, "HiveTablePartition cannot be null");
+		Preconditions.checkNotNull(tblProperties, "Table properties cannot be null");
+
+		HadoopUtils.mergeHadoopConf(jobConf);
+		this.jobConf = jobConf;
+		this.dbName = dbName;
+		this.tableName = tableName;
+		this.partitionCols = partitionCols;
+		this.rowTypeInfo = rowTypeInfo;
+		this.hiveTablePartition = hiveTablePartition;
+		this.tblProperties = tblProperties;
+		this.overwrite = overwrite;
+		isPartitioned = partitionCols != null && !partitionCols.isEmpty();
+		isDynamicPartition = isPartitioned && partitionCols.size() > hiveTablePartition.getPartitionSpec().size();
+	}
+
+	//  Custom serialization methods
+
+	private void writeObject(ObjectOutputStream out) throws IOException {
+		super.write(out);
+		jobConf.write(out);
+		out.writeObject(isPartitioned);
+		out.writeObject(isDynamicPartition);
+		out.writeObject(overwrite);
+		out.writeObject(rowTypeInfo);
+		out.writeObject(hiveTablePartition);
+		out.writeObject(partitionCols);
+		out.writeObject(dbName);
+		out.writeObject(tableName);
+		out.writeObject(tblProperties);
+	}
+
+	@SuppressWarnings("unchecked")
+	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
+		super.read(in);
+		if (jobConf == null) {
+			jobConf = new JobConf();
+		}
+		jobConf.readFields(in);
+		jobConf.getCredentials().addAll(this.credentials);
+		Credentials currentUserCreds = HadoopInputFormatCommonBase.getCredentialsFromUGI(UserGroupInformation.getCurrentUser());
+		if (currentUserCreds != null) {
+			jobConf.getCredentials().addAll(currentUserCreds);
+		}
+		isPartitioned = (boolean) in.readObject();
+		isDynamicPartition = (boolean) in.readObject();
+		overwrite = (boolean) in.readObject();
+		rowTypeInfo = (RowTypeInfo) in.readObject();
+		hiveTablePartition = (HiveTablePartition) in.readObject();
+		partitionCols = (List<String>) in.readObject();
+		dbName = (String) in.readObject();
+		tableName = (String) in.readObject();
+		partitionToWriter = new HashMap<>();
+		tblProperties = (Properties) in.readObject();
+	}
+
+	@Override
+	public void finalizeGlobal(int parallelism) throws IOException {
+		StorageDescriptor jobSD = hiveTablePartition.getStorageDescriptor();
+		Path stagingDir = new Path(jobSD.getLocation());
+		FileSystem fs = stagingDir.getFileSystem(jobConf);
+		IMetaStoreClient client = null;
+		try {
+			client = HMSClientFactory.create(new HiveConf(jobConf, HiveConf.class));
+			Table table = client.getTable(dbName, tableName);
+			if (!isDynamicPartition) {
+				commitJob(stagingDir.toString());
+			}
+			if (isPartitioned) {
+				// TODO: to be implemented
+			} else {
+				moveFiles(stagingDir, new Path(table.getSd().getLocation()));
+			}
+		} catch (TException e) {
+			throw new CatalogException("Failed to query Hive metaStore", e);
+		} finally {
+			if (client != null) {
+				client.close();
+			}
+			fs.delete(stagingDir, true);
+		}
+	}
+
+	@Override
+	public void initializeGlobal(int parallelism) throws IOException {
+	}
+
+	@Override
+	public void configure(Configuration parameters) {
+		// since our writers are transient, we don't need to do anything here
+	}
+
+	@Override
+	public void open(int taskNumber, int numTasks) throws IOException {
+		try {
+			StorageDescriptor sd = hiveTablePartition.getStorageDescriptor();
+			serializer = (AbstractSerDe) Class.forName(sd.getSerdeInfo().getSerializationLib()).newInstance();
+			ReflectionUtils.setConf(serializer, jobConf);
+			// Pass null as partition properties, assuming they're same as table properties
 
 Review comment:
   I think it's possible that a Hive partition can be different from its table in terms of properties. For instance, a partition can has a different storage format. For now, I think we can add a todo item.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services