Create Custom Mapping Functions

If creating the mapping manually, the source dataset is not required to be ingested first; however you need to know what the source fields are.

Before you begin

You need:

About this task

In this task, you will create and upload custom mapping functions to use in your mapping definition.

Procedure

  1. Create a valid JavaScript script (.sjs) or module (.mjs) or a valid XQuery script (.xqy).

    The file must be stored in the directory your-project-root/src/main/ml-modules/root/custom-modules/mapping-functions.

    Note:
    • JavaScript scripts (.sjs) and XQuery scripts (.xqy) require MarkLogic Server 9.0-10 and later.
    • Javascript modules (.mjs) require MarkLogic Server 10.0-2 and later.

    The value passed to the function is of type node.

       'use strict';
    
      function removeHyphens(val) {
        return fn.string(val).replace("-", "");
      }
    
      module.exports = {
        removeHyphens
      }
    
       xquery version "1.0-ml";
    
      module namespace custom = "http://marklogic.com/mapping-functions/custom";
    
      declare function remove-hyphens($val as xs:string?) as xs:string?
      {
        fn:replace($val, "-", "")
      };
    
  2. Run the Gradle task hubDeploy or hubDeployAsDeveloper.
    The functions in the scripts or modules will be compiled into XSLT templates that can be used in your mapping.

What to do next

  • If you use Quickstart,
    1. Navigate to the mapping step where you want to use the custom function.
    2. In the step details panel, enter the custom function name or select it from the f(x) dropdown lists.
  • If you edit your flows manually,
    1. In the mapping definition file, use your custom function in the sourcedFrom value of the entity property node.
         {
          "name" : "MyFlow-MyMappingStep",
          ...
          "properties" : {
            "my-entity-property": {
              "sourcedFrom": "MyCustomFunction( my-source-field-X )"
            }
            ...
          }
        }
      
    2. In the flow configuration file, update the name of the mapping definition in your mapping step as needed.
         "steps" : {
          ...
          "2" : {
            "name" : "MyMappingStep",
            ...
            "options" : {
              ...
              "mapping" : {
                "name" : "MyFlow-MyMappingStep",
              },
              ...
            },
            ...
          }
        }