Skip to content

Commit a55db09

Browse files
committed
add new feature : streaming api
1 parent e2e2f5b commit a55db09

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1811
-347
lines changed

.vs/dolphindb_csharpapi/v15/.suo

6 KB
Binary file not shown.

README.md

Lines changed: 279 additions & 120 deletions
Large diffs are not rendered by default.

README_CN.md

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
### 1. C# API 概念
22
C# API本质上实现了.Net程序和DolphinDB服务器之间的消息传递和数据转换协议。
3-
C# API运行在.Net Framework 4.0 及以上环境
3+
C# API运行在.Net Framework 4.0 及以上环境
44

5-
C# API遵循面向接口编程的原则。C# API使用接口类Entity来表示DolphinDB返回的所有数据类型。在Entity接口类的基础上,根据DolphinDB的数据类型,C# API提供了7种拓展接口,分别是scalar,vector,matrix,set,dictionary,table和chart。这些接口类都包含在 dolphindb.data包中。
5+
C# API遵循面向接口编程的原则。C# API使用接口类Entity来表示DolphinDB返回的所有数据类型。在Entity接口类的基础上,根据DolphinDB的数据类型,C# API提供了6种拓展接口,分别是scalar,vector,matrix,set,dictionary,table。这些接口类都包含在 dolphindb.data包中。
66

77
拓展的接口类|命名规则|例子
88
---|---|---
99
scalar|`Basic<DataType>`|BasicInt, BasicDouble, BasicDate, etc.
1010
vector,matrix|`Basic<DataType><DataForm>`|BasicIntVector, BasicDoubleMatrix, BasicAnyVector, etc.
1111
set, dictionary和table|`Basic<DataForm>`|BasicSet, BasicDictionary, BasicTable.
12-
chart||BasicChart
1312

1413
“Basic”表示基本的数据类型接口,`<DataType>`表示DolphinDB数据类型名称,`<DataForm>`是一个DolphinDB数据形式名称。
1514

16-
详细接口和类描述请参考[C# API手册](https://www.dolphindb.com/C#api/)
17-
1815
DolphinDB C# API 提供的最核心的对象是DBConnection,它主要的功能就是让C#应用可以通过它在DolphinDB服务器上执行脚本和函数,并在两者之间双向传递数据。
1916
DBConnection类提供如下主要方法:
2017

@@ -48,7 +45,7 @@ public void Test_Connect(){
4845
```
4946
boolean success = conn.connect("localhost", 8848, "admin", "123456");
5047
```
51-
当不带用户名密码连接成功后,脚本在Guest权限下运行,后续运行中若需要提升权限,可以通过调用 `conn.login('admin','123456',true)` 登录获取权限。
48+
当不带用户名密码连接成功后,脚本在Guest权限下运行,后续运行中若需要提升权限,可以通过调用 `conn.login('admin','123456',false)` 登录获取权限。
5249

5350
### 3.运行脚本
5451

@@ -63,13 +60,13 @@ conn.run("<SCRIPT>");
6360
### 4. 运行函数
6461
当一段逻辑需要被服务端脚本反复调用时,可以用DolphinDB脚本将逻辑封装成自定义函数,类似于存储过程,然后在C#程序中通过函数方式调用。
6562

66-
下面的示例展示C#程序调用DolhinDB的add函数的方式,add函数有两个参数,参数的存储位置不同,也会导致调用方式的不同,下面会分三种情况来展示示例代码
63+
下面的示例展示C#程序调用DolhinDB的`add`函数,`add`函数有两个参数`x,y`,参数的存储位置不同,也会导致调用方式的不同,存在以下三种情况
6764

6865
* 所有参数都在DolphinDB Server端
6966

7067
变量 x, y 已经通过C#程序提前在服务器端生成。
7168
```
72-
conn.run("x = [1,3,5];y = [2,4,6]")
69+
conn.run("x = [1,3,5];y = [2,4,6]");
7370
```
7471
那么在C#端要对这两个向量做加法运算,只需要直接使用`run(script)`的方式即可
7572
```
@@ -85,7 +82,7 @@ public void testFunction()
8582

8683
变量 x 已经通过C#程序提前在服务器端生成,参数 y 要在C#客户端生成
8784
```
88-
conn.run("x = [1,3,5]")
85+
conn.run("x = [1,3,5]");
8986
```
9087
这时就需要使用`部分应用`方式,把参数 x 固化在add函数内,具体请参考[部分应用文档](https://www.dolphindb.com/cn/help/PartialApplication.html)
9188

@@ -123,9 +120,8 @@ public void testFunction()
123120
```
124121

125122
### 5. 上传数据对象
126-
当C#中的一些数据需要被服务端频繁的用到,那么每次调用的时候都上传一次肯定不是一个好的做法,这个时候可以使用upload方法,将数据上传到服务器并分配给一个变量,在Server端就可以重复使用这个变量。
127-
128-
我们可以将二进制数据对象上传到DolphinDB服务器,并将其分配给一个变量以备将来使用。 变量名称可以使用三种类型的字符:字母,数字或下划线。 第一个字符必须是字母。
123+
当C#中的一些数据需要被服务端频繁的用到,那么每次调用的时候都上传一次肯定不是一个好的做法,这个时候可以使用upload方法,将数据上传到服务器并分配给一个变量,后续就可以重复使用这个变量。
124+
变量名称可以使用三种类型的字符:字母,数字或下划线。 第一个字符必须是字母。
129125

130126
```
131127
public void testUpload()
@@ -159,7 +155,7 @@ using dolphindb.data;
159155
```
160156
rand(`IBM`MSFT`GOOG`BIDU,10)
161157
```
162-
返回C#对象BasicStringVector。vector.rows()方法能够获取向量的大小。我们可以使用vector.getString(i)方法按照索引访问向量元素。
158+
返回C#对象BasicStringVector。vector.rows()方法能够获取向量的大小。我们可以使用vector.get(i)方法按照索引访问向量元素。
163159

164160
```
165161
public void testStringVector(){
@@ -178,10 +174,7 @@ public void testDoubleVector(){
178174
Console.WriteLine(v.rows());
179175
Console.WriteLine(Math.Round(((BasicDouble)v.get(1)).getValue(), 4));
180176
}
181-
```
182177
183-
184-
```
185178
public void testAnyVector(){
186179
BasicAnyVector v = (BasicAnyVector)conn.run("[1 2 3,3.4 3.5 3.6]");
187180
Console.WriteLine(v.rows());
@@ -201,7 +194,7 @@ public void testSet(){
201194

202195
- 矩阵
203196

204-
要从整数矩阵中检索一个元素,我们可以使用getInt(row,)。 要获取行数和列数,我们可以使用函数rows()和columns()。
197+
要从整数矩阵中检索一个元素,可以使用getInt(row,)。 要获取行数和列数,可以使用函数rows()和columns()。
205198

206199
```
207200
public void testIntMatrix(){
@@ -233,7 +226,7 @@ public void testDictionary(){
233226

234227
-
235228

236-
要获取表的列,我们可以调用table.getColumn(index);同样,我们可以调用table.getColumnName(index)获取列名。 对于列和行的数量,我们可以分别调用table.columns()和table.rows()。
229+
要获取表的列,可以用table.getColumn(index),使用table.columns()和table.rows()来分别获取列和行数
237230

238231
```
239232
public void testTable(){
@@ -249,7 +242,7 @@ public void testTable(){
249242
```
250243
public void testVoid(){
251244
IEntity obj = conn.run("NULL");
252-
Assert.AreEqual(obj.getObject(), null);
245+
Console.WriteLine(obj.getDataType());
253246
}
254247
255248
```
@@ -306,7 +299,7 @@ public void test_save_TableInsert(string[] strArray, int[] intArray, long[] tsAr
306299
```
307300
def saveData(v1,v2,v3,v4){tableInsert(sharedTable,v1,v2,v3,v4)}
308301
```
309-
然后再通过`conn.run("saveData",args)`运行函数虽然这样也能实现目标,但是对Java程序来说要多一次服务端的调用,多消耗了网络资源。
302+
然后再通过`conn.run("saveData",args)`运行函数虽然这样也能实现目标,但是对Java程序来说要多一次服务端的调用,多消耗了网络资源。
310303
在本例中,使用了DolphinDB 中的`部分应用`这一特性,将服务端表名以`tableInsert{sharedTable}`这样的方式固化到tableInsert中,作为一个独立函数来使用。这样就不需要再使用自定义函数的方式实现。
311304
具体的文档请参考[部分应用文档](https://www.dolphindb.com/cn/help/PartialApplication.html)
312305

@@ -320,7 +313,7 @@ public void test_save_table(BasicTable table1)
320313
conn.run("append!{shareTable}", args);
321314
}
322315
```
323-
#### 7.2 保存数据到分布式表
316+
### 7.2 保存数据到分布式表
324317
分布式表是DolphinDB推荐在生产环境下使用的数据存储方式,它支持快照级别的事务隔离,保证数据一致性; 分布式表支持多副本机制,既提供了数据容错能力,又能作为数据访问的负载均衡。
325318

326319
本例中涉及到的数据表可以通过如下脚本构建 :
@@ -373,7 +366,7 @@ public void test_save_table(String dbPath, BasicTable table1)
373366
conn.run(String.Format("append!{loadTable('%s','tb1')}",dbPath), args);
374367
}
375368
```
376-
#### 7.4 读取和使用表数据
369+
### 7.4 读取和使用表数据
377370
在C# API中,表数据保存为BasicTable对象,由于BasicTable是列式存储,所以要读取和使用所有desultory需要通过先取出列,再循环取出行的方式。
378371

379372
例子中参数BasicTable的有4个列,分别是`STRING,INT,TIMESTAMP,DOUBLE`类型,列名分别为`cstring,cint,ctimestamp,cdouble`

dolphindb_csharpapi.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.9
4+
VisualStudioVersion = 15.0.26430.15
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dolphindb_csharpapi", "dolphindb_csharpapi\dolphindb_csharpapi.csproj", "{F744DA92-D9F2-49BB-9783-F89E825CB307}"
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dolphindb_csharpapi_test", "dolphindb_csharpapi_test\dolphindb_csharpapi_test.csproj", "{9297EF6F-E380-423B-BA64-FE2907226FF8}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dolphindb_dataAppender", "dolphindb_dataAppender\dolphindb_dataAppender.csproj", "{59591491-5A40-470F-AC0C-EBB66382E5DD}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -41,6 +43,18 @@ Global
4143
{9297EF6F-E380-423B-BA64-FE2907226FF8}.Release|x64.Build.0 = Release|Any CPU
4244
{9297EF6F-E380-423B-BA64-FE2907226FF8}.Release|x86.ActiveCfg = Release|Any CPU
4345
{9297EF6F-E380-423B-BA64-FE2907226FF8}.Release|x86.Build.0 = Release|Any CPU
46+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Debug|x64.ActiveCfg = Debug|Any CPU
49+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Debug|x64.Build.0 = Debug|Any CPU
50+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Debug|x86.ActiveCfg = Debug|Any CPU
51+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Debug|x86.Build.0 = Debug|Any CPU
52+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|x64.ActiveCfg = Release|Any CPU
55+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|x64.Build.0 = Release|Any CPU
56+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|x86.ActiveCfg = Release|Any CPU
57+
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|x86.Build.0 = Release|Any CPU
4458
EndGlobalSection
4559
GlobalSection(SolutionProperties) = preSolution
4660
HideSolutionNode = FALSE

dolphindb_csharpapi/DBConnection.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.IO;
11+
using System.Net;
1112
using System.Net.Sockets;
1213
using System.Threading;
1314

@@ -87,7 +88,7 @@ public bool connect(string hostName, int port, string userId, string password, s
8788
{
8889
try
8990
{
90-
if (this.sessionID.Length > 0)
91+
if (sessionID.Length > 0)
9192
{
9293
return true;
9394
}
@@ -96,7 +97,7 @@ public bool connect(string hostName, int port, string userId, string password, s
9697
this.port = port;
9798
this.userId = userId;
9899
this.password = password;
99-
this.encrypted = false;
100+
encrypted = false;
100101
this.initialScript = initialScript;
101102
return connect();
102103

@@ -155,8 +156,8 @@ public bool connect()
155156
remoteLittleEndian = true;
156157
}
157158

158-
if (this.userId.Length > 0 && this.password.Length > 0) login();
159-
if (this.initialScript != "") run(initialScript);
159+
if (userId.Length > 0 && password.Length > 0) login();
160+
if (initialScript != "") run(initialScript);
160161
return true;
161162

162163
}
@@ -168,7 +169,7 @@ public void login(string userId, string password, bool enableEncryption)
168169
{
169170
this.userId = userId;
170171
this.password = password;
171-
this.encrypted = enableEncryption;
172+
encrypted = enableEncryption;
172173
//this.encrypted = false; //no encrypted temporary
173174
login();
174175
}
@@ -184,13 +185,13 @@ public void login(string userId, string password, bool enableEncryption)
184185
private void login()
185186
{
186187
List<IEntity> args = new List<IEntity>();
187-
if (this.encrypted)
188+
if (encrypted)
188189
{
189190
BasicString keyCode = (BasicString)run("getDynamicPublicKey()");
190191

191192
string key = RSAUtils.GetKey(keyCode.getString());
192-
string usr = RSAUtils.RSA(this.userId, key);
193-
string pass = RSAUtils.RSA(this.password, key);
193+
string usr = RSAUtils.RSA(userId, key);
194+
string pass = RSAUtils.RSA(password, key);
194195

195196

196197
args.Add(new BasicString(usr));
@@ -202,7 +203,7 @@ private void login()
202203
{
203204
args.Add(new BasicString(userId));
204205
args.Add(new BasicString(password));
205-
run("login('" + this.userId + "','" + this.password + "')"); //no encrypted temporary
206+
run("login('" + userId + "','" + password + "')"); //no encrypted temporary
206207

207208
}
208209
//login("login", args);
@@ -214,7 +215,7 @@ public virtual bool RemoteLittleEndian
214215
{
215216
get
216217
{
217-
return this.remoteLittleEndian;
218+
return remoteLittleEndian;
218219
}
219220
}
220221

@@ -370,11 +371,10 @@ public virtual IEntity run(string script, ProgressListener listener)
370371
if (reconnect)
371372
{
372373
sessionID = headers[0];
373-
if (this.userId.Length>0 && this.password.Length>0)
374+
if (userId.Length>0 && password.Length>0)
374375
{
375376
login();
376377
}
377-
if (this.initialScript != "") run(initialScript);
378378
}
379379
int numObject = int.Parse(headers[1]);
380380

@@ -515,11 +515,10 @@ public virtual IEntity run(string function, IList<IEntity> arguments)
515515
if (reconnect)
516516
{
517517
sessionID = headers[0];
518-
if (this.userId.Length > 0 && this.password.Length > 0)
518+
if (userId.Length > 0 && password.Length > 0)
519519
{
520520
login();
521521
}
522-
if (this.initialScript != "") run(initialScript);
523522
}
524523
int numObject = int.Parse(headers[1]);
525524

@@ -681,11 +680,10 @@ public virtual void upload(IDictionary<string, IEntity> variableObjectMap)
681680
if (reconnect)
682681
{
683682
sessionID = headers[0];
684-
if (this.userId.Length > 0 && this.password.Length > 0)
683+
if (userId.Length > 0 && password.Length > 0)
685684
{
686685
login();
687686
}
688-
if (this.initialScript != "") run(initialScript);
689687
}
690688
string msg = @in.readLine();
691689
if (!msg.Equals("OK"))
@@ -756,6 +754,13 @@ public virtual int Port
756754
}
757755
}
758756

757+
public string LocalAddress
758+
{
759+
get
760+
{
761+
return ((IPEndPoint)socket.LocalEndPoint).Address.ToString();
762+
}
763+
}
759764
}
760765

761766
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

dolphindb_csharpapi/data/AbstractMatrix.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public abstract class AbstractMatrix : AbstractEntity, IMatrix
2626

2727
protected internal AbstractMatrix(int rows, int columns)
2828
{
29-
this._rows = rows;
30-
this._columns = columns;
29+
_rows = rows;
30+
_columns = columns;
3131
}
3232

3333
protected internal AbstractMatrix(ExtendedDataInput @in)
@@ -314,12 +314,12 @@ public virtual void write(ExtendedDataOutput @out)
314314
public virtual DataTable toDataTable()
315315
{
316316
DataTable dt = buildTable();
317-
for (int i = 0; i < this.rows(); i++)
317+
for (int i = 0; i < rows(); i++)
318318
{
319319
DataRow dr = dt.NewRow();
320-
for (int j = 0; j < this.columns(); j++)
320+
for (int j = 0; j < columns(); j++)
321321
{
322-
dr[j] = this.get(i, j).getObject();
322+
dr[j] = get(i, j).getObject();
323323
}
324324
dt.Rows.Add(dr);
325325
}
@@ -328,16 +328,16 @@ public virtual DataTable toDataTable()
328328
protected DataTable buildTable()
329329
{
330330
DataTable dt = new DataTable();
331-
string[] colnames = new string[this._columns];
332-
if (this.columnLabels == null)
333-
for (int c = 0; c < this._columns; c++)
331+
string[] colnames = new string[_columns];
332+
if (columnLabels == null)
333+
for (int c = 0; c < _columns; c++)
334334
colnames[c] = "col" + c.ToString();
335335
else
336-
for (int c = 0; c < this._columns; c++)
337-
colnames[c] = this.columnLabels.get(c).getString();
336+
for (int c = 0; c < _columns; c++)
337+
colnames[c] = columnLabels.get(c).getString();
338338

339-
for (int j = 0; j < this.columns(); j++)
340-
dt.Columns.Add(colnames[j], Utils.getSystemType(this.getDataType()));
339+
for (int j = 0; j < columns(); j++)
340+
dt.Columns.Add(colnames[j], Utils.getSystemType(getDataType()));
341341

342342
return dt;
343343
}

dolphindb_csharpapi/data/AbstractVector.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public virtual DataTable toDataTable()
2424
{
2525
DataTable dt = buildTable();
2626

27-
for (int i = 0; i < this.rows(); i++)
27+
for (int i = 0; i < rows(); i++)
2828
{
2929
DataRow dr = dt.NewRow();
30-
dr[0] = this.get(i).getObject();
30+
dr[0] = get(i).getObject();
3131
dt.Rows.Add(dr);
3232
}
3333
return dt;
@@ -83,7 +83,7 @@ public virtual void write(ExtendedDataOutput @out)
8383
protected DataTable buildTable()
8484
{
8585
DataTable dt = new DataTable();
86-
dt.Columns.Add("dolphinVector", Utils.getSystemType(this.getDataType()));
86+
dt.Columns.Add("dolphinVector", Utils.getSystemType(getDataType()));
8787
return dt;
8888
}
8989

0 commit comments

Comments
 (0)