5
我有以下挑戰。我們有我們想要使用mlcp加載到MarkLogic數據庫的csv文件。我們還希望在加載過程中將加載的行轉換爲OBI源代碼,因此我們爲此創建了一個轉換函數。mlcp將csv文件轉換爲OBI源文件
現在我正在爲變換而努力。沒有轉換,數據按預期加載爲每行文檔。
CSV例如:
voornaam,achternaam
hugo,koopmans
thijs,van ulden
變換ambulance.xqy:
xquery version "1.0-ml";
module namespace rws = "http://marklogic.com/rws";
import module namespace source = "http://marklogic.com/solutions/obi/source" at "/ext/obi/lib/source-lib.xqy";
(: If the input document is XML, create an OBI source from it, with the value
: specified in the input parameter. If the input document is not
: XML, leave it as-is.
:)
declare function rws:transform(
$content as map:map,
$context as map:map
) as map:map*
{
let $attr-value :=
(map:get($context, "transform_param"), "UNDEFINED")[1]
let $the-doc := map:get($content, "value")
return
if (fn:empty($the-doc/element()))
then $content
else
let $root := xdmp:unquote($the-doc/*)
let $source-title := "ambulance source data"
let $collection := 'ambulance'
let $source-id := source:create-source($source-title,(),$root)
let $_ := xdmp:document-add-collections(concat("/marklogic.solutions.obi/source/", $source-id[1],".xml"), $collection)
return (
map:put($content, "value",
$source-id[2]
), $content
)
};
MLCP命令:
mlcp.sh import \
-host localhost \
-port 27041 \
-username admin \
-password admin \
-input_file_path ./sampledata/so-example.csv \
-input_file_type delimited_text \
-transform_module /transforms/transform-ambulance.xqy \
-transform_namespace "http://marklogic.com/rws" \
-mode local
MLCP輸出:
15/09/08 21:35:08 INFO contentpump.ContentPump: Hadoop library version: 2.6.0
15/09/08 21:35:08 INFO contentpump.LocalJobRunner: Content type: XML
15/09/08 21:35:08 INFO input.FileInputFormat: Total input paths to process : 1
15/09/08 21:35:10 WARN mapreduce.ContentWriter: XDMP-DOCROOTTEXT: xdmp:unquote(document{<root><voornaam>hugo</voornaam><achternaam>koopmans</achternaam></root>}) -- Invalid root text "hugokoopmans" at line 1
15/09/08 21:35:10 WARN mapreduce.ContentWriter: XDMP-DOCROOTTEXT: xdmp:unquote(document{<root><voornaam>thijs</voornaam><achternaam>van ulden</achternaam></root>}) -- Invalid root text "thijsvan ulden" at line 1
15/09/08 21:35:11 INFO contentpump.LocalJobRunner: completed 100%
15/09/08 21:35:11 INFO contentpump.LocalJobRunner: com.marklogic.contentpump.ContentPumpStats:
15/09/08 21:35:11 INFO contentpump.LocalJobRunner: ATTEMPTED_INPUT_RECORD_COUNT: 2
15/09/08 21:35:11 INFO contentpump.LocalJobRunner: SKIPPED_INPUT_RECORD_COUNT: 0
15/09/08 21:35:11 INFO contentpump.LocalJobRunner: Total execution time: 2 sec
我曾嘗試沒有xdmp:所享有(),但後來我打了一個強制文檔節點()錯誤......
請諮詢......
很高興你知道了。請接受您的答案,將其從「未答覆」列表中移除。 (是的,StackOverflow禮儀鼓勵你這樣做。) –
我注意到了兩件小事情:首先,'$ the-doc'已經是一個文檔節點,爲什麼要取'root'元素,節點。我認爲你可以直接使用'$ the-doc'而不是'$ root'。其次,你用從'source:create-source'返回的值更新'$ content',但我認爲該函數已經將一個文檔插入到數據庫中。我想你可以在那裏返回一個空序列。 – grtjn