DAY 30
0
Elastic Stack on Cloud

# Scripted Metric

init_script→map_script→combine_script→reduce_script

## params

1.撰寫init_script和params

``````"init_script": "state.transactions = new HashMap();"
``````

``````#這邊用json大概示意一下
{
"資工一1": [77, 80], #第1,2位同學的平均
"資工一2": [86, 60]
}
``````

``````"params": {"add_point": 10},#上面提到加10分
``````

2.撰寫map_script

``````"map_script": """
def class = doc['class'].value;        #拿值
ArrayList tmp = null;
#判斷state.transactions內有無班級，有則直接存取，沒有則新建
if(!state.transactions.containsKey(class)){
tmp = new ArrayList();
state.transactions.put(class, tmp);
} else {
tmp = state.transactions.get(class);
}

#判斷小於60加分
if (math < 60){
if (math > 50){
math = 60;
}else{
}
}
if (mand < 60){
if (mand > 50){
mand = 60;
}else{
}
}
if (eng < 60){
if (eng > 50){
eng = 60;
}else{
}
}
if (soc < 60){
if (soc > 50){
soc = 60;
}else{
}
}
#計算平均且加入state.transactions
long avg = 0;
avg = (math + mand + eng + soc) / 4;
"""
``````

3.撰寫combine_script

``````"combine_script": "return state.transactions"
``````

4.撰寫reduce_script

Debug.explain

``````"reduce_script": """
Debug.explain(states);
"""
``````
``````"to_string" : "[{資工一1=[77, 77]}, {資工一2=[64, 94], 資工一1=[94]}, {資工一2=[56]}]"
``````

``````"reduce_script": """
#先合併成績，整理成新的資料結構
while (states.size() > 0){
def data_buffer = states.remove(0);
for(class in data_buffer.keySet()){
Map class_tmp = null;
class_tmp = new HashMap();
class_tmp.put("s", 0);
class_tmp.put("n", 0);
for(s in data_buffer.get(class)){
class_tmp.s += s;
class_tmp.n +=1;
}
}else{
for(s in data_buffer.get(class)){
class_tmp.s += s;
class_tmp.n +=1;
}
}
}
}
#根據新的資料結構算出平均
Map result = new HashMap();
long avg = 0;
avg = data.s / data.n;
result.put(class, avg);
}
return result;
"""
``````

``````{
"aggs": {
"class_avg": {
"scripted_metric": {
"params": {
},
"init_script": "state.transactions = new HashMap();",
"map_script": """
def class = doc['class'].value;
ArrayList tmp = null;

if(!state.transactions.containsKey(class)){
tmp = new ArrayList();
state.transactions.put(class, tmp);
} else {
tmp = state.transactions.get(class);
}
if (math < 60){
if (math > 50){
math = 60;
}else{
}
}
if (mand < 60){
if (mand > 50){
mand = 60;
}else{
mand += 10;
}
}
if (eng < 60){
if (eng > 50){
eng = 60;
}else{
eng += 10;
}
}
if (soc < 60){
if (soc > 50){
soc = 60;
}else{
soc += 10;
}
}
long avg = 0;
avg = (math + mand + eng + soc) / 4;
""",
"combine_script": "return state.transactions",
"reduce_script": """
while (states.size() > 0){
def data_buffer = states.remove(0);
for(class in data_buffer.keySet()){
Map class_tmp = null;
class_tmp = new HashMap();
class_tmp.put("s", 0);
class_tmp.put("n", 0);
for(s in data_buffer.get(class)){
class_tmp.s += s;
class_tmp.n +=1;
}
}else{
for(s in data_buffer.get(class)){
class_tmp.s += s;
class_tmp.n +=1;
}
}
}
}
Map result = new HashMap();
long avg = 0;
avg = data.s / data.n;
result.put(class, avg);
}
return result;
"""
}
}
}
}
``````

``````"aggregations" : {
"class_avg" : {
"value" : {
"資工一1" : 82,
"資工一2" : 72
}
}
}
``````