Nodejs/Php/Java Runtime for Table Storage Trigger Function Computing Example

Posted by blui on Wed, 08 May 2019 01:50:03 +0200

Preface

Function Compute is an event-driven service. Through function computing, users do not need to manage the operation of servers, but only write code and upload it. Function computing prepares computing resources and runs user code in a flexible and scalable manner, while users only need to pay for the resources consumed by running the actual code. Table Store Stream is a data channel used to acquire incremental data in Table Store table. By creating Table Store trigger, it can realize automatic docking between Table Store Stream and function calculation, thus realizing customized automatic processing when OTS data changes. Specific can be seen Form Storage Trigger Function Computing Official Tutorial However, the official tutorial only has a python version of the code sample, which complements this tutorial by demonstrating how to play Table Store triggers under other runtime s.

The third library used by other Runtime

Packing third-party libraries, please refer to separately:

The complete code samples of nodejs and php are downloaded in the annex to this article, while java has a separate article Table Storage Triggers java runtime Function Computing Processing Example Tutorial Explain the specific operation process

Code example

nodejs

  • directory structure

    -rw-r--r--  1 songluo  staff   350 Nov 21 16:22 index.js
    drwxr-xr-x  9 songluo  staff   288 Nov 21 15:54 node_modules
    -rw-r--r--  1 songluo  staff  1639 Nov 21 15:54 package-lock.json
    -rw-r--r--  1 songluo  staff    49 Nov 21 15:54 package.json
  • Entry function

    'use strict';
    var cbor = require('cbor');
    
    module.exports.handler = function(event, context, callback) {
        cbor.decodeFirst(event, function(error, obj){
            // error != null if there was an error
            // obj is the unpacked objec
            console.info(JSON.stringify(obj));
            // use obj do anything
        });
        callback(null, 'ok');
    };

php

  • directory structure

    -rw-r--r--  1 songluo  staff   218 Nov 21 16:43 composer.json
    -rw-r--r--  1 songluo  staff  1979 Nov 21 16:43 composer.lock
    -rw-r--r--  1 songluo  staff  1197 Nov 21 17:01 index.php
    drwxr-xr-x  5 songluo  staff   160 Nov 21 16:43 vendor
  • Entry function

    <?php
    require_once __DIR__ . "/vendor/autoload.php";
    
    function myErrorHandler($errno, $errstr, $errfile, $errline) {
        if (!(error_reporting() & $errno)) {
            return false;
        }
    
        switch ($errno) {
        case E_USER_ERROR:
            $errInfo = array(
                "errorMessage" => $errstr,
                "errorType"    => \ServerlessFC\friendly_error_type($errno),
                "stackTrace"   => array(
                    "file" => $errfile,
                    "line" => $errline,
                ),
            );
            throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
            break;
    
        default: // E_USER_WARNING | E_USER_NOTICE
            break;
        }
    
        /* Don't execute PHP internal error handler */
        return true;
    }
    
    // set to the user defined error handler
    // solve 'Non-static method CBOR\\CBOREncoder::bigint_unpack() should not be called statically'
    // https://stackoverflow.com/questions/10768576/how-can-i-solve-non-static-method-xxxxxx-should-not-be-called-statically-in
    set_error_handler("myErrorHandler");
    
    function handler($event, $context){
        //decode
        $decoded_evt = \CBOR\CBOREncoder::decode($event);
        //output
        var_export(json_encode($decoded_evt));
    }

java

Please refer to Table Storage Triggers java runtime Function Computing Processing Example Tutorial

Topics: PHP Java JSON Python