我到处查看过,甚至JSONiq文档都说“这超出了本文档的范围”。我有一个JSON文件(一个JSON对象数组),我想导入到JSONiq中(特别是Zorba,顺便说一句,它是一个糟糕的名称,因为它使互联网搜索信息无效)作为一个集合来查询。有没有教程,规范,或任何地方告诉我如何做到这一点?
发布于 2018-06-12 08:02:41
Zorba支持向集合添加文档。这样做的框架是文档化的这里。但是,请注意,Zorba是一个内存存储,不会在一个查询范围之外持久化任何东西,因此在没有持久层的情况下这是有限的。
如果用例只是查询存储在本地驱动器上的JSON文件,那么使用EXPath文件模块和解析-json可能更简单,如下所示:
jsoniq version "1.0";
import module namespace file = "http://expath.org/ns/file";
let $my-object := parse-json(file:read-text("/path/to/document.json"))
return $my-object.foo如果"bar"包含/path/to/document.json,则上面的查询返回/path/to/document.json
{ "foo" : "bar" } parse-json为您提供了使用多个对象(JSON行等)解析文档的附加选项。
对于高级用户,这是如何使用集合避免每次读取文件:
jsoniq version "1.0";
import module namespace file = "http://expath.org/ns/file";
import module namespace ddl = "http://zorba.io/modules/store/dynamic/collections/ddl";
import module namespace dml = "http://zorba.io/modules/store/dynamic/collections/dml";
(: Populating the collection :)
variable $my-collection := QName("my-collection");
ddl:create($my-collection, parse-json(file:read-text("/tmp/doc.json")));
(: And now the query :)
for $object in dml:collection($my-collection)
group by $value := $object.foo
return {
"value" : $value,
"count" : count($object)
}我是/tmp/doc.json
{ "foo" : "bar" }
{ "foo" : "bar" }
{ "foo" : "foo" }
{ "foo" : "foobar" }
{ "foo" : "foobar" }上面的查询返回:
{ "value" : "bar", "count" : 2 }
{ "value" : "foobar", "count" : 2 }
{ "value" : "foo", "count" : 1 }发布于 2020-04-16 13:25:01
为了完整起见,为了完整起见,对于Spark上的分布式隆隆声实现,JSON文件使用JSON ()(当分散在多行时)或json- line () (其中每行有一个JSON值,可能有数十亿行)读取。
https://stackoverflow.com/questions/50807182
复制相似问题