Home > Back-end >  How do I declare and define global variables?
How do I declare and define global variables?

Time:12-08

I’d like to define some global configuration parameters to share them across sessions. How do I declare and define global variables in DolphinDB?

CodePudding user response:

you can define global variables using the global keyword.

global x = 1

CodePudding user response:

Solution #1:

You can create a thread-safe dictionary which is shared across sessions. For example, create a global configuration file:

config = syncDict(STRING, STRING, `config)
config[`key1]=`value1
config[`key2]=`value2

Solution #2:

The first solution can only take effect on one node. To share configuration among nodes while maintaining the consistency, it is recommended to use a high-availability stream table.

colNames = `key`value`modifyTs
colTypes = [STRING, STRING, TIMESTAMP]
t=table(1000:0,colNames,colTypes)
haStreamTable(2, t,`global_config,10000000, keyColumn=`key, retentionMinutes=9999999)

In this case, you can even use DolphinDB as ZooKeeper. The configuration can be shared across the cluster, and can be restored after crash.

  • Related