Java 如何在mapreduce中将数组中的字符串附加到Text()
我有一个数组中的字符串。我需要根据索引将数组的所有元素附加到mapreduce中的Java 如何在mapreduce中将数组中的字符串附加到Text(),java,hadoop,mapreduce,Java,Hadoop,Mapreduce,我有一个数组中的字符串。我需要根据索引将数组的所有元素附加到mapreduce中的Text()。我需要将这些值作为字符串附加到Text()中 例如: String s = "12,23"; String[] array = s.split(","); Text t1 = new Text(); for(int i=0;i<array.length;i++) { t1.append(array[i]); } String s=“12,23”; 字符串[]数组=s.spl
Text()
。我需要将这些值作为字符串附加到Text()中
例如:
String s = "12,23";
String[] array = s.split(",");
Text t1 = new Text();
for(int i=0;i<array.length;i++) {
t1.append(array[i]);
}
String s=“12,23”;
字符串[]数组=s.split(“,”);
Text t1=新文本();
对于(int i=0;i您需要使用此方法:append(byte[]utf8,int start,int len)
()
因此,您的代码看起来像:
t1.append(array[i].getBytes(), 0, array[i].getBytes().length);
作为快速单元测试:
@Test
public void textAppendTest() {
String s = "12,23";
String[] array = s.split(",");
Text t1 = new Text();
for(int i=0;i<array.length;i++){
t1.append(array[i].getBytes(), 0, array[i].getBytes().length);
}
System.out.println(t1); // prints 1223
}
@测试
公共void textAppendTest(){
字符串s=“12,23”;
字符串[]数组=s.split(“,”);
Text t1=新文本();
对于(int i=0;i您需要使用此方法:append(byte[]utf8,int start,int len)
()
因此,您的代码看起来像:
t1.append(array[i].getBytes(), 0, array[i].getBytes().length);
作为快速单元测试:
@Test
public void textAppendTest() {
String s = "12,23";
String[] array = s.split(",");
Text t1 = new Text();
for(int i=0;i<array.length;i++){
t1.append(array[i].getBytes(), 0, array[i].getBytes().length);
}
System.out.println(t1); // prints 1223
}
@测试
公共void textAppendTest(){
字符串s=“12,23”;
字符串[]数组=s.split(“,”);
Text t1=新文本();
对于(int i=0;i您可以这样做,而不需要拆分字符串数组:
String str = "12,23";
String newStr = str.replace("," , ""); // or replace with any character or string you want between those numbers
Text text = new Text(newStr);
这样做不需要拆分字符串数组:
String str = "12,23";
String newStr = str.replace("," , ""); // or replace with any character or string you want between those numbers
Text text = new Text(newStr);
我不想在这个方法中做,我必须基于索引访问我不想在这个方法中做,我必须基于索引访问我也尝试了..我用0替换了start.它对我不起作用:(我也尝试了..我用0替换了start.它对我不起作用:(