Hisoka-X commented on code in PR #9688: URL: https://github.com/apache/seatunnel/pull/9688#discussion_r2347371504
########## seatunnel-api/src/main/java/org/apache/seatunnel/api/metalake/GravitinoClient.java: ########## @@ -0,0 +1,66 @@ +/* + * 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.seatunnel.api.metalake; + +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.JsonNode; +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.ObjectMapper; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +public class GravitinoClient implements MetalakeClient { + private final String metalakeUrl; + + public GravitinoClient(String metalakeUrl) { + this.metalakeUrl = metalakeUrl; + } + + @Override + public String getType() { + return "gravitino"; + } + + @Override + public JsonNode getMetaInfo(String sourceId) throws IOException { + try (CloseableHttpClient client = HttpClients.createDefault()) { + HttpGet request = new HttpGet(this.metalakeUrl + sourceId); + request.addHeader("Accept", "application/vnd.gravitino.v1+json"); + try (CloseableHttpResponse response = client.execute(request)) { + HttpEntity entity = response.getEntity(); + if (entity == null) { + throw new RuntimeException("No response entity"); + } + ObjectMapper mapper = new ObjectMapper(); Review Comment: Please reuse `JsonUtils` ########## seatunnel-api/src/main/java/org/apache/seatunnel/api/metalake/MetalakeConfigUtils.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.seatunnel.api.metalake; + +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.JsonNode; +import org.apache.seatunnel.shade.com.typesafe.config.Config; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigList; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigObject; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValue; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValueFactory; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValueType; + +import org.apache.seatunnel.common.utils.PlaceholderUtils; + +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@Slf4j +public class MetalakeConfigUtils { + + private static final Pattern pattern = Pattern.compile("\\$\\{([^}]*)\\}"); + + public static Config getMetalakeConfig(Config jobConfigTmp) { + Config update = jobConfigTmp; + String metalakeType = System.getenv("METALAKE_TYPE"); + String metalakeUrl = System.getenv("METALAKE_URL"); + + MetalakeClient metalakeClient = MetalakeClientFactory.create(metalakeType, metalakeUrl); + + try { + ConfigList sourceList = jobConfigTmp.getList("source"); + List<ConfigValue> newSourceList = new ArrayList<>(sourceList); + + for (int i = 0; i < sourceList.size(); i++) { + ConfigObject sourceObj = (ConfigObject) sourceList.get(i); + if (sourceObj.containsKey("sourceId")) { + ConfigObject tmp = sourceObj; + String sourceId = sourceObj.toConfig().getString("sourceId"); + JsonNode metalakeJson = metalakeClient.getMetaInfo(sourceId); + for (Map.Entry<String, ConfigValue> entry : sourceObj.entrySet()) { + String subKey = entry.getKey(); + ConfigValue value = entry.getValue(); + + if (value.valueType() == ConfigValueType.STRING) { + String strValue = (String) value.unwrapped(); + Matcher matcher = pattern.matcher(strValue); + if (matcher.find()) { + String placeholder = matcher.group(1); + + if (metalakeJson.has(placeholder)) { + String replaced = metalakeJson.get(placeholder).asText(); + String newValue = + PlaceholderUtils.replacePlaceholders( + strValue, placeholder, replaced); + tmp = + tmp.withValue( + subKey, + ConfigValueFactory.fromAnyRef(newValue)); + } + } + } + } + newSourceList.set(i, tmp); + } + } + update = update.withValue("source", ConfigValueFactory.fromIterable(newSourceList)); + } catch (IOException e) { + log.error("Fail to get MetaInfo, metalakeUrl: {}", metalakeUrl, e); + } + + try { + ConfigList sinkList = jobConfigTmp.getList("sink"); + List<ConfigValue> newSinkList = new ArrayList<>(sinkList); + + for (int i = 0; i < sinkList.size(); i++) { + ConfigObject sinkObj = (ConfigObject) sinkList.get(i); + if (sinkObj.containsKey("sourceId")) { + ConfigObject tmp = sinkObj; + String sourceId = sinkObj.toConfig().getString("sourceId"); + JsonNode metalakeJson = metalakeClient.getMetaInfo(sourceId); + for (Map.Entry<String, ConfigValue> entry : sinkObj.entrySet()) { + String subKey = entry.getKey(); + ConfigValue value = entry.getValue(); + + if (value.valueType() == ConfigValueType.STRING) { + String strValue = (String) value.unwrapped(); + Matcher matcher = pattern.matcher(strValue); + if (matcher.find()) { + String placeholder = matcher.group(1); + + if (metalakeJson.has(placeholder)) { + String replaced = metalakeJson.get(placeholder).asText(); Review Comment: parse source and sink has same code. Let's refactor it again! ########## tools/dependencies/known-dependencies.txt: ########## @@ -9,7 +9,9 @@ disruptor-3.4.4.jar guava-27.0-jre.jar hazelcast-5.1.jar httpclient-4.5.13.jar +httpcore-4.4.13.jar httpcore-4.4.16.jar +httpcore-4.4.4.jar Review Comment: It's weired we introduce new two version of httpcore. Can you analyze the dependencies and use only httpcore-4.4.16.jar? ########## seatunnel-api/src/main/java/org/apache/seatunnel/api/metalake/MetalakeConfigUtils.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.seatunnel.api.metalake; + +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.JsonNode; +import org.apache.seatunnel.shade.com.typesafe.config.Config; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigList; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigObject; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValue; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValueFactory; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValueType; + +import org.apache.seatunnel.common.utils.PlaceholderUtils; + +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@Slf4j +public class MetalakeConfigUtils { + + private static final Pattern pattern = Pattern.compile("\\$\\{([^}]*)\\}"); + + public static Config getMetalakeConfig(Config jobConfigTmp) { + Config update = jobConfigTmp; + String metalakeType = System.getenv("METALAKE_TYPE"); + String metalakeUrl = System.getenv("METALAKE_URL"); Review Comment: Maybe user can overwrite it on job config `env` part is better. cc @liugddx ########## seatunnel-api/src/main/java/org/apache/seatunnel/api/metalake/MetalakeConfigUtils.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.seatunnel.api.metalake; + +import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.JsonNode; +import org.apache.seatunnel.shade.com.typesafe.config.Config; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigList; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigObject; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValue; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValueFactory; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValueType; + +import org.apache.seatunnel.common.utils.PlaceholderUtils; + +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@Slf4j +public class MetalakeConfigUtils { + + private static final Pattern pattern = Pattern.compile("\\$\\{([^}]*)\\}"); + + public static Config getMetalakeConfig(Config jobConfigTmp) { + Config update = jobConfigTmp; + String metalakeType = System.getenv("METALAKE_TYPE"); + String metalakeUrl = System.getenv("METALAKE_URL"); + + MetalakeClient metalakeClient = MetalakeClientFactory.create(metalakeType, metalakeUrl); + + try { + ConfigList sourceList = jobConfigTmp.getList("source"); + List<ConfigValue> newSourceList = new ArrayList<>(sourceList); + + for (int i = 0; i < sourceList.size(); i++) { + ConfigObject sourceObj = (ConfigObject) sourceList.get(i); + if (sourceObj.containsKey("sourceId")) { + ConfigObject tmp = sourceObj; + String sourceId = sourceObj.toConfig().getString("sourceId"); + JsonNode metalakeJson = metalakeClient.getMetaInfo(sourceId); + for (Map.Entry<String, ConfigValue> entry : sourceObj.entrySet()) { + String subKey = entry.getKey(); + ConfigValue value = entry.getValue(); + + if (value.valueType() == ConfigValueType.STRING) { + String strValue = (String) value.unwrapped(); + Matcher matcher = pattern.matcher(strValue); + if (matcher.find()) { + String placeholder = matcher.group(1); Review Comment: how about move this part into `PlaceholderUtils` with new method? -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
