@1533-systems/driveline 中文文档教程

发布于 5年前 浏览 34 项目主页 更新于 3年前

Driveline Javascript SDK

Javascript SDK 的核心是类 DrivelineClient

DrivelineClient.connect('ws://127.0.0.1:8080', {onConnected:main});

const main = (client) => {

  // list all streams
  // name is null when all streams have been listed
  client.listStreams('*', (name)=> console.log(name));

  // list all keys in the Key-Value store
  // name is null when all keys have been listed
  client.listKeys('*', (name) => console.log(name));

  // load a key from the key-value store
  client.load('kv[1]').then(({recordId, record, error}) => {
    if (error) {
        alert('something bad happened: ' + error);
        return;
    }
    // go on with recordId and record
    console.log('loaded record with id:', recordId, 'value:', record);
  });

  // remove all records in the key-value store which keys matching 'kv[...]'
  // this will watch kv[1], kv[12321312], kv[say whaaaat?]
  client.removeMatches('kv[*]');

  // store an object in the key-value store at key 'kv[2]'
  client.store('kv[2]', {message:'hello, world!'});

  // run a live query against a stream.
  // in this case we want every records
  client.continuousQuery('SELECT * FROM STREAM stream1', ({ recordId, record, error}) => {
      // process ... 
  });

};

DQL Driveline Query Language

Query syntax

Basic syntax

SELECT <selector> FROM STREAM <stream> [WHERE <expression>]
SELECT * FROM STREAM stream_1
SELECT * FROM STREAM stream_1 WHERE key=value
SELECT time AS t,(2+3) AS five FROM STREAM stream_1 WHERE age BETWEEN 21 AND 25 OR name LIKE 'Joe%'

DQL 支持标准的 SQL 查询语法,不包括 Joins 和 聚合体。 这意味着 DQL 可用于所有形式的数据过滤 以及实时流上的数据分区。

KV query

SELECT <selector> FROM <string-key-name-expression> [WHERE <expression>]
SELECT * FROM 'users/*' WHERE color='red'

多键查询是一个 DQL 扩展,可让您订阅多个 事件流,在新流形成时自动订阅, 基于流名称表达式。 流名称表达式使用文件系统/Python Glob, 以?***作为通配匹配字符。

ECMAScript Object Notation (JavaScript extensions)

SELECT {time,name:user.name,phone_number:user.phone.mobile.number,original:{...*}} FROM stream

使用以下形式的输入:

{time:123, user: {name:'joe', phone: {mobile: {number:'1-800-123-4567'}}}}

结果:

{time:123,name:'joe',phone_number:'1-800-123-4567',original:{time:123,user:phone:{...}}}

Operators

下表按以下顺序总结了所有语言运算符 优先权

NameDescriptionExampleAdditional details
ORextended logical ORSELECT * FROM STREAM stream WHERE a OR bif (a) return a; else return b;
ANDlogical ANDSELECT * FROM STREAM stream WHERE a AND bif (a && b) return true; else return false;
NOTlogical NOTSELECT * FROM STREAM stream WHERE NOT aif (a) return false; else return true;
IS [NOT] NULLNull checkSELECT * FROM STREAM stream WHERE a IS NOT NULLif (null===a) return true; return false;
INSet lookupSELECT * FROM STREAM stream WHERE a IN (1,2,3)All values in paranthesis must be constants
BETWEENCompare rangeSELECT * FROM STREAM stream WHERE a BETWEEN b AND cif (b<c) return (a>=b && a<=c); else return (a>=c && a<=b);
= >= <= != > < !< !> <>CompareSELECT * FROM STREAM STREAM WHERE a <> b
LIKEPattern matchSELECT * FROM STREAM stream WHERE a like '%b%'_ stands for single, % stands for multi-char match
+ -Unary plus/minusSELECT * FROM STREAM stream WHERE -5 < +5
+ -AdditionSELECT * FROM STREAM stream WHERE 1+2=3
* / %MultiplicativeSELECT * FROM STREAM stream WHERE 3%2=1
( exp )ParanthesisSELECT * FROM STREAM stream WHERE (1+2)*3=9
true false nullConstantSELECT * FROM STREAM stream WHERE true != false
-123.45e-1Numeric constant
'hello'String constantSELECT * FROM stream WHERE name='joe'
nameIdentifierSELECT name FROM STREAM stream
`user name`IdentifierSELECT `user name` FROM STREAM stream(backticks) Allows using identifier names that are otherwise invalid, e.g., contain invalid characters or symbols

Built-in functions

NameDescriptionExample
ABSAbsolute value float=>floatSELECT ABS(-5) AS num FROM stream {num:5}
CEILRounded up value float=>floatSELECT CEIL(4.5) AS num FROM stream {num:5}
FLOORRounded down value float=>floatSELECT FLOOR(4.5) AS num FROM stream {num:4}
EXPNatural exponent float=>floatSELECT EXP(1) AS num FROM stream {num:2.718281828459045}
LNNatural logarithm float=>floatSELECT LN(2) AS num FROM stream {num:0.6931471805599453}
SQRTSquare root float=>floatSELECT SQRT(9) AS num FROM stream {num:3}
HASHHash function any=>uint64SELECT HASH('abc') AS num FROM stream {num:4952883123889572249}
CHAR_LENGTHLength of string string->int32SELECT CHAR_LENGTH('abc') AS num FROM stream {num:3}
POSITIONIndex of substring in string string,string->int32SELECT POSITION('bc' IN 'abc') AS num FROM stream {num:2}
LOCATEIndex of substring in string string,string->int32SELECT LOCATE('bc', 'abc') AS num FROM stream {num:2}

