![](/img/trans.png)
[英]Copy local file to Unix Server using java Code on windows machine?
[英]bulk of file copy from local to DBFS server using multithreading in java
我需要將一些共享網絡路徑中的大量文件(例如100 gbs)上傳到數據庫DBFS服務器。 我正在使用下面的代碼,它工作正常。 但是我需要通過使用JSch和ExecutorService類線程池使用多線程概念來加快過程
看到我下面的代碼
public static void sftpConnection()throws JSchException, SftpException, IOException {
System.out.println("Inside sftpConnection method");
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
String user = "oracle";
// String host ="";
String host ="";
Integer port =;
String password ="";
String Folder ="";
String outputDir ="";
String filema =".txt";
ChannelSftp sftpChannel = null;
try
{
session = jsch.getSession(user, host, port);
System.out.println("Afer getting Session"+session);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
System.out.println("Set Password");
session.setConfig("PreferredAuthentications","publickey,keyboard-interactive,password");
session.connect();
System.out.println("Connection Successfull");
channel = session.openChannel("sftp");
channel.connect();
System.out.println("Channel Connection Succesfull");
sftpChannel = (ChannelSftp)channel;
File[] files = findFile(Folder, filema);
for(File file : files)
{
putFile(file, sftpChannel, outputDir);
}
File dir = new File("");
dir.mkdirs();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if (sftpChannel != null) {
sftpChannel.exit();
}
session.disconnect();
}
}
誰能幫我實現JSch和ExecutorService來處理從服務器到DBFS服務器的100gbs數據。 提前致謝
輕松使用Java8:
Arrays.stream(files).parallel()
.forEach((f) -> putFile(f, sftpChannel, outputDir));
請注意, sftpChannel
必須是線程安全的,才能正常工作,這可能不會使您提高性能。 我會為每個put
操作創建一個通道,或使用連接池為您管理此操作。
您可以通過更改公共forkjoin池的大小來控制所使用的並行度,如下所示(作為JVM參數傳遞):
-Djava.util.concurrent.ForkJoinPool.common.parallelism=120
這將創建一個具有120個線程的線程池。
多線程可以通過使用多個線程並行化計算來幫助加快計算密集型過程。 但是簡單的文件復制不占用大量計算資源,而是I / O大量資源。
在多個線程上運行對您無濟於事。 網絡帶寬是相同的,而不管您嘗試向其中注入管道的數量如何。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.