简体   繁体   English

我可以在Marklogic Server中的xquery中声明一个全局变量吗?

[英]Can I declare a global variable in xquery in Marklogic Server?

I want a global variable which I can use in my different .xqy pages. 我想要一个全局变量,我可以在不同的.xqy页面中使用它。 Can I declare such a variable in xquery in Marklogic Server ? 我可以在Marklogic Server中的xquery中声明这样的变量吗?

You can declare a variable in any module. 您可以在任何模块中声明变量。 For instance, it is config.xqy. 例如,它是config.xqy。

declare variable $PRECISION as xs:integer := 4;

For using this variable you need to import this module in your work module. 要使用此变量,您需要在工作模块中导入此模块。

import module namespace config = "http://your-namespace" at "config.xqy";

And refer to this variable: 并参考这个变量:

$config:PRECISION

如果您的应用程序在单个E节点上运行,则可以使用服务器字段 ,这些字段也是针对此用例设计的。

If you need values accessible across the server, there is a library in the Marklogic XQuery Commons for storing persistent key/value pairs: 如果您需要跨服务器访问的值,Marklogic XQuery Commons中有一个用于存储持久键/值对的库:

https://github.com/marklogic/commons/blob/master/properties/properties.xqy https://github.com/marklogic/commons/blob/master/properties/properties.xqy

And you may have already considered this, but you could also just simply store the global data in a document on the database and access with doc() - or eval() if you need to get to it from a different database. 您可能已经考虑过这一点,但您也可以简单地将全局数据存储在数据库中的文档中,并使用doc()或eval()进行访问(如果需要从其他数据库访问它)。

You have a few options. 你有几个选择。 If you need a global constant variable, the config.xqy method mentions in @Andrew Orlov's answer is great because you avoid any locking from concurrent access to a properties.xml file. 如果你需要一个全局常量变量,那么在@Andrew Orlov的答案中提到的config.xqy方法是很好的,因为你可以避免任何锁定对properties.xml文件的并发访问。

If you need a variable that can be mutated across a cluster of nodes, the property.xqy example linked by @wst appears to use globally assigned namespaces to embed a retrievable key and value. 如果需要一个可以在节点集群中进行变异的变量,则@wst链接的property.xqy示例似乎使用全局分配的名称空间来嵌入可检索的键和值。 Pretty clever. 很聪明。 However, I'm not sure how much this is meant for heavy levels of change. 但是,我不确定这对于重大变化有多大意义。

The E-node specific variable from @Eric Bloch is good, but please also be aware that it will not survive a system restart. 来自@Eric Bloch的E节点特定变量很好,但请注意它不会在系统重启后继续存在。

I'd be interested to know how these all compare performance-wise. 我有兴趣知道这些都是如何比较性能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM