I have a mapper that emit key/value pairs(composite keys and composite values separated by comma).
e.g
key: a,b,c,d Value: 1,2,3,4,5
key: a1,b1,c1,d1 Value: 5,4,3,2,1
...
...
key: a,b,c,d Value: 5,4,3,2,1
I could easily SUM these values using reduceByKey.
e.g
reduceByKey(new Function2<String, String, String>() {
#Override
public String call(String value1, String value2) {
String oldValue[] = value1.toString().split(",");
String newValue[] = value2.toString().split(",");
int iFirst = Integer.parseInt(oldValue[0]) + Integer.parseInt(newValue[0]);
int iSecond = Integer.parseInt(oldValue[1]) + Integer.parseInt(newValue[1]);
int iThird = Integer.parseInt(oldValue[2]) + Integer.parseInt(newValue[2]);
int iFourth = Integer.parseInt(oldValue[3]) + Integer.parseInt(newValue[3]);
int iFifth = Integer.parseInt(oldValue[4]) + Integer.parseInt(newValue[4]);
return iFirst + "," + iSecond + ","
+ iThird+ "," + iFourth+ "," + iFifth;
}
});
But the problem is how do I find average of just one of these values. Lets assume I want to SUM iFirst, iSecond, iThird and iFourth but I want to find Average of iFifth. How do i do it? With a simple key/value pairs I could use mapValues function but not sure how I could do it with my example. Please advice.
I've used foldByKey function to resolve this issue.
Related
I want to get the desired output from the below logic. The output should be a key-value pair.
I'm looping the apiList and appending the apiList <value> to input details <key>. If input details contains v1, the key is appended as (success_api.tenantId.host.v1:http):
class azureConfig {
public static void main(String[] args){
String inputDetails = """version=v1 host=http port=80"""
def inputList = inputDetails.split("\\s")
def id= "tenantId"
def apiList = [{api = success_api}, {api = fail_api}]
def keyValue = ""
for(String item: inputList){
def path = item.split("=")
keyValue += id + "." + path[0] + "::" + path[1] + "\n"
}
println(keyValue)
}
}
Output should be:
success_api.tenantId.host.v1::http
success_api.tenantId.port.v1::80
fail_api.tenantId.host.v1::http
fail_api.tenantId.port.v1::80
I have got this ouput for above logic:
tenantId.version::v1
tenantId.host::http
tenantId.port::80
final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
This email string convert to this patter like
Fox Example
email -> ab****j#email.com
phoneNumber -> (012)3****89
Please help using RegExp and 0ther technics are most welcome.
Try this way
void main() {
var result = 'nilesh.rathod#gmail.com'.replaceAll(new RegExp('(?<=.)[^#](?=[^#]*?[^#]#)'), '*');
print(result);
}
You can use the replaceRange method
final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
final hiderPlaceholder = "****";
final censuredEmail = email.replaceRange(2, email.indexOf("#")-1, hiderPlaceholder);
final censuredPhoneNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3).replaceRange(1, phoneNumber.substring(3).length-2, hiderPlaceholder);
print (censuredEmail);
print (censuredPhoneNumber);
Or you can just go for the evergreen substring
final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
final hiderPlaceholder = "****";
final censuredEmail = email.substring(0, 2) + hiderPlaceholder + email.substring(email.indexOf("#")-1);
final censuredPhoneNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 4) + hiderPlaceholder + phoneNumber.substring(phoneNumber.length-2);
print (censuredEmail);
print (censuredPhoneNumber);
P.s. obviously, add all the controls you want, e.g. for the length of the email/phone number
Map attributeMap = new TreeMap<>();
attributeMap.put("C","FIRSTNAMe");
attributeMap.put("C2","LASTNAMe");
attributeMap.put("C3","1111");
attributeMap.put("C4","ABCNAMe");
How to make single String of above example
output is c,c2,c3,c4 and FIRSTNAMe,LASTNAMe,'1111',ABCNAMe
Something like this:
Predicate<String> predicate = "1111"::equals;
String left = attributeMap.keySet().stream().map(String::toLowerCase).collect(Collectors.joining(","));
String right = attributeMap.values().stream().map(x -> (predicate.test(x) ? "'" + x + "'" : x)).collect(Collectors.joining(","));
System.out.println(left + right);
Wanted to check if there is any null value in column field in the csv file and also there shouldn't be null value after / in number column, if it is null the entire row should not be written to output file.
name,number,gender,country
iva 1/001 f Antartica
aaju 2/002 m russia
lax 3/ m brazil
ana 4/004 f Thailand
vis 5/005 m
for e.g. 3rd and 5th row should not be written to output file.
using (StreamWriter file = new StreamWriter(filepathop)) {
for (int i = 0; i < csv.Length; i++) {
{
if (i == 0) {
file.WriteLine(header + "," + "num" + "," + "serial" + "," + "date");
}
else {
var newline = new StringBuilder();
string[] words = csv[i].Split(',');
string[] no = words[1].Split('/');
string number = no[0];
string serial = no[1];
newline.Append(number + "," + serial + "," + tokens[0]);
file.WriteLine(csv[i] + "," + newline);
}
}
}
}
}
}
}
You can test for null columns with string.IsNullOrEmpty(column) or column.Length == 0 like so:
if (!string.IsNullOrEmpty(serial) && !string.IsNullOrEmpty(country))
file.WriteLine(csv[i] + "," + newline);
You might want to check and remove white space, too. Depends on your input.
basic loadstrings() code:
String[] txt = loadStrings("/Users/rjth/Desktop/data.txt");
println("there are " + txt.length + " lines");
println(txt);
when I add text(); or RG.getText(); I am given an error code:
String[] txt = loadStrings("/Users/rjth/Desktop/data.txt");
println("there are " + txt.length + " lines");
println(txt);
text(txt, 1, 1);
error: The method text(char[], int, int float, float) in the type PApplet is not applicable for the arguments (String[], int, int)
where is the problem?
well that's because txt as you set it is an array (that's what loadstrings gives out).. You have to iterate over it and set the text to each one of its elements like this:
for(int i = 0; i < txt.length; i++) text(txt[i],1,1);
or:
for(String s: txt) text(s,1,1);