Azure Pipeline映射:CSV导入时为UserName字段设置静态值
Got it, let's figure out how to assign a fixed static value to the UserName field in your target JSON when importing from CSV via Azure Pipeline. Based on your input dataset details, here are two reliable approaches you can implement right away:
1. Use Copy Activity (Simplest for Basic Mapping)
Since your source is an Azure Blob CSV dataset (Input_dat) and target is JSON, the Copy Activity is the quickest way to handle this static value assignment without extra data flow overhead.
Step-by-Step Setup:
- Add a Copy Activity to your pipeline, set the source to your
Input_datdataset, and configure your target JSON dataset. - Navigate to the Mapping tab of the Copy Activity:
- For fields like
ServerName,DTVer, andVer, keep the default 1:1 mapping between source CSV columns and target JSON fields. - For the
UserNamefield:- Click Add > Derived Column (or directly edit the existing
UserNamemapping if it's already listed). - In the derived column configuration:
- Set the Column name to
UserName(matching your target JSON field name). - For the Expression, enter your fixed static value wrapped in single quotes, e.g.,
'your-static-username-here'(this tells Azure Data Factory to treat it as a literal string, not a column reference).
- Set the Column name to
- Ensure this derived column is mapped to the target JSON's
UserNamefield.
- Click Add > Derived Column (or directly edit the existing
- For fields like
Example Mapping JSON Snippet:
If you prefer editing the pipeline JSON directly, here's what the translator section would look like:
"translator": { "type": "TabularTranslator", "mappings": [ { "source": { "name": "ServerName" }, "sink": { "name": "ServerName" } }, { "source": { "name": "DTVer" }, "sink": { "name": "DTVer" } }, { "source": { "name": "Ver" }, "sink": { "name": "Ver" } }, { "source": { "type": "Expression", "value": "'static-user-001'" }, "sink": { "name": "UserName" } } ] }
2. Use Mapping Data Flow (For Flexible, Scalable Transformations)
If you anticipate needing additional data transformations later, Mapping Data Flow is a better fit. It gives you more control over data manipulation:
Step-by-Step Setup:
- Add a Mapping Data Flow activity to your pipeline.
- Inside the data flow:
- Add a Source transformation, connect it to your
Input_datdataset. - Add a Derived Column transformation:
- Create a new column (or overwrite the existing
UserNamecolumn from the source) and set the expression to your static value, e.g.,'fixed-username'.
- Create a new column (or overwrite the existing
- Add a Sink transformation, connect it to your target JSON dataset, and verify all fields (including the static
UserName) are correctly mapped.
- Add a Source transformation, connect it to your
Key Notes:
- Make sure your target JSON dataset's structure includes the
UserNamefield with a String type (matching your source dataset'sUserNametype). - If your source CSV already has a
UserNamecolumn, be sure to overwrite it with the static value in your mapping/derived column step—otherwise, the source value will take precedence. - Always test the pipeline with a small sample CSV to confirm the static value is correctly populated in the target JSON.
内容的提问来源于stack exchange,提问作者user2347519

