Transform your data

Let's add transformations to your data before sending it to a Splunk Enterprise index.

In this section, you change the value of a field to provide more meaningful information, extract interesting nested fields into top-level fields, and redact credit card information from the data before sending it off to a Splunk Enterprise index for indexing.

  1. Change the source_type of your data events from Unknown to purchases.
    1. In the DSP UI, on the pipeline canvas, click the + icon between the Splunk DSP Firehose and Send to Splunk HTTP Event Collector functions, and select Eval from the function picker.
    2. On the View Configurations tab, enter the following SPL2 expression in the function field:
      CODE
      source_type="purchases"
    3. Click the More Options (More options button) button located beside the Activate Pipeline button, and select Validate to check that all functions in your pipeline are configured properly.
  2. To see how this function transforms your data, send your sample data to your pipeline again and preview the data that comes out from the Eval function.
    1. On the pipeline canvas, select the Eval function in your pipeline and then click the Start Preview button (Start preview button) to restart your preview session.
    2. Wait a few seconds before running the following command in the Splunk Cloud Services CLI. Because your pipeline isn't activated, resending your sample data doesn't result in data duplication.
      CODE
      cat examples/demodata.txt | while read line; do echo $line | ./scloud ingest post-events --host Buttercup --source syslog --sourcetype Unknown; done
    3. Back in DSP, view the data on the Preview Results tab and confirm that the value of the source_type field is now purchases.
  3. In the data preview, you'll notice there are several interesting fields in the body field including the type of card used, the purchase amount, and the sale date. Extract some of these nested fields into the attributes field.
    1. On the pipeline canvas, select the Eval function.
    2. On the View Configurations tab, click +Add and then enter the following SPL2 expression in the newly added field. This SPL2 expression casts the body data to the string data type, extracts key-value pairs from those strings using regular expressions, and then inserts the extracted key-value pairs into the attributes field. The body data must be cast to a different data type first because by default it is considered to be a union of all DSP data types, and the extract_regex function only accepts string data as its input.
      CODE
      attributes=extract_regex(cast(body, "string"), /(?<tid>[A-Z0-9]+?),(?<cid>[A-Z0-9]+?),(?<Type>[\w]+\s\w+),(?<Amount>[\S]+),(?<sdate>[\S]+)\s(?<stime>[\S]+),(?<edate>[\S]+)\s(?<etime>[\S]+?),(?<Longitude>[\S]+?),(?<Latitude>[\S]+?),(?<Card>[\d]*)/)
  4. Now that you've extracted some of your nested fields into the attributes field, take it one step further and promote these attributes as top-level fields in your data.
    1. On the pipeline canvas, click the + icon between the Eval and Send to Splunk HTTP Event Collector functions, and then select Eval from the function picker.
    2. On the View Configurations tab, enter the following SPL2 expression in the function field for the newly added Eval function. These expressions turn the key-value pairs in the attributes field into top-level fields so that you can easily see the fields that you've extracted.
      CODE
      Transaction_ID=map_get(attributes, "tid"),
      Customer_ID=map_get(attributes, "cid"),
      Type=map_get(attributes, "Type"),
      Amount=map_get(attributes, "Amount"),
      Start_Date=map_get(attributes, "sdate"),
      Start_Time=map_get(attributes, "stime"),
      End_Date=map_get(attributes, "edate"),
      End_Time=map_get(attributes, "etime"),
      Longitude=map_get(attributes, "Longitude"),
      Latitude=map_get(attributes, "Latitude"),
      Credit_Card=map_get(attributes, "Card")
  5. Notice that your data contains the credit card number used to make a purchase. Redact that information before sending it to your index.
    1. On the pipeline canvas, click the + icon between the second Eval function and the Send to Splunk HTTP Event Collector function, and then select Eval from the function picker.
    2. On the View Configurations tab, enter the following SPL2 expression in the function field for the newly added Eval function. This SPL2 expression uses a regular expression pattern to detect credit card numbers and replace the numbers with <redacted>.
      CODE
      Credit_Card=replace(cast(Credit_Card, "string"), /\d{15,16}/, "<redacted>")
  6. The original body of the event had sensitive credit card information, let's also redact that information from the body field.
    1. On the pipeline canvas, select the Eval function that you added during the previous step.
    2. On the View Configurations tab, click +Add and then enter the following SPL2 expression in the newly added field. This SPL2 expression casts the body data to the string data type, detects credit card numbers in those strings using a regular expression pattern, and then replaces the numbers with <redacted>. The body data must be cast to a different data type first because by default it is considered to be a union of all DSP data types, and the replace function only accepts string data as its input.
      CODE
      body=replace(cast(body,"string"),/[1-5][0-9]{15}/,"<redacted>")
  7. The attributes field also contains sensitive credit card information, so let's also redact the information there.
    1. On the pipeline canvas, select the Eval function that you modified during the previous step.
    2. On the View Configurations tab, click +Add and then enter the following SPL2 expression in the newly added field:
      CODE
      attributes=map_set(attributes, "Card", replace(ucast(map_get(attributes, "Card"), "string", null), /[1-5][0-9]{15}/, "<redacted>"))

      This SPL2 expression does the following:

      • Gets the Card value from the attributes map, and then casts that value to the string data type.

      • Detects credit card numbers in the Card value using a regular expression pattern, and then replaces the numbers with <redacted>.

      • Converts the Card value from the string data type back to the map data type.

      • The Card value must be converted between the string and map data types because attributes contains map data, and the replace function only accepts string data as its input.

  8. Now that you've constructed a full pipeline, preview your data again to see what your transformed data looks like.
    1. Click the More Options button (More options button) located beside the Activate Pipeline button, and select Validate to check that all functions in your pipeline are configured properly.
    2. Select the last Eval function in the pipeline, and then click the Start Preview button (Start preview button) to restart your preview session.
    3. Wait a few seconds, and then run this command again in the Splunk Cloud Services CLI. Because your pipeline isn't activated yet, resending your sample data will not result in data duplication.
    4. Back in DSP, view the data on the Preview Results tab and confirm the following:
      • The value in the source_type field is purchases.

      • The attributes field contains information such as the type of card used, the purchase amount, and the sale date

      • The body and attributes fields do not show any credit card numbers.

  9. To save these transformations to your pipeline, click Save.

After following these steps, you have a DSP pipeline that resembles the following: