Creating Javascript User-defined Functions

With SQL Stream Builder, you can create user functions to write powerful functions in JavaScript, that you can use to enhance the functionality of SQL.

User functions can be simple translation functions like Celsius to Fahrenheit, more complex business logic, or even looking up data from external sources. User functions are written in JavaScript. When you write them, you create a library of useful functions.
  1. Navigate to the Streaming SQL Console.
    1. Go to your cluster in Cloudera Manager.
    2. Select SQL Stream Builder from the list of services.
    3. Click SQLStreamBuilder Console.
    The Streaming SQL Console opens in a new window.
  2. Click Create Job or select a previous job on the Getting Started page.
    You are redirected to the Console page. The Materialized View button will be available when you add a query to the SQL Editor.
  3. Select Functions from the main menu.
  4. Click New function.
  5. Add a Name to the UDF.
    For example, name the UDF to HELLO_WORLD.
  6. Add a Description to the UDF.
  7. Select the Output and Input data type.
    For example, select STRING.
  8. Paste the JavaScript code to the editor.
    For example:
    // check to see if the card is VISA
    function HELLO_WORLD(card){
        var cardType = "Other";
        if (card.charAt(0) == 4){
          cardType = "Visa";
        return cardType;
    HELLO_WORLD($p0); // this line must exist
  9. Click Create.
  10. Once created, you can use a User Function in your SQL statement:
    For example:
    -- simple usage
    FROM ev_sample_fraud;
    -- in the predicate
    SELECT amount, card
    FROM ev_sample_fraud
    WHERE HELLO_WORLD(card) = "Visa";