Introduction of external URLs
[aai/sparky-fe.git] / scripts / elasticsearch / prepareElasticSearchBulkImport.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my $filename = $ARGV[0];
7 my $outputfile= $ARGV[1];
8
9 open my $fh_input, '<', $filename or die "Cannot open $filename: $!";
10 open my $fh_output, '>', $outputfile or die "Cannot open $outputfile: $!";
11
12 while ( my $line = <$fh_input> ) {
13     chomp ($line);
14
15     if ( $line =~ /(.*)(\".*\")(.*)/ ) {
16
17        # we have seen examples of the status field containing quoted comma-delimited
18        # strings which is messing up parsing of the record data which is supposed to be
19        # comma-separated at the field level.  This little block converts sections of
20        # this type of data into a single-quoted-string with a semi-colon delimiter instead.
21
22        my $beforeBadStr = $1;
23        my $badStr       = $2;
24        my $afterBadStr  = $3;
25
26        $badStr =~ s/,/;/g;
27        $badStr =~ s/"/'/g;
28
29        $line = $beforeBadStr . $badStr . $afterBadStr ;
30
31     }
32
33     my @row = split(",", $line);
34     print $fh_output "{\"index\":{\"_index\":\"auditdata\",\"_type\":\"default\"}\n";
35     print $fh_output "{\"entityType\": \"$row[0]\", \"errorMessage\": \"$row[1]\", \"violations\": [{ \"violationTimestamp\": \"$row[2]\", \"severity\": \"$row[3]\", \"violationType\": \"$row[4]\", \"violationDetails\": { \"MISSING_REL\": \"$row[5]\", \"entityType\": \"$row[6]\", \"entityId\": { \"vdc-id\": \"$row[7]\" } }, \"category\": \"$row[8]\" }, { \"violationTimestamp\": \"$row[9]\", \"severity\": \"$row[10]\", \"violationType\": \"$row[11]\", \"violationDetails\": { \"MISSING_REL\": \"$row[12]\", \"entityType\": \"$row[13]\", \"entityId\": { \"vdc-id\": \"$row[14]\" } }, \"category\": \"$row[15]\" }]}\n";
36
37 }
38
39 close($fh_input);
40 close($fh_output);
41