3

我有一个 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])
}

此脚本的输出如下所示:

[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:533.6, heap_max:1212.15, times:2017-10-08T07:26:21.050Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:526.72, heap_max:1212.15, times:2017-10-08T07:26:11.042Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:518.82, heap_max:1212.15, times:2017-10-08T07:25:51.047Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:515.73, heap_max:1212.15, times:2017-10-08T07:25:31.055Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:525.69, heap_max:1212.15, times:2017-10-08T07:26:01.047Z]
[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:517.71, heap_max:1212.15, times:2017-10-08T07:25:41.041Z]

从输出中可以看出 - >我基本上有6条与时间戳不同的相同行。HeapSize 和 Max HeapSize 是不同的,但这并不重要。

由于集群对于所有六个条目 /cluster1/ 都是相同的,我认为它是一个输出。理想情况下,我想应用某种unique()函数,它可以为我提供一行作为输出

如下所示:

[cluster:cluster1, port:4344, hostname:cluster1.com, heap_used:523.0450, heap_max:1212.15, times:2017-10-08T07:25:41.041Z]

其中 heap_used 是 6 个值的平均值以及 heap_max。我知道在 python pandas 中我可以用一个命令来完成它。但是我不知道 groovy,我一直在互联网上搜索。

编辑:不幸的是,Groovy 解决方案不会将 1:1 转移到 Painless。

4

1 回答 1

2

您可以results通过以下方式处理您的列表:

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()
        ]}

首先,我们将所有列表元素按包含cluster,port和的三元组分组hostnamecluster然后我们通过组合,porthostname,heap_used: avg(heap_used)heap_max: avg(heap_max)来收集所有条目times: max(times)

这里

it.value.heap_used*.toBigDecimal().sum()

我们获取所有heap_used值的列表 ( it.value.heap_used),然后使用扩展运算符应用于.toBigDecimal()每个列表元素,因为您的初始值表示为字符串。为了计算平均值,我们只需将所有heap_used值的总和除以列表的大小。

输出

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

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