简体   繁体   English

如何使用Java代码将文件上传和下载到hdfs

[英]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

  1. If 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"); .
  2. 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.

相关问题 Java客户端如何将文件上传到远程HDFS服务器或从远程HDFS服务器下载文件? - How a Java client upload/download a file to/from remote HDFS server? 在 Java 上使用 DFSClient 将文件上传到 HDFS - Upload file to HDFS using DFSClient on Java 如何使用纯 Java 生成 Parquet 文件(包括日期和十进制类型)并上传到 S3 [Windows](无 HDFS) - How to Generate Parquet File Using Pure Java (Including Date & Decimal Types) And Upload to S3 [Windows] (No HDFS) 如何使用 Java 查找文件是否存在于 hdfs 中? - How to find if the file exists in hdfs using Java? 使用Java代码读取存储在hdfs中的.properties文件 - reading a .properties file stored in hdfs using java code 使用Java将文件写入HDFS - Writing file to HDFS using Java 如何通过JAVA代码从CQ DAM /存储库上传和下载任何文件 - How to upload and download any file from CQ DAM/repository through JAVA code 如何运行 hdfs cat 命令并使用 Java 读取 output 文件 - How to run a hdfs cat command and read output file using Java java 本地上传下载文件 - java local upload and download file 如何使用 Java 代码启动 Fuseki 服务器并使用 java 代码将 OWL 文件上传到它? - How to start Fuseki server using Java code and upload OWL file to it using java code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM