巨大な XML ファイル (>10 GB) を処理して CSV に変換する必要があります。を使用してXML::Twig
います。
このファイルには、約 260 万人の顧客のデータが含まれており、それぞれに約 100 から 150 のフィールドがあります (顧客のプロファイルによって異なります)。
1 つのサブスクライバーのすべての値を hash に格納し%customer
、処理が完了したら、ハッシュの値を CSV 形式のテキスト ファイルに出力します。
問題はパフォーマンスです。処理には約6~8時間かかります。どのように減らすことができますか?
my $t = XML::Twig->new(
twig_handlers => {
'objects/simple' => \&simpleProcess ,
'objects/detailed' => \&detailedProcess ,
},
twig_roots => { objects => 1}
);
sub simpleProcess {
my ($t, $simple) = @_;
%customer= (); #reset the hash
$customer{id} = $simple->first_child_text('id');
$customer{Key} = $simple->first_child_text('Key');
}
詳細タグには、ネストされたフィールドを含むいくつかのフィールドが含まれています。そのため、さまざまな種類のフィールドを収集するために毎回関数を呼び出します。
sub detailedProcess {
my ($t, $detailed1) = @_;
$detailed = $detailed1;
if ($detailed->has_children('profile11')){ &profile11();}
if ($detailed->has_children('profile12')){ &profile12();}
if ($detailed->has_children('profile13')){ &profile13();}
}
sub profile11 {
foreach $comcb ($detailed->children('profile11')) {
$customer{COMCBcontrol} = $comcb->first_child_text('ValueID');
}
他の関数 *(value2, value3) についても同様です。シンプルにするための他の機能については言及していません。
<objecProfile>
<simple>
<id>12345</id>
<Key>N894FE</Key>
</simple>
<detailed>
<ntype>single</ntype>
<SubscriberType>genericSubscriber</SubscriberType>
<odbssm>0</odbssm>
<osb1>true</osb1>
<natcrw>true</natcrw>
<sr>2</sr>
<Profile11>
<ValueID>098765</ValueID>
</Profile11>
<Profile21>
<ValueID>098765</ValueID>
</Profile21>
<Profile22>
<ValueID>098765</ValueID>
</Profile22>
<Profile61>
<ValueID>098765</ValueID>
</Profile61>
</detailed>
</objectProfile>
foreach
問題は次のとおりです。ほとんどの場合、子インスタンスは顧客プロファイル全体で 1 回しか発生しませんが、すべての子に使用します。遅延が発生する可能性がありますか、またはパフォーマンスを改善するための他の提案はありますか? 糸通しなど? (ググったところ、スレッド化はあまり役に立たないことがわかりました。)