U-SQL技术问询:获取输入文件名用于输出及相关需求的解决办法
Got it, let's break down how to solve both your issues—since direct variable interpolation in the OUTPUT statement isn't supported, we'll work around it with practical component and DataWeave logic:
1. Generate the dd-mm-yy-test.txt output filename
Since you can't use {filename} directly in the OUTPUT path, we'll build the full output path dynamically before writing the file:
Step 1: Capture the source filename and build the output path
Add aSet Variablecomponent to your flow, name itdynamicOutputPath, and use this DataWeave expression to construct the path with the required date format and source filename:%dw 2.0 output application/java --- "/output/" ++ (now() as String {format: "dd-MM-yy"}) ++ "-" ++ (attributes.filename replace ".csv" with ".txt")attributes.filenamepulls the original input filename (make sure your input connector, like File Read, has metadata enabled—this is usually on by default, but double-check settings to ensure file attributes are passed through).- The
replacefunction swaps the.csvextension to.txtfor the output file.
Step 2: Use the dynamic path in your write operation
Instead of hardcoding the path in theOUTPUTstatement, use a File ConnectorWriteoperation. Set the "File Path" field tovars.dynamicOutputPath—this will use the dynamically generated path we created in the previous step.
2. Add the current filename to your extracted output content
If you want to include the source filename within the text content of your output file, adjust your DataWeave transformation to inject it. For example:
%dw 2.0 output text/plain outputHeader=false quoting=false --- // Example: Prepend the source filename to the output content "Processed from file: " ++ attributes.filename ++ "\n\n" ++ payload joinBy "\n"
This will add a line at the top of your output file stating the source CSV filename, followed by the extracted content from the CSV. If you need to embed the filename elsewhere (like in each line), just adjust the DataWeave logic to fit your use case.
Quick check: If attributes.filename isn't returning the correct value, verify that your input connector (e.g., File Read) is configured to pass metadata. Most file connectors have an option to enable attribute propagation—enable that to access file-specific details like name, size, and creation date.
内容的提问来源于stack exchange,提问作者user2956121

