zhangshenghang commented on code in PR #7361: URL: https://github.com/apache/seatunnel/pull/7361#discussion_r1728455498
########## seatunnel-formats/seatunnel-format-protobuf/src/main/java/org/apache/seatunnel/format/protobuf/CompileDescriptor.java: ########## @@ -0,0 +1,103 @@ +/* + * 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.format.protobuf; + +import org.apache.seatunnel.common.utils.FileUtils; +import org.apache.seatunnel.format.protobuf.exception.ProtobufFormatErrorCode; +import org.apache.seatunnel.format.protobuf.exception.SeaTunnelProtobufFormatException; + +import com.github.os72.protocjar.Protoc; +import com.google.protobuf.DescriptorProtos; +import com.google.protobuf.Descriptors; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.List; + +public class CompileDescriptor { + + public static Descriptors.Descriptor compileDescriptorTempFile( + String protoContent, String messageName) + throws IOException, InterruptedException, Descriptors.DescriptorValidationException { + // Because Protobuf can only be dynamically parsed through the descriptor file, the file + // needs to be compiled and generated. The following method is used here to solve the + // problem: generate a temporary directory and compile .proto into a descriptor temporary + // file. The temporary file and directory are deleted after the JVM runs. + File tmpDir = createTempDirectory(); + File protoFile = createProtoFile(tmpDir, protoContent); + String targetDescPath = compileProtoToDescriptor(tmpDir, protoFile); + + try (FileInputStream fis = new FileInputStream(targetDescPath)) { + DescriptorProtos.FileDescriptorSet descriptorSet = + DescriptorProtos.FileDescriptorSet.parseFrom(fis); + Descriptors.FileDescriptor[] descriptorsArray = buildFileDescriptors(descriptorSet); + return descriptorsArray[0].findMessageTypeByName(messageName); + } + } + + private static File createTempDirectory() throws IOException { + File tmpDir = File.createTempFile("tmp_protobuf_", "_proto"); Review Comment: > Put all protobuf file into `/tmp/seatunnel/tmp_protobuf_`? Protobuf needs to convert configuration content into descriptor files using Protoc.runProtoc conversion, which requires temporary directories and files. When executing the code for the first time, protobuf configuration content will be parsed into descriptor files, which will be used to parse data -- 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]
