From commits-return-14007-archive-asf-public=cust-asf.ponee.io@hudi.apache.org Sun Mar 22 20:17:11 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 5F3261806C5 for ; Sun, 22 Mar 2020 21:17:10 +0100 (CET) Received: (qmail 28147 invoked by uid 500); 22 Mar 2020 20:17:09 -0000 Mailing-List: contact commits-help@hudi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hudi.apache.org Delivered-To: mailing list commits@hudi.apache.org Received: (qmail 28019 invoked by uid 99); 22 Mar 2020 20:17:09 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 22 Mar 2020 20:17:09 +0000 From: GitBox To: commits@hudi.apache.org Subject: [GitHub] [incubator-hudi] bvaradar commented on a change in pull request #1150: [HUDI-288]: Add support for ingesting multiple kafka streams in a single DeltaStreamer deployment Message-ID: <158490822950.4339.10036523925764733689.gitbox@gitbox.apache.org> References: In-Reply-To: Date: Sun, 22 Mar 2020 20:17:09 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit bvaradar commented on a change in pull request #1150: [HUDI-288]: Add support for ingesting multiple kafka streams in a single DeltaStreamer deployment URL: https://github.com/apache/incubator-hudi/pull/1150#discussion_r396132803 ########## File path: hudi-utilities/src/main/java/org/apache/hudi/utilities/deltastreamer/HoodieMultiTableDeltaStreamer.java ########## @@ -0,0 +1,259 @@ +/* + * 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.hudi.utilities.deltastreamer; + +import org.apache.hadoop.fs.FileUtil; +import org.apache.hudi.DataSourceWriteOptions; +import org.apache.hudi.common.util.FSUtils; +import org.apache.hudi.common.util.TypedProperties; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.utilities.UtilHelpers; +import org.apache.hudi.utilities.deltastreamer.HoodieDeltaStreamer.Config; +import org.apache.hudi.utilities.schema.SchemaRegistryProvider; + +import com.beust.jcommander.JCommander; +import com.google.common.base.Strings; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.apache.spark.api.java.JavaSparkContext; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Wrapper over HoodieDeltaStreamer.java class. + * Helps with ingesting incremental data into hoodie datasets for multiple tables. + * Currently supports only COPY_ON_WRITE storage type. + */ +public class HoodieMultiTableDeltaStreamer { + + private static Logger logger = LogManager.getLogger(HoodieMultiTableDeltaStreamer.class); + + private List tableExecutionObjects; + private transient JavaSparkContext jssc; + private Set successTables; + private Set failedTables; + + public HoodieMultiTableDeltaStreamer(String[] args, JavaSparkContext jssc) throws IOException { + this.tableExecutionObjects = new ArrayList<>(); + this.successTables = new HashSet<>(); + this.failedTables = new HashSet<>(); + this.jssc = jssc; + String commonPropsFile = getCommonPropsFileName(args); + String configFolder = getConfigFolder(args); + FileSystem fs = FSUtils.getFs(commonPropsFile, jssc.hadoopConfiguration()); + configFolder = configFolder.charAt(configFolder.length() - 1) == '/' ? configFolder.substring(0, configFolder.length() - 1) : configFolder; + checkIfPropsFileAndConfigFolderExist(commonPropsFile, configFolder, fs); + TypedProperties properties = UtilHelpers.readConfig(fs, new Path(commonPropsFile), new ArrayList<>()).getConfig(); + //get the tables to be ingested and their corresponding config files from this properties instance + populateTableExecutionObjectList(properties, configFolder, fs, args); + } + + private void checkIfPropsFileAndConfigFolderExist(String commonPropsFile, String configFolder, FileSystem fs) throws IOException { + if (!fs.exists(new Path(commonPropsFile))) { + throw new IllegalArgumentException("Please provide valid common config file path!"); + } + + if (!fs.exists(new Path(configFolder))) { + fs.mkdirs(new Path(configFolder)); + } + } + + private void checkIfTableConfigFileExists(String configFolder, FileSystem fs, String configFilePath) throws IOException { + if (!fs.exists(new Path(configFilePath)) || !fs.isFile(new Path(configFilePath))) { + throw new IllegalArgumentException("Please provide valid table config file path!"); + } + + Path path = new Path(configFilePath); + Path filePathInConfigFolder = new Path(configFolder, path.getName()); + if (!fs.exists(filePathInConfigFolder)) { + FileUtil.copy(fs, path, fs, filePathInConfigFolder, false, fs.getConf()); + } + } + + //commonProps are passed as parameter which contain table to config file mapping + private void populateTableExecutionObjectList(TypedProperties properties, String configFolder, FileSystem fs, String[] args) throws IOException { + List tablesToBeIngested = getTablesToBeIngested(properties); + TableExecutionObject executionObject; + for (String table : tablesToBeIngested) { + String[] tableWithDatabase = table.split("\\."); + String database = tableWithDatabase.length > 1 ? tableWithDatabase[0] : "default"; + String currentTable = tableWithDatabase.length > 1 ? tableWithDatabase[1] : table; + String configProp = Constants.INGESTION_PREFIX + database + Constants.DELIMITER + currentTable + Constants.INGESTION_CONFIG_SUFFIX; + String configFilePath = properties.getString(configProp, configFolder + "/" + database + "_" + currentTable + Constants.DEFAULT_CONFIG_FILE_NAME_SUFFIX); + checkIfTableConfigFileExists(configFolder, fs, configFilePath); + TypedProperties tableProperties = UtilHelpers.readConfig(fs, new Path(configFilePath), new ArrayList<>()).getConfig(); + properties.forEach((k,v) -> { + tableProperties.setProperty(k.toString(), v.toString()); + }); + final Config cfg = new Config(); + String[] tableArgs = args.clone(); + String targetBasePath = resetTarget(tableArgs, database, currentTable); + JCommander cmd = new JCommander(cfg); + cmd.parse(tableArgs); + String overriddenTargetBasePath = tableProperties.getString(Constants.TARGET_BASE_PATH_PROP, ""); + cfg.targetBasePath = Strings.isNullOrEmpty(overriddenTargetBasePath) ? targetBasePath : overriddenTargetBasePath; + if (cfg.enableHiveSync && Strings.isNullOrEmpty(tableProperties.getString(DataSourceWriteOptions.HIVE_TABLE_OPT_KEY(), ""))) { + throw new HoodieException("Hive sync table field not provided!"); + } + populateSchemaProviderProps(cfg, tableProperties); + executionObject = new TableExecutionObject(); + executionObject.setProperties(tableProperties); + executionObject.setConfig(cfg); + executionObject.setDatabase(database); + executionObject.setTableName(currentTable); + this.tableExecutionObjects.add(executionObject); + } + } + + private List getTablesToBeIngested(TypedProperties properties) { + String combinedTablesString = properties.getString(Constants.TABLES_TO_BE_INGESTED_PROP); + if (combinedTablesString == null) { + return new ArrayList<>(); + } + String[] tablesArray = combinedTablesString.split(","); + return Arrays.asList(tablesArray); + } + + private void populateSchemaProviderProps(Config cfg, TypedProperties typedProperties) { + if (cfg.schemaProviderClassName.equals(SchemaRegistryProvider.class.getName())) { + String schemaRegistryBaseUrl = typedProperties.getString(Constants.SCHEMA_REGISTRY_BASE_URL_PROP); + String schemaRegistrySuffix = typedProperties.getString(Constants.SCHEMA_REGISTRY_URL_SUFFIX_PROP); + typedProperties.setProperty(Constants.SOURCE_SCHEMA_REGISTRY_URL_PROP, schemaRegistryBaseUrl + typedProperties.getString(Constants.KAFKA_TOPIC_PROP) + schemaRegistrySuffix); + typedProperties.setProperty(Constants.TARGET_SCHEMA_REGISTRY_URL_PROP, schemaRegistryBaseUrl + typedProperties.getString(Constants.KAFKA_TOPIC_PROP) + schemaRegistrySuffix); + } + } + + public static void main(String[] args) throws IOException { + JavaSparkContext jssc = UtilHelpers.buildSparkContext("multi-table-delta-streamer", Constants.LOCAL_SPARK_MASTER); + try { + new HoodieMultiTableDeltaStreamer(args, jssc).sync(); + } finally { + jssc.stop(); + } + } + + private static String getCommonPropsFileName(String[] args) { + String commonPropsFileName = "common_props.properties"; + for (int i = 0; i < args.length; i++) { + if (args[i].equals(Constants.PROPS_FILE_PROP)) { + commonPropsFileName = args[i + 1]; + break; + } + } + return commonPropsFileName; + } + + private static String getConfigFolder(String[] args) { Review comment: Same here. ---------------------------------------------------------------- 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