将对象附加到json文件

时间:2016-04-18 11:20:39

标签: json perl

我正在尝试追加

 {"create": {"_index":"socteam", "_type":"products"}}

到这个json

{"general":{"date_time":"2016-04-1806:50:40","total_requests":12,"valid_requests":12,"failed_requests":0,"generation_time":0,"unique_visitors":12,"unique_files":11,"excluded_hits":0,"unique_referrers":0,"unique_not_found":0,"unique_static_files":0,"log_size":0,"bandwidth":1233,"log_path":"STDIN"},"visitors":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"20160418"}],"requests":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"2xxSuccess","items":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"200-OK:Therequestsentbytheclientwassuccessful"}]}]}

我试过了

{"create": {"_index":"socteam", "_type":"products"}},
{"general":{"date_time":"2016-04-1806:50:40","total_requests":12,"valid_requests":12,"failed_requests":0,"generation_time":0,"unique_visitors":12,"unique_files":11,"excluded_hits":0,"unique_referrers":0,"unique_not_found":0,"unique_static_files":0,"log_size":0,"bandwidth":1233,"log_path":"STDIN"},"visitors":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"20160418"}],"requests":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"2xxSuccess","items":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"200-OK:Therequestsentbytheclientwassuccessful"}]}]}

但是我一直在意外地结束输入,我试图在将这些数据插入弹性搜索之前为其定义索引。

2 个答案:

答案 0 :(得分:3)

您收到的评论是因为我认为您没有正确陈述您的问题。

您似乎正在尝试使用elasticsearch bulk API;这是对的吗?

如果是这样,那么API接受的是多个JSON文档,由换行符分隔。您将两个JSON对象合并到另一个JSON文件中。相反,您只需将两个对象作为两个单独的JSON文档放在不同的行上:

{"create": {"_index":"socteam", "_type":"products"}}
{"general":{"date_time":"2016-04-1806:50:40","total_requests":12,"valid_requests":12,"failed_requests":0,"generation_time":0,"unique_visitors":12,"unique_files":11,"excluded_hits":0,"unique_referrers":0,"unique_not_found":0,"unique_static_files":0,"log_size":0,"bandwidth":1233,"log_path":"STDIN"},"visitors":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"20160418"}],"requests":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"2xxSuccess","items":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"200-OK:Therequestsentbytheclientwassuccessful"}]}]}

没有逗号或任何内容。

另外,当您联系弹性搜索机器时,您正在使用端点/_bulk,是吗?

答案 1 :(得分:0)

正如评论者所说,你要么制作一个2元素的数组,要么合并两个哈希togeather(导致第二个用相同的键复制任何条目);

use v5.12;
use JSON::Tiny qw(decode_json encode_json);

my $j1 = decode_json '{"general":{"date_time":"2016-04-1806:50:40","total_requests":12,"valid_requests":12,"failed_requests":0,"generation_time":0,"unique_visitors":12,"unique_files":11,"excluded_hits":0,"unique_referrers":0,"unique_not_found":0,"unique_static_files":0,"log_size":0,"bandwidth":1233,"log_path":"STDIN"},"visitors":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"20160418"}],"requests":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"2xxSuccess","items":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"200-OK:Therequestsentbytheclientwassuccessful"}]}]}' ;

my $j2 = decode_json '{"create": {"_index":"socteam", "_type":"products"}}' ;

my @merged_array = ( %$j1 , %$j2 );
my %merged_hash  = ( %$j1 , %$j2 );

say encode_json \@merged_array ;
say "========================" ;
say encode_json \%merged_hash  ;

产生;

["general",{"log_path":"STDIN","unique_static_files":0,"generation_time":0,"unique_referrers":0,"unique_not_found":0,"date_time":"2016-04-1806:50:40","failed_requests":0,"bandwidth":1233,"unique_files":11,"excluded_hits":0,"log_size":0,"total_requests":12,"valid_requests":12,"unique_visitors":12},"requests",[{"bytes":1233,"hits":12,"percent":100,"data":"2xxSuccess","items":[{"bytes":1233,"hits":12,"percent":100,"data":"200-OK:Therequestsentbytheclientwassuccessful","visitors":12}],"visitors":12}],"visitors",[{"bytes":1233,"hits":12,"percent":100,"data":"20160418","visitors":12}],"create",{"_index":"socteam","_type":"products"}]
========================
{"create":{"_index":"socteam","_type":"products"},"general":{"log_path":"STDIN","unique_static_files":0,"generation_time":0,"unique_referrers":0,"unique_not_found":0,"date_time":"2016-04-1806:50:40","failed_requests":0,"bandwidth":1233,"unique_files":11,"excluded_hits":0,"log_size":0,"total_requests":12,"valid_requests":12,"unique_visitors":12},"requests":[{"bytes":1233,"hits":12,"percent":100,"data":"2xxSuccess","items":[{"bytes":1233,"hits":12,"percent":100,"data":"200-OK:Therequestsentbytheclientwassuccessful","visitors":12}],"visitors":12}],"visitors":[{"bytes":1233,"hits":12,"percent":100,"data":"20160418","visitors":12}]}