使用Java将文件移动到HDFS

使用Java将文件移动到HDFS,java,hadoop,hdfs,text-files,Java,Hadoop,Hdfs,Text Files,如何使用Java在hdfs中执行hadoop put文件?有可能吗 使用此语句: public abstract boolean rename(Path src, Path dst) throws IOException ? 谢谢 试试这个: //Source file in the local file system String localSrc = args[0]; //Destination file in HDFS String dst = args[1]; //Input str

如何使用Java在hdfs中执行hadoop put文件?有可能吗

使用此语句:

public abstract boolean rename(Path src, Path dst) throws IOException
?

谢谢

试试这个:

//Source file in the local file system
String localSrc = args[0];
//Destination file in HDFS
String dst = args[1];

//Input stream for the file in local file system to be written to HDFS
InputStream in = new BufferedInputStream(new FileInputStream(localSrc));

//Get configuration of Hadoop system
Configuration conf = new Configuration();
System.out.println("Connecting to -- "+conf.get("fs.defaultFS"));

//Destination file in HDFS
FileSystem fs = FileSystem.get(URI.create(dst), conf);
OutputStream out = fs.create(new Path(dst));

//Copy file from local to HDFS
IOUtils.copyBytes(in, out, 4096, true);
您应该能够使用:

检查这个
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path localPath = new Path("path/to/local/file");
Path hdfsPath = new Path("/path/in/hdfs");
fs.copyFromLocalFile(localPath, hdfsPath);