-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Flink]Support flink migrate hive database
- Loading branch information
Showing
8 changed files
with
534 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...imon-flink-common/src/main/java/org/apache/paimon/flink/action/MigrateDatabaseAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.paimon.flink.action; | ||
|
||
import org.apache.paimon.flink.procedure.MigrateDatabaseProcedure; | ||
|
||
import org.apache.flink.table.procedure.DefaultProcedureContext; | ||
|
||
import java.util.Map; | ||
|
||
/** Migrate from external all hive table in database to paimon table. */ | ||
public class MigrateDatabaseAction extends ActionBase { | ||
private final String connector; | ||
private final String hiveDatabaseName; | ||
private final String tableProperties; | ||
|
||
public MigrateDatabaseAction( | ||
String connector, | ||
String warehouse, | ||
String hiveDatabaseName, | ||
Map<String, String> catalogConfig, | ||
String tableProperties) { | ||
super(warehouse, catalogConfig); | ||
this.connector = connector; | ||
this.hiveDatabaseName = hiveDatabaseName; | ||
this.tableProperties = tableProperties; | ||
} | ||
|
||
@Override | ||
public void run() throws Exception { | ||
MigrateDatabaseProcedure migrateDatabaseProcedure = new MigrateDatabaseProcedure(); | ||
migrateDatabaseProcedure.withCatalog(catalog); | ||
migrateDatabaseProcedure.call( | ||
new DefaultProcedureContext(env), connector, hiveDatabaseName, tableProperties); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ink-common/src/main/java/org/apache/paimon/flink/action/MigrateDatabaseActionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.paimon.flink.action; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** Action Factory for {@link MigrateDatabaseAction}. */ | ||
public class MigrateDatabaseActionFactory implements ActionFactory { | ||
|
||
public static final String IDENTIFIER = "migrate_database"; | ||
|
||
private static final String SOURCE_TYPE = "source_type"; | ||
private static final String OPTIONS = "options"; | ||
|
||
@Override | ||
public String identifier() { | ||
return IDENTIFIER; | ||
} | ||
|
||
@Override | ||
public Optional<Action> create(MultipleParameterToolAdapter params) { | ||
String warehouse = params.get(WAREHOUSE); | ||
String connector = params.get(SOURCE_TYPE); | ||
String sourceHiveDatabase = params.get(DATABASE); | ||
Map<String, String> catalogConfig = optionalConfigMap(params, CATALOG_CONF); | ||
String tableConf = params.get(OPTIONS); | ||
|
||
MigrateDatabaseAction migrateDatabaseAction = | ||
new MigrateDatabaseAction( | ||
connector, warehouse, sourceHiveDatabase, catalogConfig, tableConf); | ||
return Optional.of(migrateDatabaseAction); | ||
} | ||
|
||
@Override | ||
public void printHelp() { | ||
System.out.println( | ||
"Action \"migrate_database\" migrate all tables in database from hive to paimon."); | ||
System.out.println(); | ||
|
||
System.out.println("Syntax:"); | ||
System.out.println( | ||
" migrate_database --warehouse <warehouse_path> --source_type hive " | ||
+ "--database <database_name> " | ||
+ "[--catalog_conf <key>=<value] " | ||
+ "[--options <key>=<value>,<key>=<value>,...]"); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...link-common/src/main/java/org/apache/paimon/flink/procedure/MigrateDatabaseProcedure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.paimon.flink.procedure; | ||
|
||
import org.apache.paimon.catalog.Identifier; | ||
import org.apache.paimon.flink.utils.TableMigrationUtils; | ||
import org.apache.paimon.hive.HiveCatalog; | ||
import org.apache.paimon.utils.ParameterUtils; | ||
|
||
import org.apache.flink.table.procedure.ProcedureContext; | ||
import org.apache.hadoop.hive.metastore.IMetaStoreClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
|
||
/** Migrate procedure to migrate all hive tables in database to paimon table. */ | ||
public class MigrateDatabaseProcedure extends ProcedureBase { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(MigrateDatabaseProcedure.class); | ||
private static final String PAIMON_SUFFIX = "_paimon_"; | ||
|
||
@Override | ||
public String identifier() { | ||
return "migrate_database"; | ||
} | ||
|
||
public String[] call( | ||
ProcedureContext procedureContext, String connector, String sourceDatabasePath) | ||
throws Exception { | ||
return call(procedureContext, connector, sourceDatabasePath, ""); | ||
} | ||
|
||
public String[] call( | ||
ProcedureContext procedureContext, | ||
String connector, | ||
String sourceDatabasePath, | ||
String properties) | ||
throws Exception { | ||
if (!(catalog instanceof HiveCatalog)) { | ||
throw new IllegalArgumentException("Only support Hive Catalog"); | ||
} | ||
HiveCatalog hiveCatalog = (HiveCatalog) this.catalog; | ||
IMetaStoreClient client = hiveCatalog.getHmsClient(); | ||
List<String> sourceTables = client.getAllTables(sourceDatabasePath); | ||
for (String sourceTable : sourceTables) { | ||
String sourceTablePath = sourceDatabasePath + "." + sourceTable; | ||
String targetPaimonTablePath = sourceTablePath + PAIMON_SUFFIX; | ||
|
||
Identifier sourceTableId = Identifier.fromString(sourceTablePath); | ||
Identifier targetTableId = Identifier.fromString(targetPaimonTablePath); | ||
|
||
TableMigrationUtils.getImporter( | ||
connector, | ||
(HiveCatalog) this.catalog, | ||
sourceTableId.getDatabaseName(), | ||
sourceTableId.getObjectName(), | ||
targetTableId.getDatabaseName(), | ||
targetTableId.getObjectName(), | ||
ParameterUtils.parseCommaSeparatedKeyValues(properties)) | ||
.executeMigrate(); | ||
|
||
LOG.info("rename " + targetTableId + " to " + sourceTableId); | ||
this.catalog.renameTable(targetTableId, sourceTableId, false); | ||
} | ||
return new String[] {"Success"}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.