- Notifications
You must be signed in to change notification settings - Fork2
License
forcedotcom/datacloud-customcode-python-sdk
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This package provides a development kit for creating custom data transformations inData Cloud. It allows you to write your own data processing logic in Python while leveraging Data Cloud's infrastructure for data access and running data transformations, mapping execution into Data Cloud data structures likeData Model Objects andData Lake Objects.
More specifically, this codebase gives you ability to test code locally before pushing to Data Cloud's remote execution engine, greatly reducing how long it takes to develop.
Use of this project with Salesforce is subject to theTERMS OF USE
- Python 3.11 only (currently supported version - if your system version is different, we recommend usingpyenv to configure 3.11)
- Azul Zulu OpenJDK 17.x
- Docker support likeDocker Desktop
- A salesforce org with some DLOs or DMOs with data and this feature enabled (it is not GA)
- Aconnected app
The SDK can be downloaded directly from PyPI withpip:
pip install salesforce-data-customcodeYou can verify it was properly installed via CLI:
datacustomcode versionEnsure you have all theprerequisites prepared on your machine.
To get started, create a directory and initialize a new project with the CLI:
mkdir datacloud&&cd datacloudpython3.11 -m venv .venvsource .venv/bin/activatepip install salesforce-data-customcodedatacustomcode init my_package
This will yield all necessary files to get started:
.├── Dockerfile├── README.md├── requirements.txt├── requirements-dev.txt├── payload│ ├── config.json│ ├── entrypoint.py├── jupyterlab.sh└── requirements.txtDockerfile(Do not update) – Development container emulating the remote execution environment.requirements-dev.txt(Do not update) – These are the dependencies for the development environment.jupyterlab.sh(Do not update) – Helper script for setting up Jupyter.requirements.txt– Here you define the requirements that you will need for your script.payload– This folder will be compressed and deployed to the remote execution environment.config.json– This config defines permissions on the back and can be generated programmatically withscanCLI method.entrypoint.py– The script that defines the data transformation logic.
A functional entrypoint.py is provided so you can run once you've configured your connected app:
cd my_packagedatacustomcode configuredatacustomcode run ./payload/entrypoint.pyImportant
The example entrypoint.py requires aAccount_Home__dll DLO to be present. And in order to deploy the script (next step), the output DLO (which isAccount_Home_copy__dll in the example entrypoint.py) also needs to exist and be in the same dataspace asAccount_Home__dll.
After modifying theentrypoint.py as needed, using any dependencies you add in the.venv virtual environment, you can run this script in Data Cloud:
To Add New Dependencies:
- Make sure your virtual environment is activated
- Add dependencies to
requirements.txt - Run
pip install -r requirements.txt - The SDK automatically packages all dependencies when you run
datacustomcode zip
datacustomcode scan ./payload/entrypoint.pydatacustomcode deploy --path ./payload --name my_custom_script --cpu-size CPU_L
Tip
Thedeploy process can take several minutes. If you'd like more feedback on the underlying process, you can add--debug to the command likedatacustomcode --debug deploy --path ./payload --name my_custom_script
Note
CPU Size: Choose the appropriate CPU/Compute Size based on your workload requirements:
- CPU_L / CPU_XL / CPU_2XL / CPU_4XL: Large, X-Large, 2X-Large and 4X-Large CPU instances for data processing
- Default is
CPU_2XLwhich provides a good balance of performance and cost for most use cases
You can now use the Salesforce Data Cloud UI to find the created Data Transform and use theRun Now button to run it.Once the Data Transform run is successful, check the DLO your script is writing to and verify the correct records were added.
The SDK automatically handles all dependency packaging for Data Cloud deployment. Here's how it works:
- Add dependencies to
requirements.txt- List any Python packages your script needs - Install locally - Use
pip install -r requirements.txtin your virtual environment - Automatic packaging - When you run
datacustomcode zip, the SDK automatically:- Packages all dependencies from
requirements.txt - Uses the correct platform and architecture for Data Cloud
- Packages all dependencies from
No need to worry about platform compatibility - the SDK handles this automatically through the Docker-based packaging process.
.├── payload│ ├── config.json│ ├── entrypoint.py├── files│ ├── data.csvYour Python dependencies can be packaged as .py files, .zip archives (containing multiple .py files or a Python package structure), or .egg files.
.├── payload│ ├── config.json│ ├── entrypoint.py├── py-files│ ├── moduleA│ │ ├── __init__.py│ │ ├── moduleA.pyYour entry point script will define logic using theClient object which wraps data access layers.
You should only need the following methods:
find_file_path(file_name)- Returns a file pathread_dlo(name)– Read from a Data Lake Object by nameread_dmo(name)– Read from a Data Model Object by namewrite_to_dlo(name, spark_dataframe, write_mode)– Write to a Data Model Object by name with a Spark dataframewrite_to_dmo(name, spark_dataframe, write_mode)– Write to a Data Lake Object by name with a Spark dataframe
For example:
from datacustomcode import Clientclient = Client()sdf = client.read_dlo('my_DLO')# some transformations# ...client.write_to_dlo('output_DLO')Warning
Currently we only support reading from DMOs and writing to DMOs or reading from DLOs and writing to DLOs, but they cannot mix.
The Data Cloud Custom Code SDK provides a command-line interface (CLI) with the following commands:
--debug: Enable debug-level logging
Display the current version of the package.
Configure credentials for connecting to Data Cloud.
Options:
--profile TEXT: Credential profile name (default: "default")--username TEXT: Salesforce username--password TEXT: Salesforce password--client-id TEXT: Connected App Client ID--client-secret TEXT: Connected App Client Secret--login-url TEXT: Salesforce login URL--dataspace TEXT: Dataspace name (optional, for non-default dataspaces)
Initialize a new development environment with a template.
Argument:
DIRECTORY: Directory to create project in (default: ".")
Scan a Python file to generate a Data Cloud configuration.
Argument:
FILENAME: Python file to scan
Options:
--config TEXT: Path to save the configuration file (default: same directory as FILENAME)--dry-run: Preview the configuration without saving to a file
Run an entrypoint file locally for testing.
Argument:
ENTRYPOINT: Path to entrypoint Python file
Options:
--config-file TEXT: Path to configuration file--dependencies TEXT: Additional dependencies (can be specified multiple times)--profile TEXT: Credential profile name (default: "default")
Zip a transformation job in preparation to upload to Data Cloud.
Options:
--path TEXT: Path to the code directory (default: ".")--network TEXT: docker network (default: "default")
Deploy a transformation job to Data Cloud.
Options:
--profile TEXT: Credential profile name (default: "default")--path TEXT: Path to the code directory (default: ".")--name TEXT: Name of the transformation job [required]--version TEXT: Version of the transformation job (default: "0.0.1")--description TEXT: Description of the transformation job (default: "")--network TEXT: docker network (default: "default")--cpu-size TEXT: CPU size for the deployment (default: "CPU_XL"). Available options: CPU_L(Large), CPU_XL(Extra Large), CPU_2XL(2X Large), CPU_4XL(4X Large)
The SDK provides Docker-based development options that allow you to test your code in an environment that closely resembles Data Cloud's execution environment.
When you initialize a project withdatacustomcode init my_package, aDockerfile is created automatically. This Dockerfile:
- Isn't used during local development with virtual environments
- Becomes active during packaging when you run
datacustomcode zipordeploy - Ensures compatibility by using the same base image as Data Cloud
- Handles dependencies automatically regardless of platform differences
Within yourinited package, you will find a.devcontainer folder which allows you to run a docker container while developing inside of it.
Read more about Dev Containers here:https://code.visualstudio.com/docs/devcontainers/containers.
- Install the VS Code extension "Dev Containers" by microsoft.com.
- Open your package folder in VS Code, ensuring that the
.devcontainerfolder isat the root of the File Explorer - Bring up the Command Palette (on mac: Cmd + Shift + P), and select "DevContainers: Rebuild and Reopen in Container"
- Allow the docker image to be built, then you're ready to develop
Once inside the Dev Container:
- Terminal access: Open a terminal within the container
- Run your code: Execute
datacustomcode run ./payload/entrypoint.py - Environment consistency: Your code will run inside a docker container that more closely resembles Data Cloud compute than your machine
Tip
IDE Configuration: UseCMD+Shift+P (orCtrl+Shift+P on Windows/Linux), then select "Python: Select Interpreter" to configure the correct Python Interpreter
Important
Dev Containers get their own tmp file storage, so you'll need to re-rundatacustomcode configure every time you "Rebuild and Reopen in Container".
Within yourinited package, you will find ajupyterlab.sh file that can open a jupyter notebook for you. Jupyter notebooks, incombination with Data Cloud'sQuery EditorandData Explorer, can be extremely helpful for dataexploration. Instead of running an entire script, one can run one code cell at a time as they discover and experiment with the DLO or DMO data.
You can read more about Jupyter Notebooks here:https://jupyter.org/
- Within the root project of your package folder, run
./jupyterlab.sh start - Double-click on "account.ipynb" file, which provides a starting point for a notebook
- Use shift+enter to execute each cell within the notebook. Add/edit/delete cells of code as needed for your data exploration.
- Don't forget to run
./jupyterlab.sh stopto stop the docker container
Important
JupyterLab uses its own tmp file storage, so you'll need to re-rundatacustomcode configure each time you./jupyterlab.sh start.
- Log in to salesforce as an admin. In the top right corner, click on the gear icon and go to
Setup - In the left hand side, search for "App Manager" and select the
App ManagerunderneathApps - Click on
New Connected Appin the upper right - Fill in the required fields within the
Basic Informationsection - Under the
API (Enable OAuth Settings)section:- Click on the checkbox to Enable OAuth Settings.
- Provide a callback URL likehttp://localhost:55555/callback
- In the Selected OAuth Scopes, make sure that
refresh_token,api,cdp_query_api,cdp_profile_apiis selected. - Click on Save to save the connected app
- From the detail page that opens up afterwards, click the "Manage Consumer Details" button to find your client id and client secret
- Go back to
Setup, thenOAuth and OpenID Connect Settings, and enable the "Allow OAuth Username-Password Flows" option
You now have all fields necessary for thedatacustomcode configure command.
If you're working with a non-default dataspace in Salesforce Data Cloud, you can specify the dataspace during configuration:
datacustomcode configure --dataspace my-dataspace
For default dataspaces, you can omit the--dataspace parameter entirely - the SDK will connect to the default dataspace automatically.
About
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.