[英]How to read first line in Hadoop (HDFS) file efficiently using Java?
[英]How make directory to file in HDFS Hadoop (Cloudera) java
我首先使用Hadoop程序集Cloudera。 我需要使用HDFS在Linux服務器中建立文件目錄。 但是,當我在cmd Linux中使用Java時,它不會創建目錄,而是創建文件。 您能幫我做我做的不好的事嗎,我可以在我的代碼java附近在hdfs和file中創建目錄。謝謝。 對不起,英語不好)
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FileDemo {
public static void main(String args[]) throws IOException {
Scanner reader = new Scanner(System.in);
boolean success = false;
System.out.println("Enter path of directory to create");
String dir = reader.nextLine();
// Creating new directory in Java, if it doesn't exists
File directory = new File(dir);
if (directory.exists()) {
System.out.println("Directory already exists ...");
} else {
System.out.println("Directory not exists, creating now");
success = directory.mkdir();
if (success) {
System.out.printf("Successfully created new directory : %s%n", dir);
} else {
System.out.printf("Failed to create new directory: %s%n", dir);
}
}
// Creating new file in Java, only if not exists
System.out.println("Enter file name to be created ");
String filename = reader.nextLine();
File f = new File(filename);
if (f.exists()) {
System.out.println("File already exists");
} else {
System.out.println("No such file exists, creating now");
success = f.createNewFile();
if (success) {
System.out.printf("Successfully created new file: %s%n", f);
} else {
System.out.printf("Failed to create new file: %s%n", f);
}
}
// close Scanner to prevent resource leak
reader.close();
}
}
當我想在HDFS上建立目錄時,可以從命令行進行。 在命令提示符下嘗試
hadoop fs -mkdir /input
那應該在HDFS中創建一個名為input的文件夾。
在這個網站上 ,我找到了
URI uri=URI.create (“hdfs://host: port/path”);
我相信應該為您創建目錄。
hadoop fs -mkdir -p /home/hduser/wordcount/input/
為我工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.