How to return byte array output from the script into the pipeline

KanikaAg15KanikaAg15 Member Posts: 19 Contributor I
Hi,
I have implemented a script for decoding an xlsm file but somehow I am facing issue in using the decoded data into the next operator within a pipeline.
 I have used macros to store output but it has capability of storing data in the string format. 
It would be great if some alternative can be suggested. 

Best Answer

  • MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,503 RM Data Scientist
    Solution Accepted
    Hi,
    I think you want to use something like this:
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import org.apache.commons.codec.binary.Base64;
    import com.rapidminer.operator.Operator;
    import com.rapidminer.operator.OperatorDescription;
    import com.rapidminer.operator.nio.file.SimpleFileObject;

    String originalString = "Just a text";
    byte[] decodedBytes = Base64.decodeBase64(originalString);
    File tempFile = null;
    try {
    tempFile = File.createTempFile("rm-", ".tempFile");
    tempFile.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(tempFile);
    fos.write(decodedBytes);
    fos.close();

    } catch (IOException e) {
    throw new RuntimeException(e);
    }
    SimpleFileObject fileObject = new SimpleFileObject(tempFile);
    return fileObject

    This give you a purple fileObject which can be used with any other Read operator.

    Best,
    Martin

    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany

Answers

  • MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,503 RM Data Scientist
    Hi,
    not sure what "decoded" is, but this is surely a tough one to do with operators. Why can't you just use Read Excel?

    If this is a commercial request, please consult support.rapidminer.com so we can assign more resources.

    Best,
    Martin
    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany
  • KanikaAg15KanikaAg15 Member Posts: 19 Contributor I
    @MartinLiebig
    thanks fore revert. we cannot raise any commerical request. My only limitation is the I have a data within byte array which I need as output from the execute script operator. If you can suggest a method around that would be a great help. 
  • MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,503 RM Data Scientist
    What should the output be? a purple file object you can push into Read Excel or so?
    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany
  • KanikaAg15KanikaAg15 Member Posts: 19 Contributor I
    edited February 2023
    @MartinLiebig I want to use the output as normal exampleset so that the attributes values can be further utilized in the further pipeline.
  • MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,503 RM Data Scientist
    Can you share an example?
    - Sr. Director Data Solutions, Altair RapidMiner -
    Dortmund, Germany
  • KanikaAg15KanikaAg15 Member Posts: 19 Contributor I
    @MartinLiebig
    Please find the below script. The output that is getting stored in byte array i.e decodedByte. I need that as an output from execute operator and dont want to store the file in local repository.
    import java.util.Base64;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class ExcelFromBase64 {
    public static void writeExcelFromBase64(String base64) throws IOException {
    byte[] decodedBytes = Base64.getDecoder().decode(base64);
    FileOutputStream fos = new FileOutputStream("%{Path}");
    fos.write(decodedBytes);
    fos.close();
    }

    public static void main(String[] args) throws IOException {
    String base64 = "%{base64}";
    writeExcelFromBase64(base64);
    }
    }
  • KanikaAg15KanikaAg15 Member Posts: 19 Contributor I
    Hi Martin, I cannot thank you enough. This really helped. A big thanks for your help.
Sign In or Register to comment.