WEBHDFS REST API to copy/move files from windows server/local folder/desktop to HDFS
We can copy files from windows file system to HDFS by using scp command.
scp source_file_name user@/path/file_name
and also we can achieve this by using winscp tool. you can install it and establish a connect to hdfs server then files can be transfer.
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;import jcifs.smb.NtlmPasswordAuthentication;import jcifs.smb.SmbFile;import jcifs.smb.SmbFileInputStream;public class FileWriteToHDFS { public static void main(String[] args) throws Exception { String src = args[0]; String dest = args[1]; Console console = System.console(); String username = console.readLine("Username: "); String password = new String(console.readPassword("Password: ")); String domain = console.readLine("Authentication Domain: "); NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domain, username, password); SmbFile srcSMB = new SmbFile (src, auth); InputStream in = new SmbFileInputStream(srcSMB); Configuration myConf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(dest), myConf); OutputStream out = fs.create(new Path(dest)); try { IOUtils.copyBytes(in, out, 4096, false); } catch(IOException e) { e.printStackTrace(); } finally { IOUtils.closeStream(in); } }}
This code uses JCIFS to copy through smb protocol to HDFS