How to use aggregations framework in new Elasticsearch using Perl language - perl

I am working on Elasticsearch and I have to do aggregations (i.e used for summarize our data) I shared my code below....
CODE:
my $portal_es = Search::Elasticsearch->new(nodes => [$es_ip.':'.$es_port],request_timeout => 180);
my $result = $portal_es->scroll_helper(index => 10002500,size =>"10000", params=>{rest_total_hits_as_int =>true},{
#"size": 0,
aggs=> {
"my-agg-name"=> {
terms=> {
field=> "tcode"
}
}
}
});
I am getting following error
[Param] ** Expecting a HASH ref or a list of key-value pairs, called from sub Search::Elasticsearch::Role::Client::Direct::Main::scroll_helper at /home/prity/Desktop/BL_script/search_index_processor/aggregation.pl line 15. With vars: {'params' => ['index','10002500','size','10000','params',{'rest_total_hits_as_int' => 'true'},{'aggs' => {'my-agg-name' => {'terms' => {'field' => 'tcode'}}}}]}

The data structure in your argument list is probably broken.
my $result = $portal_es->scroll_helper(
index => 10002500,
size => "10000",
params => {
rest_total_hits_as_int => 'true'
},
{ # <---- here
aggs => {
# ...
You are passing a list of key/value pairs into the scroll_helper, but there is one extra last argument after params. It's a hash reference with aggs in it that has no key for it.
Turn on use warnings and you'll get a warning that your data structure is missing a value (because that hashref will stringify into a key).
You probably shouldn't have closed the hashref for params and opened a new one. But that's just a guess, I don't know what this method expects.

Related

Puppet - deep merge lookup default_value with hiera hash returned

I am trying to deep merge the lookup 'default_value' or 'default_values_hash' with the hash returned from a lookup. It will not merge and the default_value only appear to take effect if the hiera title isn't found at all. I cannot set resource defaults here as the values returned are later processed and not actual resource keys yet.
I've tried numerous variations including 'default_value', 'default_values_hash'. I'm seeking a way to just set a default hash in the manifest and have it deep merge with hiera to create a larger hash.
Manifest:
class test (
Hash $result = lookup('test::my_hash', {merge => 'deep', default_values_hash => {foo => 'bar', this => 'that', him => 'her'}}),
){
notice($result)
}
include test
Hiera:
---
test::my_hash:
foo: 'nobar'
this: 'then'
desired result (deep merge):
{ foo => 'nobar', this => 'then', him => 'her' }
actual result (returns hiera hash only):
{ foo => 'nobar', this => 'then' }
UPDATE:
I got it working with the code below. Still interested if anyone has a better solution.
class test (
$stuff = {
foo => 'bar',
this => 'that',
him => 'her'
},
Hash $result = deep_merge($stuff, lookup('test::my_hash')),
){
notice($result)
}
Unfortunately, that is the way lookup works. The default value is only used if no other value is found. The documentation for the default in lookup says
If present, lookup returns this when it can’t find a normal value. Default values are never merged with found values.
Your version using the deep_merge function from stdlib appears to be the best solution.
class foo {
$default_foo_attribute = {
foo => 'bar',
this => 'that',
him => 'her',
}
$attribute = deep_merge($default_foo_attribute,
lookup('foo::attribute',
Hash[String, String],
'deep',
{})
notice($attribute)
}

Example for creating split-transaction in Perl Finance::QIF

I need to import my bank-exported transactions (CSV) into GNUcash.
I am almost finished with the perl script using Finance::QIF
I parse the CSV and write it out like this:
my $record = {
header => "Type:Bank",
date => $outdatum,
memo => $outtext,
transaction => $outbetrag,
};
$out->header( $record->{header} );
$out->write($record);
....
But my problem is creating a split.
http://finance-qif.sourceforge.net/ says " If the transaction contains splits this will be defined and consist of an array of hash references. With each split potentially having the following values." - so I tried this:
my $record = {
header => "Type:Bank",
date => $outdatum,
memo => $outtext,
transaction => $outbetrag,
#splits = (
{
category => "Gesundheit:Arzt:Kind1",
memo => "L",
amount => "-161,66"
},
{
category => "Gesundheit:Arzt:Kind2",
memo => "F",
amount => "-162,66"
}
)
};
This leads to the error:
Unsupported field 'HASH(0x221c9e8)' found in record ignored in file '>_TESTqif.qif' line 22 at convert_bank_CSV.pl line 195.
Unfortunately, I nowhere found an example for creating a split, just for a normal transaction.
Can someone please help how Finance::QIF can be used to create split-transactions?
I know nothing about Finance::QIF but your #splits code makes no sense.
Try this instead:
my $record = {
header => "Type:Bank",
date => $outdatum,
memo => $outtext,
transaction => $outbetrag,
splits => [
{
category => "Gesundheit:Arzt:Kind1",
memo => "L",
amount => "-161,66",
},
{
category => "Gesundheit:Arzt:Kind2",
memo => "F",
amount => "-162,66",
}
],
};
See perldoc perlreftut for more information about references and data structures in Perl.

How can I join a nested Perl hash?

I have a Perl hash, where I store information about LUNs. It has the following structure:
my %luns = (
360000 => {
Devices => [
{ Major_Minor => "8:144",
SCSI_Address => "1:0:0:8",
SCSI_Device => "sdj",
SCSI_Host => "host1",
},
{ Major_Minor => "129:48",
SCSI_Address => "3:0:0:8",
SCSI_Device => "sder",
SCSI_Host => "host3",
},
],
DM_Device => "dm-13",
Size => "45G",
WWID => 360000,
},
360001 => {
Devices => [
{ Major_Minor => "70:144",
SCSI_Address => "1:0:1:39",
SCSI_Device => "sddb",
SCSI_Host => "host1",
},
{ Major_Minor => "135:48",
SCSI_Address => "3:0:1:39",
SCSI_Device => "sdij",
SCSI_Host => "host3",
},
],
DM_Device => "dm-53",
Size => "200G",
WWID => 360000,
},
);
How can I use join to get a comma-separated list of all SCSI_Devices, for example, of 360000?
You're working with a Hash of Hash of Array of Hash. To learn how to work with such structures, I recommend reading perldsc - Perl Data Structures Cookbook.
In this instance, the following loop will print out each of your device lists:
for my $id ( sort { $a <=> $b } keys %luns ) {
my #devices = map { $_->{SCSI_Device} } #{ $luns{$id}{Devices} };
print "$id - #devices\n";
}
Outputs:
360000 - sdj sder
360001 - sddb sdij
Live Demo
You say you want a list of values for LUN 360000, so for a start you need
$luns->{36000}
which is another hash with a Devices element, which has an array reference as a value, and DM_Device, Size, and WWID elements, whose values are simple scalars.
So presumably you want the list that is
$luns->{36000}{Devices}
which is an array of references to hashes, each of which has Major_Minor, SCSI_Address, SCSI_Device, and SCSI_Host elements.
It sounds like you want the SCSI_Device element, and map is the ideal tool to help you with this
my #scsi_devices = map { $_->{SCSI_Device} } #{ $luns->{360000}{Devices} };
That last step is a big leap, and it may help to separate it in your code. For instance, you can copy the reference to the list of devices for 360000, like this
my $devices = $luns->{360000}{Devices};
and extract the SCSI_Device from each of the hashes in that array with
my #scsi_devices = map { $_->{SCSI_Device} } #$devices;
Either way, the array reference must be dereferenced and the required element from each hash in that array must be extracted.
To get a CSV record, unless the data may contain commas of double-quotes, you simply need to join the result of that map
print join(',', #scsi_devices), "\n";
output
sdj,sder
Although I think this falls short of what you actually need. If this isn't clear then please ask.

How can I look and search for a key inside a heavily nested hash?

I am trying to check if a BIG hash has any keys from small hash and see if they exist, and if they do modify the BigHash with updated values from small hash.
So the lookup hash would look like this :
configure =(
CommonParameter => {
'SibSendOverride' => 'true',
'SibOverrideEnabledFlag' => 'true',
'SiPosition' => '8',
'Period' => '11'
}
)
But the BigHash is very very nested.. The key/hash CommonParameter from the small hash configure is there in the BigHash.
Can somebody help/suggest some ideas for me please?
Here is an example BigHash :
%BigHash = (
'SibConfig' => {
'CELL' => {
'Sib9' => {
'HnbName' => 'HnbName',
'CommonParameter' => {
'SibSendOverride' => 'false',
'SibMaskOverrideEnabledFlag' => 'false',
'SiPosition' => '0',
'Period' => '8'
}
}
}
},
)
I hope I was clear in my question. Trying to modify values of heavily nested BigHash based on Lookup Hash if those keys exist.
Can somebody help me? I am not approaching this in the right way. Is there a neat little key lookup fucntion or something available perhaps?
Give Data::Search a try.
use Data::Search;
#results = Data::Search::datasearch(
data => $BigHash, search => 'keys',
find => 'CommonParameter',
return => 'hashcontainer');
foreach $result (#results) {
# result is a hashref that has 'CommonParameter' as a key
if ($result->{CommonParameter}{AnotherKey} ne $AnotherValue) {
print STDERR "AnotherKey was ", $result->{CommonParameter}{AnotherKey},
" ... fixing\n";
$result->{CommonParameter}{AnotherKey} = $AnotherValue;
}
}

How do I override compare_fields when using Rose::HTML::Form

So I'm trying use Rose::HTML::Form and I want my fields to appear based on 'rank' rather than by name (the default) .
I've written a comparator subroutine:
sub _order_by_rank {
my ($self, $one, $two) = #_;
return $one->rank <=> $two->rank;
};
and referenced it in my form constructor:
Rose::HTML::Form->new(method => 'post', compare_fields => \&_order_by_rank);
But I am then left with:
Can't call method "name" on unblessed reference at /usr/lib/perl5/site_perl/5.8.8/Rose/HTML/Form/Field/Collection.pm line 405.
It seems to call the comparator before I've added anything.
After constructing the form object, I add some fields and then call init_fields:
$form->add_fields(
id => { type => 'hidden', value => "", rank => 0 },
number => { type => 'int', size => 4, required => 1, label => 'Plant Number', rank => 1 },
name => { type => 'text', size => 25, required => 1, label => 'Plant Name', rank => 2 },
...
);
$form->init_fields;
According to the documentation this is something people usually do. What it doesn't explain is how to do it.
Hopefully someone can explain this to me before I have to buy a new keyboard :)
From the documentation it looks as though, rather than passing in a subroutine reference, you need to subclass Rose::HTML::Form and override the compare_fields method.
The default comparison method is Rose::HTML::Form::compare_fields. You have to create subclasses if you want different sorting methods for different forms.
It would help me to explain further if you showed your full code.