我有這樣的數據。總計豬的每n個行值
1:23:0.20
2:34:0.50
3:67:0.90
4:87:0.10
5:23:0.12
我想總結每2行最後一列值這樣。
0.20+0.50 = 0.70
0.90+0.10 = 1.0
,並打印這樣
1:23:0.20:0.70
2:34:0.50:0.70
3:67:0.90:1.0
4:87:0.10:1.0
5:23:0.12
這是我的豬腳本
data = LOAD '/home/user/Documents/test/test.txt' using PigStorage(':') AS (tag:int,rssi:chararray,weightage:chararray,seqnum:int);
B = FOREACH (GROUP data ALL) {
A_ordered = ORDER data BY rssi;
GENERATE FLATTEN(CUSTOM_UDF(A_ordered));
}
我試圖用java UDF。但不能正常工作。
this is what I tried.
public List<String> sumValues() {
List<String> processedList = new ArrayList<>();
if (entries == null) {
return processedList;
} else {
double columnSum = 0;
List<String> tempList = new ArrayList<>();
int length = entries.size();
for (int index = 1; index <= length; index++) {
tempList.add(entries.get(index - 1));
String[] splitValues = entries.get(index - 1).split(DELIMITER);
if (splitValues.length >= MIN_SPLIT_STRING_LENGTH) {
try {
double lastValue = Double.parseDouble(splitValues[WEIGHTAGE_INDEX]);
columnSum = columnSum + lastValue;
if ((index % ROWS_TO_BE_SUMMED == 0) || (index == length)) {
for (String tempString : tempList) {
processedList.add(tempString + ":" + columnSum);
}
tempList.clear(); // Clear the temporary array
columnSum = 0;
}
} catch (NumberFormatException e) {
System.out.println("Invalid weightage");
}
} else {
System.out.println("Invalid input");
}
}
}
return processedList;
}
@Override
public String exec(Tuple input) throws IOException {
System.out.println("------INSIDE EXEC FUCTION ----" + input);
if (input != null && input.size() != 0) {
try {
String str = (String) input.get(0);
if (str != null) {
String splitStrings[] = str.split(":");
if (splitStrings != null && splitStrings.length >= 3 && splitStrings[2].equals(EXIT)) {
List<String> processedList = sumValues();
String sum = processedList.toString();
System.out.println("SUM VALUE----:" + sum);
return sum;
} else {
System.out.println("INPUT VALUE----:" + str);
entries.add(str);
return null;
}
}
} catch (Exception e) {
return null;
}
}
return null;
}
}
上面的代碼打印空結果。 任何幫助將不勝感激。
不打印空結果 –