Skip to content

Commit

Permalink
Changed logic for retrieving mappings from S3 file
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Boinapalli <[email protected]>
  • Loading branch information
vishalboin committed Jul 27, 2023
1 parent bd3163b commit 419d14d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

import java.io.File;
import java.util.List;

public class FileParameterConfig {
Expand All @@ -35,11 +33,9 @@ public List<MappingsParameterConfig> getFileMappings() {

if (this.awsConfig != null) {
return handler.getS3FileMappings(this.awsConfig, this.fileName);
} else {
File localFile = new File(this.fileName);
return handler.getFileMappings(localFile);
} else{
return handler.getMappingsFromFilePath(this.fileName);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

package org.opensearch.dataprepper.plugins.processor.translate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -68,22 +70,18 @@ public List<MappingsParameterConfig> getS3FileMappings(S3ObjectConfig awsConfig,
S3Object s3Object = s3Client.getObject(bucketName, key);
S3ObjectInputStream inputStream = s3Object.getObjectContent();

// Creating a temporary file
File tempFile = File.createTempFile("temp", null);

FileOutputStream outputStream = new FileOutputStream(tempFile);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] read_buf = new byte[1024];
int read_len = 0;
while ((read_len = inputStream.read(read_buf)) > 0) {
outputStream.write(read_buf, 0, read_len);
byteArrayOutputStream.write(read_buf, 0, read_len);
}
inputStream.close();
outputStream.close();

// Extracting mappings from the temporary file.
s3FileMappings = getFileMappings(tempFile);
byte[] fileData = byteArrayOutputStream.toByteArray();
s3FileMappings = getMappingsFromByteArray(fileData);

tempFile.delete();
byteArrayOutputStream.close();
} catch (IOException | AmazonServiceException e) {
LOG.error("Error while retrieving mappings from S3 Object");
e.printStackTrace();
Expand All @@ -96,7 +94,18 @@ public List<MappingsParameterConfig> getS3FileMappings(S3ObjectConfig awsConfig,
return s3FileMappings;
}

public List<MappingsParameterConfig> getFileMappings(File file){
public List<MappingsParameterConfig> getMappingsFromFilePath(String fileName){
try{
Path filePath = Paths.get(fileName);
byte[] fileData = Files.readAllBytes(filePath);
return getMappingsFromByteArray(fileData);
}catch (IOException ex){
LOG.error("Unable to parse the mappings from file", ex);
return null;
}
}

private List<MappingsParameterConfig> getMappingsFromByteArray(byte[] file){
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
FileMappingsRef fileParser = mapper.readValue(file, FileMappingsRef.class);
Expand Down

0 comments on commit 419d14d

Please sign in to comment.