使用 MarkLogic REST API 将文本文件加载为二进制文件

Load Text Files as Binary Using MarkLogic REST APIs

是否可以通过 MarkLogic REST APIs 将文本文件(无论其内容如何)作为二进制文档加载?更具体地说是通过资源扩展端点?

我看到可以通过 xdmp:document-load 函数实现,但不太确定如何使用 REST APIs 实现。

xdmp:document-load("C:\my\path\test.txt",
    map:map() => map:with("uri", "/test/test.txt")
              => map:with("format", "binary")
)

我试图通过 PUT /v1/documents API 加载相同的文档并将 format 参数设置为 binary。但它仍然作为 text 文件加载。

用例是我需要摄取一堆附件文件,其中偶尔包含一些文本文件。我不需要 MarkLogic 来为其内容编制索引,事实上,如果 MarkLogic 尝试这样做,那么其中许多文件都存在编码或格式问题。

谢谢!

对于/v1/documents PUT,format参数用于指示元数据的格式,而不是文档的格式。

Controlling Input and Output Content Type

所述
  • Primary: URI extension MIME type mapping, as long as the request does not specify a transform function.
  • Fallback: Content-type header MIME type mapping. For multipart input, the request Content-type header must be multipart/mixed, so the Content-type header for each part specifies the MIME type of the content for that part.

文档 URI 中的资源文件扩展名用于查找已配置的 Mimetype。如果有匹配的条目,它将使用 format 作为配置的 Mimetype。

不幸的是,显式 Content-type header 不会覆盖隐式 format 确定。因此,如果您想将具有 .txt 文件扩展名的文档加载为 binary() 文档,那么您将需要实施一些解决方法。

为了将文本文档加载为 binary()/v1/documents PUT,您可以:

  • 使用不同的文件扩展名。将“.bin”附加到文本文件 URI 的末尾,即 /myTextFile.txt.bin。这可能不是我们想要的,因为它确实改变了文档的 URI,但确实表明文本文档正在存储为二进制文档。
  • 加载文档时应用自定义转换并指定所需的 Content-type

可以应用的直通变换示例,因此不应用隐式 URL format 检测,而应用显式 Content-type header :

function noop(context, params, content){
  return content;
} 
exports.transform=noop

installing the custom transform 之后,姓名 noop: 下面是安装 noop 转换的示例 curl 命令。适当更新 username/password:

curl --anyauth --user myUsername:myPassword -X PUT -i -d "function noop(context, params, content){return content;} exports.transform=noop" -H "Content-type: application/vnd.marklogic-javascript" http://localhost:8000/LATEST/config/transforms/noop

然后可以调用 /v1/documents PUT 并将 Content-type 指定为二进制 Mimetype(在本例中为 application-octet-stream):

curl --anyauth --user myUsername:myPassword -T ./test.txt -i -H "Content-type: application/octet-stream" "http://localhost:8000/v1/documents?uri=/test.txt&transform=noop"

它将被加载为 binary() 而不是 text()

doc("/test.txt")/node()/xdmp:node-kind(.)

产量:binary