Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过MapReduce的第二个选项卡拆分单词?_Java_String_Hadoop_Mapreduce - Fatal编程技术网

Java 如何通过MapReduce的第二个选项卡拆分单词?

Java 如何通过MapReduce的第二个选项卡拆分单词?,java,string,hadoop,mapreduce,Java,String,Hadoop,Mapreduce,我正在做一些网络数据的MapReduces。(我是MapReduce的新手,所以想想经典的WordCount类型的东西。)输入文件如下,数字后跟一个制表符: 3 2 2 4 2 2 3 3 虽然我知道如何获得数字的经典“字数”,但我真正想做的是成对地计算数字,这样映射器就可以读取上面的“32”、“22”、“24”、“22”等等。我该怎么做?我想所有需要做的就是调整StringTokenizer,以通过第二个制表符或其他方式拆分单词,但是我该怎么做呢?这可能吗 下面是我正在使用的Java代码,到目

我正在做一些网络数据的MapReduces。(我是MapReduce的新手,所以想想经典的WordCount类型的东西。)输入文件如下,数字后跟一个制表符:

3 2 2 4 2 2 3 3

虽然我知道如何获得数字的经典“字数”,但我真正想做的是成对地计算数字,这样映射器就可以读取上面的“32”、“22”、“24”、“22”等等。我该怎么做?我想所有需要做的就是调整StringTokenizer,以通过第二个制表符或其他方式拆分单词,但是我该怎么做呢?这可能吗

下面是我正在使用的Java代码,到目前为止,它只是MapReduce中的经典字数示例:

public static class TokenizerMapper
   extends Mapper<Object, Text, Text, IntWritable>{

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(Object key, Text value, Context context
                ) throws IOException, InterruptedException {
  StringTokenizer itr = new StringTokenizer(value.toString());
  while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    context.write(word, one);
  }
}
}
公共静态类TokenizerMapper
扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共无效映射(对象键、文本值、上下文
)抛出IOException、InterruptedException{
StringTokenizer itr=新的StringTokenizer(value.toString());
而(itr.hasMoreTokens()){
set(itr.nextToken());
上下文。写(单词,一);
}
}
}

您可以轻松地修改字数,以获得预期的行为

public static class TokenizerMapper 
   extends Mapper<Object, Text, Text, IntWritable>{

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(Object key, Text value, Context context
                ) throws IOException, InterruptedException {
  StringTokenizer itr = new StringTokenizer(value.toString());
  while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    String myString = word.toString();
    String [] numbers = myString.split("\t"); // split by tab
    if (numbers.length> 2)
    {
        // you need at least two numbers to make one pair
        int first = Integer.parseInt(numbers[0]);
        int second;
        for (int i=1; i < numbers.length; ++i)
        {
           second = Integer.parseInt(numbers[i]);
           Text keynew = new Text(first+"\t"+second);
           context.write(keynew, one);
           // your second will be the first in the next loop iteration
           first = second;
        }
    }
  }
}
}
公共静态类TokenizerMapper
扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共无效映射(对象键、文本值、上下文
)抛出IOException、InterruptedException{
StringTokenizer itr=新的StringTokenizer(value.toString());
而(itr.hasMoreTokens()){
set(itr.nextToken());
String myString=word.toString();
String[]numbers=myString.split(“\t”);//按选项卡拆分
如果(数字长度>2)
{
//一对至少需要两个数字
int first=Integer.parseInt(数字[0]);
int秒;
对于(int i=1;i
试试这个:

String data = "0\t0\t1\t2\t4\t5\t3\t4\t6\t7";
String[] array = data.split("(?<=\\G\\w{1,3}\t\\w{1,3})\t");

    for(String s : array){
        System.out.println(s);
    }
对于您的代码

String[] pairsArray = value.toString().split("(?<=\\G\\w{1,3}\t\\w{1,3})\t");
for (String pair : pairsArray) {
     context.write(new Text(pair), one);
}

String[]pairsArray=value.toString().split((?感谢所有帮助!这就是我提出的解决方案(在添加一些前导零以帮助格式化之后):

公共类字数{
公共静态类令牌映射器
扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共无效映射(对象键、文本值、上下文
)抛出IOException、InterruptedException{
字符串数据=value.toString();
对于(int i=0;i<(data.length()/3)-1;i++){
字符串对=数据。子字符串(i*3,(i*3)+5);
编写(新文本(对),一个);
}
}
}

这看起来可以工作,有一些小错误,所以我更正了它们,然后运行了它,但它实际上什么都没写。知道为什么吗?@user3222006尝试将“first”和“second”变量的数据类型更改为String,并删除parseInt(数据中可能有非数字的内容…),并确保选项卡是分隔符。@user3222006顺便说一句,脚本确实在输出文件夹中生成了_SUCCESS文件?对吗?我不知道,不能真正访问输出文件夹,说里面什么都没有?但是,这会显示:“15/02/28 14:39:28 INFO mapreduce.Job:Job Job\u local619852621\u 0001已成功完成然后是写了多少字节,等等。我也尝试过将变量改为字符串,但不幸的是没有骰子。还有其他想法吗?非常感谢您迄今为止的帮助。通常,“写的字节”和“读的字节”mapreduce输出最底部的部分让我知道“好吧,它成功读取了文件”和“好吧,它什么都没写”:15/02/28 14:39:28信息mapreduce.Job:map 100%reduce 100%15/02/28 14:39:28信息mapreduce.Job:Job\u local619852621\u 0001成功完成……文件输入格式计数器字节Read=12581691 File Output Format Counters Bytes Write=0我的意思是我了解如何在真空中拆分选项卡上的字符串,我想这里更大的问题是如何将其集成到所有MapReduce的类和迭代器中。例如,它们都去了哪里?您可以将其用于映射任务。例如value.toString().split…此代码段的特殊之处在于它的正则表达式,它按您想要的第二个制表符拆分字符串。现在您可以在代码中迭代对数组。如果您还需要其他内容,请告诉我。不过,这就是问题所在。value.toString().split未编译,获取如下错误:WordCount.java:26:error:未找到StringTokenizer(String[])StringTokenizer itr=new StringTokenizer(value.toString().split()(?您不应该使用StringTokenizer。我已经修改了代码。请检查。这最终非常有用。最终,我不得不放弃.split(),因为我正在寻找重叠的子字符串,但意识到我不需要StringTokenizer是关键。非常感谢。
String[] pairsArray = value.toString().split("(?<=\\G\\w{1,3}\t\\w{1,3})\t");
for (String pair : pairsArray) {
     context.write(new Text(pair), one);
}
 public class WordCount {

   public static class TokenizerMapper
        extends Mapper<Object, Text, Text, IntWritable>{

     private final static IntWritable one = new IntWritable(1);
     private Text word = new Text();

     public void map(Object key, Text value, Context context
                ) throws IOException, InterruptedException {
         String data = value.toString();
         for (int i = 0; i < (data.length() / 3) - 1; i++) {
             String pair = data.substring(i*3, (i*3)+5);
             context.write(new Text(pair), one);
         }
      }
   }