gpt4 book ai didi

java - 将普通的Java代码转换为hadoop代码而不使用mapreduce?

转载 作者:行者123 更新时间:2023-12-02 21:48:22 26 4
gpt4 key购买 nike

我目前正在使用hadoop。我想将我的Java代码转换为hadoop。我希望我的代码可以与hdfs一起使用。即我的代码是普通文件系统,我希望它与hdfs(hd文件系统)一起工作。我希望以下代码在hadoop(hd文件系统)中工作。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPExample {

public static void gzip() {
int i = new File("/media/0052ADF152ADEC1A/all splits").list().length;
System.out.println(i + "here");
while (i > 0) {
String file = "/media/0052ADF152ADEC1A/all splits/File" + i + ".txt";
String gzipFile = "/media/0052ADF152ADEC1A/compress/Filegz" + i + ".gz";
String newFile = "/media/0052ADF152ADEC1A/all/hadoop ebooks/test1.txt";

compressGzipFile(file, gzipFile);

decompressGzipFile(gzipFile, newFile);
i--;
}
}

private static void decompressGzipFile(String gzipFile, String newFile) {
try {
FileInputStream fis = new FileInputStream(gzipFile);
GZIPInputStream gis = new GZIPInputStream(fis);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int len;
while ((len = gis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
//close resources
fos.close();
gis.close();
} catch (IOException e) {
e.printStackTrace();
}

}

private static void compressGzipFile(String file, String gzipFile) {
try {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(gzipFile);
GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
gzipOS.write(buffer, 0, len);
}
//close resources
gzipOS.close();
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}

最佳答案

我建议您开始阅读并理解MapReduce代码的外观以及它必须实现的功能:

https://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html

然后,您将了解没有“转换”。在非常高级的 View 中,MapReduce Java代码是在两个阶段中构造代码的一种方式:Map和Reduce

以您的示例为例,您似乎想使用并行计算来解压缩大量文件,因此您应该尝试将MapReduce代码编写为解压缩文件的两步过程。抱歉,但我从未遇到过压缩算法。

关于java - 将普通的Java代码转换为hadoop代码而不使用mapreduce?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23263727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com