gpt4 book ai didi

marklogic - 如何在加载文件时从 csv 文件中删除列?

转载 作者:行者123 更新时间:2023-12-03 06:48:30 24 4
gpt4 key购买 nike

我想从 csv 文件中删除特定列并使用 mlcp 将其加载到数据库中。

我的 csv 文件包含:

URI,EmpId,Name,age,gender,salary
1/Niranjan,1,Niranjan,35,M,1000
2/Deepan,2,Deepan,25,M,2000
3/Mehul,3,Mehul,28,M,3000

我想使用该 URI 列作为文档的 uri,并且应该在插入的文档中跳过/删除该 uri 列。

怎么做?

最佳答案

使用 MLCP 而不是在 MarkLogic 数据中心上下文中时,最好的选择是使用 MLCP 转换。您可以在这里找到一些解释和一些示例:

Transforming Content During Ingestion

如果您要将 CSV 转换为 JSON,您可以使用如下所示的内容。

将其保存为/strip-columns.sjs 在模块数据库中:

/* jshint node: true */
/* global xdmp */

exports.transform = function(content, context) {
'use strict';

/* jshint camelcase: false */
var stripColumns = (context.transform_param !== undefined) ? context.transform_param.split(/,/) : [];
/* jshint camelcase: true */

// detect JSON, assumes uri has correct extension
if (xdmp.uriFormat(content.uri) === 'json') {

// Convert input to mutable object for manipulation
var newDoc = content.value.toObject();
Object.keys(newDoc)
.map(function(key) {
if (stripColumns.indexOf(key) > -1) {
delete newDoc[key];
}
});

// Convert result back into a document
content.value = newDoc;

}

// return updated content object
return content;
};

然后你可以用这样的东西调用它:

mlcp.sh import -input_file_path test.csv -input_file_type delimited_text -uri_id URI -document_type json -output_uri_prefix / -output_uri_suffix .json -output_collections data,type/csv,format/json -output_permissions app-user,read -transform_module /strip-columns.sjs -transform_param URI

呵呵!

关于marklogic - 如何在加载文件时从 csv 文件中删除列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53816551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com