简体   繁体   English

Talend:如何在tFileOutputExcel中使用相对路径或如何获取项目目录?

[英]Talend: How to use relative path in tFileOutputExcel or how to get the project dir?

Working with talend, I want to keep the project portable and therefore I need to avoid hardcoded absolute paths. 与talend一起工作时,我想保持项目的可移植性,因此需要避免使用硬编码的绝对路径。 Now I need to export a file as XLSX. 现在,我需要将文件导出为XLSX。

Assume my project resides here on my local hard drive under Windows "D:/MyLongAndFancyPathToProjects/MyCurrentProject/talend.project" 假设我的项目位于Windows "D:/MyLongAndFancyPathToProjects/MyCurrentProject/talend.project"下的本地硬盘上。

I the tFileOutputExcel component at "File Name" I started with "D:/MyLongAndFancyPathToProjects/MyCurrentProject/MyOutput/test.xlsx" which works great - but is exactly what I don't want. 我在“文件名”处的tFileOutputExcel组件中以"D:/MyLongAndFancyPathToProjects/MyCurrentProject/MyOutput/test.xlsx" ,这很好用-但这正是我所不想要的。

Then I tried 然后我尝试

  • "MyCurrentProject/MyOutput/test.xlsx"
  • "/MyCurrentProject/MyOutput/test.xlsx"

Somewhere those files where create, but where? 这些文件在哪里创建,但是在哪里? Wasn't able to find them... 找不到他们...

They where NOT created in the project directory: "D:/MyLongAndFancyPathToProjects/MyCurrentProject/" 它们"D:/MyLongAndFancyPathToProjects/MyCurrentProject/"项目目录中创建: "D:/MyLongAndFancyPathToProjects/MyCurrentProject/"

How can I get the current project in Talend? 我如何在塔伦德获得当前项目? If possible, I want to avoid configuration settings. 如果可能,我要避免配置设置。 This should work dynamically somehow. 这应该以某种方式动态地工作。

I found the interested phenomenan that use simple relative directory statement "./" . 我发现使用简单的相对目录语句“ ./”的有趣现象。

First, I used "tCreateTemporaryFile" with setting the directory "./test". 首先,我将“ tCreateTemporaryFile”用于设置目录“ ./test”。 When I execute in TOS, "test" directory was created in "C:/TOS_DI-Win32-r****-V5.3.1/test". 在TOS中执行时,在“ C:/ TOS_DI-Win32-r ****-V5.3.1 / test”中创建了“测试”目录。

Next, I exported this job(name "myjob" ) to my Desktop and execute via batch, "test" directory was created in "Desktop/myjob_0.1/myjob/test". 接下来,我将此作业(名称为“ myjob”)导出到我的桌面并通过批处理执行,在“ Desktop / myjob_0.1 / myjob / test”中创建了“ test”目录。

Third, I resset the directory "../test" and expoted and executed at Desktop. 第三,我重置目录“ ../test”,并在桌面上对其进行扩展和执行。 The "test" directory was created in "Desktop/myjob_0.1/test". 在“桌面/myjob_0.1/测试”中创建了“测试”目录。

We may be able to use simple relative directory "./" as current and "../" as parent. 我们也许可以使用简单的相对目录“ ./”作为当前目录,并使用“ ../”作为父目录。

I know you want it to work dynamically but your first step should be to create a context variable with the path to the folder containing your Excel file. 我知道您希望它动态地工作,但是第一步应该是创建一个上下文变量,并使用包含您的Excel文件的文件夹的路径。

This can then be provided at run time or given a default value that works for you. 然后可以在运行时提供此参数,或者为您提供适合您的默认值。

Once you have that working, the next trick is to try and get the path for where your job is executed from and use that if no path has been explicitly provided by context variable. 完成工作后,下一个技巧是尝试从中获取执行作业的路径,并在上下文变量未明确提供任何路径的情况下使用该路径。

I've not got access to a Studio client right now to test this but it should be possible to use the tSystem component to get the current working directory. 我现在无法访问Studio客户端以对此进行测试,但是应该可以使用tSystem组件来获取当前的工作目录。 So in a Linux based OS you could run pwd from a tSystem component to get the folder path. 因此,在基于Linux的操作系统中,您可以从tSystem组件运行pwd以获取文件夹路径。

This should then be set as the folder path context variable if the provided one is null or blank. 如果提供的值为null或空白,则应将其设置为文件夹路径上下文变量。

Your File Structure Will be created in your talend installation directory For me it is C:\\Telend\\TOS_DI-Win32-20141207_1530-V5.6.1 now you have to get this path and store it in variable using globalMap.put("HOME_DIR", System.getProperty("user.dir")); 您的文件结构将在您的talend安装目录中创建。对我来说,它是C:\\ Telend \\ TOS_DI-Win32-20141207_1530-V5.6.1,现在您必须获取此路径并将其存储在使用globalMap.put(“ HOME_DIR” System.getProperty( “user.dir来”));
and now when ever try to give path to any file just add this globalMap before any file name ie : globalMap.get("HOME_DIR").toString()+"/config.csv" 现在,当尝试提供任何文件的路径时,只需将此globalMap添加到任何文件名之前,即:globalMap.get(“ HOME_DIR”)。toString()+“ / config.csv”

and at last when you export job you have to put the same file near you exiqutable bat file so that will solve your problem :) 最后,当您导出作业时,必须将同一个文件放在可交换的bat文件附近,这样才能解决您的问题:)

by doing this you can give dynamic path to your file 通过这样做,您可以为文件提供动态路径

Here is a solution that I found. 这是我找到的解决方案。 Note that the project path is only available when running from Talend Open Studio, not when running as a standalone app. 请注意,项目路径仅在从Talend Open Studio运行时可用,而在作为独立应用程序运行时不可用。

Add this Talend helper class in your routines: 在您的例程中添加此Talend帮助器类:

package routines;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;

public class Talend {
    /**
     * @param projectName The project name, available as `projectName` in Talend components
     * @return the project path when run from Talend Open Studio or null when run as a standalone executable
     */
    public static Path projectPath(String projectName) {
        String[] classPathElements = System.getProperty("java.class.path", "").split(";");
        Stream<Path> classPaths = Arrays.stream(classPathElements).map(path -> Paths.get(path));
        Optional<Path> classesPath = classPaths.filter(path -> path.endsWith(Paths.get(".Java", "target", "classes"))).findAny();
        if (classesPath.isPresent()) {
            Path workspacePath = classesPath.get().getParent().getParent().getParent();
            Path projectPath = workspacePath.resolve(projectName);
            if (Files.isDirectory(projectPath)) {
                return projectPath;
            }
        }
        return null;
    }
}

Then in a tJava component at the beginning of your job you can save the project path in the global map for example: 然后,在工作开始时在tJava组件中,可以将项目路径保存在全局映射中,例如:

java.nio.file.Path projectPath = Talend.projectPath(projectName);
if (projectPath != null) {
    globalMap.put("projectPath", projectPath.toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM