前言
函數計算(Function Compute)是一個事件驅動的服務,通過函數計算,用戶無需管理服務器等運行情況,只需編寫代碼並上傳。函數計算準備計算資源,並以彈性伸縮的方式運行用戶代碼,而用戶只需根據實際代碼運行所消耗的資源進行付費。Table Store Stream是用於獲取Table Store表中增量數據的一個數據通道,通過創建Table Store觸發器,能夠實現Table Store Stream和函數計算的自動對接,從而實現OTS數據發生變更時候定製化的自動處理。具體可以查看表格存儲觸發函數計算官方教程,但是該官方教程只有python版的代碼示例,本教程作爲補充,通過代碼示例說明在其他runtime下怎麼玩轉Table Store觸發器。
其他 Runtime 使用的第三庫
- nodejs: node-cbor
- php: CBOR encoder for PHP
- java: jackson
打包第三方庫, 請分別參考:
本文附件中有 nodejs 和 php 完整的代碼示例下載, 而 java 有單獨一篇文章表格存儲觸發 java runtime 的函數計算處理示例教程說明具體操作過程
代碼示例
nodejs
-
目錄結構
-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
-
入口函數
'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
-
目錄結構
-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
-
入口函數
<?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)); }