java—标识.csv文件的第一行和最后一行

ee7vknir  于 2021-05-30  发布在  Hadoop
关注(0)|答案(0)|浏览(401)

我正在编写一个mapreduce代码,它将读取第一行、最后一行,并向字符串中添加一些内容,其余的行按原样打印。我用的是 .csv 文件和提取选项卡。但是,如果我像普通java程序一样使用它,那么我将如何在mr代码的mapper部分使用这个逻辑,因为我不理解mapper是如何查找行的。下面是代码。

public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        String next, fileContent = value.toString();
        for (boolean first = true, last = (fileContent == null); !last; first = false, fileContent = next) {
            last = ((next = value.toString()) == null);
        if (first) {
         String[] tab = fileContent.split(",");
         String line = "var trip = [" + "\n" + "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "],";
         text.set(line);
        }
        else if (last) {
         String[] tab = fileContent.split(",");
         String line = "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "]" + "\n" + "];";
        text.set(line);
        }
        else {
         String[] tab = fileContent.split(",");
         String line = "[" + tab[2] + "," + tab[3] + "," + tab[8] + "," + tab[11] + "," + tab[15] + "," + tab[16] + "],";
        text.set(line);
        }
        }
        context.write(null, text);
        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题