Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 如何使用无痛脚本语言将行与列表列表区分开来?_Sorting_Groovy_Unique_Distinct_<img Src="//i.stack.imgur.com/A3TTx.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch Painless - Fatal编程技术网 elasticsearch-painless,Sorting,Groovy,Unique,Distinct,elasticsearch Painless" /> elasticsearch-painless,Sorting,Groovy,Unique,Distinct,elasticsearch Painless" />

Sorting 如何使用无痛脚本语言将行与列表列表区分开来?

Sorting 如何使用无痛脚本语言将行与列表列表区分开来?,sorting,groovy,unique,distinct,elasticsearch-painless,Sorting,Groovy,Unique,Distinct,elasticsearch Painless,我有一个Groovy脚本: def results = [] def cluster = ['cluster1', 'cluster1', 'cluster1', 'cluster1', 'cluster1', 'cluster1']; def ports = ['4344', '4344', '4344', '4344', '4344', '4344']; def hostname = [ 'cluster1.com','cluster1.com','cluster1.com','clust

我有一个Groovy脚本:

def results = []
def cluster = ['cluster1', 'cluster1', 'cluster1', 'cluster1', 'cluster1', 'cluster1'];
def ports =  ['4344', '4344', '4344', '4344', '4344', '4344'];
def hostname = [ 'cluster1.com','cluster1.com','cluster1.com','cluster1.com','cluster1.com','cluster1.com' ];

def heapu = ['533.6', '526.72' , '518.82' , '515.73', '525.69', '517.71'] ;
def heapm = ['1212.15', '1212.15', '1212.15', '1212.15', '1212.15', '1212.15'];
def times = ['2017-10-08T07:26:21.050Z', '2017-10-08T07:26:11.042Z', '2017-10-08T07:25:51.047Z', '2017-10-08T07:25:31.055Z', '2017-10-08T07:26:01.047Z', '2017-10-08T07:25:41.041Z'] ;

for (int i = 0; i < cluster.size(); ++i){
    def c = cluster[i]
    def p = ports[i]
    def h = hostname[i]
    def hu = heapu[i]
    def hm = heapm[i]
    def t = times[i]

    results.add(['cluster': c,
                 'port': p,
                 'hostname': h,
                 'heap_used': hu,
                 'heap_max': hm,
                 'times': t])
    results = results.unique()
}
//    return ['results': results, 'singlex': singlex]

for (i = 0; i < results.size(); i++){
    println(results[i])
}
从输出->中可以看出,我基本上有6行相同的行,它们随时间戳的不同而不同。HeapSize和Max HeapSize是不同的,但这并不重要

由于<强>簇< /强>对于<强>所有< /强>相同。六个条目/簇1 / I将其视为一个输出。理想情况下,我希望应用某种unique()函数,它将为我提供一行作为输出

例如:

[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:523.0450, heap_max:1212.15, times:2017-10-08T07:25:41.041Z]
其中使用的heap_是6个值的平均值以及heap_max。 我知道在python中,我只需一个命令就可以完成它。但是我不知道groovy,我一直在互联网上搜索


编辑:很遗憾,Groovy解决方案没有将1:1转换为无痛模式

您可以通过以下方式处理
结果
列表:

def grouped = results.groupBy { [it.cluster, it.port, it.hostname] }
        .entrySet()
        .collect { it -> [cluster: it.key.get(0), port: it.key.get(1), hostname: it.key.get(2)] + [
                heap_used: it.value.heap_used*.toBigDecimal().sum() / it.value.size(),
                heap_max: it.value.heap_max*.toBigDecimal().sum() / it.value.size(),
                times: it.value.times.max()
        ]}
首先,我们通过包含
集群
端口
主机名
的三元组对所有列表元素进行分组。然后,我们通过将
集群
端口
主机名
heap\u used:avg(heap\u used)
heap\u max:avg(heap\u max)
times:max(times)
组合来收集所有条目

这里

我们获取所有使用的
heap\u
值(
it.value.heap\u
)的列表,然后使用扩展运算符对每个列表元素应用
.toBigDecimal()
,因为初始值表示为字符串。要计算平均值,我们只需将所有使用的
heap\u值之和除以列表的大小即可

输出 打印
grouped
变量将显示以下结果:

[[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:523.045, heap_max:1212.15, times:2017-10-08T07:26:21.050Z]]

嗨@Szymon Stepniak,首先让我非常感谢你的回答。我在IntelliJ IDEA(使用groovy 2.4和java 9)中对其进行了评估,唯一的问题是
port:null
——这可能是因为java版本。问题是,这个解决方案应该在ElasticSearchWatcher中实现,它使用的是无痛语言,应该是非常groovy的。显然,它不起作用:(.我花了这么多时间在它上面.“type”:“script_exception”,“reason”:“compile error”,“script_stack”:[“…rouped=results.groupBy{[It.cluster,It.port,…”,“^----这里的“lang”:“无痛”,“由引起”:“{”type:“非法参数_exception”,“原因”:“意外标记['{']应为[{,;'}]中的一个。”}@用户2156115关于
端口
的事情-检查它是
端口
还是
post
。在你的问题中,它被命名为
post
,我认为它是一个打字错误。也许你将它存储在elasticsearch中作为
post
,因此找不到
端口
。是的,这确实是打字错误,谢谢。你可以就无痛部分提出建议吗问题的原因是,这基本上是这个问题的主要思想,或者说可能有一些更简单的方法,比如使用for循环来获得相同的结果。老实说,我来自python世界,我很难完全理解您的代码——您的解决方案的片段对我来说很有意义。
it.value.heap_used*.toBigDecimal().sum()
[[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:523.045, heap_max:1212.15, times:2017-10-08T07:26:21.050Z]]