Driveline Javascript SDK

At the core of the Javascript SDK is the class DrivelineClient.

DrivelineClient.connect('ws://127.0.0.1:8080', {onConnected:main});

const main = (client) => {

  // list all streams
  // name is null when all streams have been listed
  client.listStreams('*', (name)=> console.log(name));

  // list all keys in the Key-Value store
  // name is null when all keys have been listed
  client.listKeys('*', (name) => console.log(name));

  // load a key from the key-value store
  client.load('kv[1]').then(({recordId, record, error}) => {
    if (error) {
        alert('something bad happened: ' + error);
        return;
    }
    // go on with recordId and record
    console.log('loaded record with id:', recordId, 'value:', record);
  });

  // remove all records in the key-value store which keys matching 'kv[...]'
  // this will watch kv[1], kv[12321312], kv[say whaaaat?]
  client.removeMatches('kv[*]');

  // store an object in the key-value store at key 'kv[2]'
  client.store('kv[2]', {message:'hello, world!'});

  // run a live query against a stream.
  // in this case we want every records
  client.continuousQuery('SELECT * FROM STREAM stream1', ({ recordId, record, error}) => {
      // process ... 
  });

};

DQL Driveline Query Language

Query syntax

Basic syntax

SELECT <selector> FROM STREAM <stream> [WHERE <expression>]
SELECT * FROM STREAM stream_1
SELECT * FROM STREAM stream_1 WHERE key=value
SELECT time AS t,(2+3) AS five FROM STREAM stream_1 WHERE age BETWEEN 21 AND 25 OR name LIKE 'Joe%'

DQL supports standard SQL query syntax, excluding Joins and Aggregates. This means DQL can be used for all forms of data filtering and partitioning of data over live streams.

KV query

SELECT <selector> FROM <string-key-name-expression> [WHERE <expression>]
SELECT * FROM 'users/*' WHERE color='red'

Multi key query is a DQL extension that lets you subscribe to multiple event streams, automatically subscribing to new streams as they form, based on the stream name expression. Stream name expression use file-system/Pythong Glob, with ?, * and ** serving as the wildcard match characters.

ECMAScript Object Notation (JavaScript extensions)

SELECT {time,name:user.name,phone_number:user.phone.mobile.number,original:{...*}} FROM stream

With inputs of the form:

{time:123, user: {name:'joe', phone: {mobile: {number:'1-800-123-4567'}}}}

Results in:

{time:123,name:'joe',phone_number:'1-800-123-4567',original:{time:123,user:phone:{...}}}

Operators

The following table summarizes all language operators in order of precedence

NameDescriptionExampleAdditional details
ORextended logical ORSELECT * FROM STREAM stream WHERE a OR bif (a) return a; else return b;
ANDlogical ANDSELECT * FROM STREAM stream WHERE a AND bif (a && b) return true; else return false;
NOTlogical NOTSELECT * FROM STREAM stream WHERE NOT aif (a) return false; else return true;
IS [NOT] NULLNull checkSELECT * FROM STREAM stream WHERE a IS NOT NULLif (null===a) return true; return false;
INSet lookupSELECT * FROM STREAM stream WHERE a IN (1,2,3)All values in paranthesis must be constants
BETWEENCompare rangeSELECT * FROM STREAM stream WHERE a BETWEEN b AND cif (b<c) return (a>=b && a<=c); else return (a>=c && a<=b);
= >= <= != > < !< !> <>CompareSELECT * FROM STREAM STREAM WHERE a <> b
LIKEPattern matchSELECT * FROM STREAM stream WHERE a like '%b%'_ stands for single, % stands for multi-char match
+ -Unary plus/minusSELECT * FROM STREAM stream WHERE -5 < +5
+ -AdditionSELECT * FROM STREAM stream WHERE 1+2=3
* / %MultiplicativeSELECT * FROM STREAM stream WHERE 3%2=1
( exp )ParanthesisSELECT * FROM STREAM stream WHERE (1+2)*3=9
true false nullConstantSELECT * FROM STREAM stream WHERE true != false
-123.45e-1Numeric constant
'hello'String constantSELECT * FROM stream WHERE name='joe'
nameIdentifierSELECT name FROM STREAM stream
`user name`IdentifierSELECT `user name` FROM STREAM stream(backticks) Allows using identifier names that are otherwise invalid, e.g., contain invalid characters or symbols

Built-in functions

NameDescriptionExample
ABSAbsolute value float=>floatSELECT ABS(-5) AS num FROM stream {num:5}
CEILRounded up value float=>floatSELECT CEIL(4.5) AS num FROM stream {num:5}
FLOORRounded down value float=>floatSELECT FLOOR(4.5) AS num FROM stream {num:4}
EXPNatural exponent float=>floatSELECT EXP(1) AS num FROM stream {num:2.718281828459045}
LNNatural logarithm float=>floatSELECT LN(2) AS num FROM stream {num:0.6931471805599453}
SQRTSquare root float=>floatSELECT SQRT(9) AS num FROM stream {num:3}
HASHHash function any=>uint64SELECT HASH('abc') AS num FROM stream {num:4952883123889572249}
CHAR_LENGTHLength of string string->int32SELECT CHAR_LENGTH('abc') AS num FROM stream {num:3}
POSITIONIndex of substring in string string,string->int32SELECT POSITION('bc' IN 'abc') AS num FROM stream {num:2}
LOCATEIndex of substring in string string,string->int32SELECT LOCATE('bc', 'abc') AS num FROM stream {num:2}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文