Exercise 7 | Run a Job Synchronously and Upload Data: Transact Data |
Data | C:\FMEData2019\Resources\RESTAPI\Chapter4Exercise7\12656-datapoints.csv |
Overall Goal | To use a single call to upload data and run a job. |
Demonstrates | How to use the Transact Data Call |
Starting Workspace | C:\FMEData2019\Resources\RESTAPI\Chapter4Exercise7\Chapter4Exercise7.Start.fmw | Ending Workspace | C:\FMEData2019\Resources\RESTAPI\Chapter4Exercise7\Chapter4Exercise7.Complete.fmw |
The Transact Data call allows a user to upload data and run a workspace with one call to FME Server. It will run the workspace as it is and write the output to the location specified in the writer. It is good for inserting new data into a database or any job where the user would not need the output returned to them. Once the call is successfully completed FME Server will return a 200 OK message. However, it will not provide feedback on whether the job was completed successfully on FME Server. It is recommended to periodically check the server to ensure your jobs are running successfully on FME Server. Getting the status of jobs can be accomplished through the REST API as well. These calls will be displayed in Exercise 8.
Let's start by creating a workspace that is compatible with the Transact Data call.
1) Open the Chapter4Exercise7.Start.fmw Workspace
Start FME Workbench and open the Chapter4Exercise7.Start.fmw workspace.
This workspace is located:
C:\FMEData2019\Resources\RESTAPI\Chapter4Exercise7\Start.fmw
This is a very simple workspace that reads a CSV File and produces a Shapefile.
However, there is one important change that is required for this translation to work in this call. The source dataset parameter will need to be set to optional. This is because the data will be sent in the body of the POST request to the URL and not by setting the path to the source dataset parameter in the workspace. The source dataset parameter will need to be left unset for this to work, so we need to make it optional to prevent the workspace from throwing an error about this.
2) Set the Source Dataset Published Parameter to Optional
Set the Source Dataset Published Parameter to optional by right-clicking on the published parameters and selecting Edit Definition.
Then select the optional setting in the within the parameter settings.
If you are not using a training machine you may also set the Default Value to navigate your Reader Dataset
C:\FMEData2019\Resources\RESTAPI\Chapter4Exercise7\12656-datapoints.csv
3) Create a folder in your FME Server for your output
First, go to your FME Server and log into the admin account. Using the username admin and the password admin.
On the left-hand side of the page locate the "Resources" and click on it. Next, click on Data. Finally, click on Create Folder and entitle it RESTTraining
After we create the folder, we can find the path to the folder by checking the folder. Then, go to Actions and select Properties.
Select the RESTTraining File Folder then select Actions
Select Properties
Copy the System Path.
4) Update the Shapefile destination to a Shared Resource File in your FME Server
Back in the FME Workbench change the Shapefile to a destination within your FME Server.
To do this right click the DestDataset published parameter, then click Edit Value. Then, insert a file path to your shared resources.
$(FME_SHAREDRESOURCE_DATA)/RESTTraining
Save the workspace before publishing.
5) Upload the Workspace to FME Server
Now, it's time to upload the workspace to your FME Server. This can be done by clicking the Publish to FME Server Button or by selecting File > Publish to FME Server from the menubar.
As this is the first time we've connected to our FME Server, we'll need to create a new connection, so in the Publish to FME Server wizard select Add Web Connection from the drop-down menu.
In the dialog that opens enter the parameters provided by your training instructor. In most cases the parameters will be as follows:
- Connection Name: restapi FME Server
- FME Server URL: http://localhost
- Username: admin
- Password: admin
Click Authenticate to confirm the connection and return to the previous dialog. Make sure the newly defined connection is selected and click Next to continue.
For this exercise, we’ll create a new repository by clicking the New button. When prompted enter the name RESTTraining.
Save the workspace as Chapter4Exercise7.Complete.fmw and select the checkbox to upload data files.
Once we've specified the files to upload we can then click Next to continue the wizard.
In the final screen of the wizard we can register the workspace for use with various services.
Select the Job Submitter service as this is the only service we are using for now.
Finally, click Publish to complete publishing the workspace.
6) Run the Workspace in your FME Server
Now we will go to our FME Server as the admin user to prove this works.
On the left-hand menu, we will see a Run Workspace tab.
Now select the following options:
- Repository: RESTTraining
- Workspace: Chapter4Exercise7.Complete.fmw
- Service: Job Submitter
Now click Run!
7) Check that the Shapefile was successfully produced
Go to Resources > Data > RESTTraining and ensure a Shapefile was produced.
Delete the Shapefile. This is done by selecting all files and then select Actions and selecting delete. We are deleting the Shapefile because we are going to use the REST API to submit this job again.
8) Update the token permissions
Once, a new repository is created it is not automatically shared with other users. This also means it would not automatically be shared with the token we created previously. We need to give our REST token permissions to access our new repository.
Click on the user icon on the top right of FME Server. Then click Manage Tokens.
Click on the token we created earlier in the course. This should be called Rest API Training Course.
Then, find the Repositories tab and find the RESTTraining repository. From here, we can select the Download, Read, and Run permissions.
Then click OK! This will automatically enable the token to run a workspace in the RESTTraining repository.
9) Use Postman to complete the Transact Data Call
Please note that this call requires authorization
Click the plus button to create a new tab. We'll now use the call to transact data to upload a CSV file and run the workspace through the REST API.
The call is:
POST | http://<yourServerHost>/fmerest/v3/transformations/transactdata/RESTTraining/Chapter4Exercise7.Complete.fmw |
---|
If you saved the workspace to a different Repository you will have to modify the call above
In Postman set up the call with the following headers:
- Authorization: fmetoken token=<yourToken>
To enter in the token we can use the preset created previously. In the key-value, type the word token and the preset created will appear.
- Accept: application/json
The file to be uploaded will be sent in the body of the call. Click on the body tab and then the binary button. This will give you an option to upload a file. Select the 12656-datapoints.csv found in the C:/FMEData2019/Resources/RESTAPI/Chapter4/Exercise7. Then run the call by pushing the SEND button.
You should receive a message 200 OK this indicates the call has completed. Check the Jobs page in FME Server to confirm the job was correctly submitted. Also, check the output location to see if the Shapefile was created.
CONGRATULATIONS |
By completing this exercise you have learned how to:
|