[英]How to upload and download file to hdfs using Java code
i am newbie to hadoop, and trying to upload and download files to hdfs via. 我是hadoop的新手,并尝试通过上传和下载文件到hdfs。 Java code. Java代码。 which should behave as 应该表现为
Data Uploading: 资料上传:
hadoop fs -put or -copyFromLocal filename directoryName
and Data Downloading 和数据下载
hadoop fs -get or -copyToLocal filename directoryName
from hdfs. 从hdfs。 i need this one because datasets contain image, audio, video etc file. 我需要这个,因为数据集包含图像,音频,视频等文件。 above command works fine with all type of data, if i try using Java i/o reader code , it is working fine for text files , but not for images, video. 上面的命令适用于所有类型的数据,如果我尝试使用Java I / O读取器代码,则它适用于文本文件,但不适用于图像,视频。 docx etc.. docx等。
pls any help here. 请在这里提供任何帮助。
Edited Here: 在这里编辑:
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Configuration conf=new Configuration();
FileSystem fs = FileSystem.get(conf);
Path path=new Path("data");
Path file=new Path(path,"screenshots.png");
BufferedImage image = ImageIO.read(new File("/home/hduser/Desktop/screenshots.png"));
if (!fs.exists(path))
throw new IOException("Output not found!");
ImageIO.write(image, "png", fs.open(path));
}
As asked i have edited here code that i am using to upload image file to hdfs. 根据要求,我在这里编辑了用于将图像文件上传到hdfs的代码。 here ImageIO.write
is not accepting arguement fs.open(path)
, because is asking for file, but i have to give path here as to read and to write to hdfs we need to give path only. 这里ImageIO.write
不接受论据fs.open(path)
,因为它正在请求文件,但是我必须在这里提供路径以读取和写入hdfs,我们只需要给出路径即可。 Actually i am in need of a method to upload and download file from hdfs using code for all type of data, so i should not write code and use plugins for all type of file. 实际上,我需要一种使用代码针对所有类型的数据从hdfs上传和下载文件的方法,因此我不应该编写代码并针对所有类型的文件使用插件。
ImageIO.write can take an OutputStream as well as a File. ImageIO.write可以使用OutputStream以及File。 However, fs.open is returning an InputStream because it is for reading files only. 但是,fs.open返回InputStream,因为它仅用于读取文件。
You need to call: 您需要致电:
ImageIO.write(image, "png", fs.create(file));
The create method will return an OutputStream
which ImageIO can write to. create方法将返回ImageIO可以写入的OutputStream
。
http://hadoop.apache.org/docs/r2.2.0/api/org/apache/hadoop/fs/FileSystem.html http://hadoop.apache.org/docs/r2.2.0/api/org/apache/hadoop/fs/FileSystem.html
path
is already exists than you will overwrite that file with your image. 如果path
已经存在,那么您将用映像覆盖该文件。 I think that you want to save your image into some existing folder in HDFS
. 我认为您想将图像保存到HDFS
某些现有文件夹中。 In that case you need to write your image to new Path(path, "SomeImageName.png");
在这种情况下,您需要将图像写入new Path(path, "SomeImageName.png");
. 。 You don't need to use ImageIO
to copy the image from local file system to HDFS
. 您无需使用ImageIO
将映像从本地文件系统复制到HDFS
。 Try to use copyFromLocalFile
method of FileSystem
: 尝试使用FileSystem
copyFromLocalFile
方法:
fs.copyFromLocalFile(new Path("/home/hduser/Desktop/screenshots.png"), path); fs.copyFromLocalFile(new Path(“ / home / hduser / Desktop / screenshots.png”),路径);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.