1. 程式人生 > >將一臺伺服器上的檔案放在另一臺伺服器 的hdfs上

將一臺伺服器上的檔案放在另一臺伺服器 的hdfs上

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 org.springframework.stereotype.Service;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
/**
* Created by panghu on 2017/9/13. */ @Service public class FileManageService { /** * 將一個伺服器上的檔案放到另一個伺服器的hdfs* @param local * @param target * @throws IOException */ public static void fileUp(String local ,String target) throws IOException { URL url = new URL(local); HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設定超時間為3conn.setConnectTimeout(3*1000); //防止遮蔽程式抓取而返回403錯誤 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //得到輸入流 InputStream in = conn.getInputStream(); System.out.println(target); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create
(target), conf); OutputStream out = fs.create(new Path(target)); IOUtils.copyBytes(in, out, 4096, true); System.out.println("上傳完成。。。。。。。"); } /** * 上傳本地檔案到hdfs * @param local * @param remote * @throws IOException * * */ public static String copyFile(String uri , String local, String remote) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri), conf); fs.copyFromLocalFile(new Path(local), new Path(remote)); System.out.println("copy from: " + local + " to " + remote); fs.close(); return "111"; } public static void main(String[] args) { System.out.println(1234); } }