我想从 Windows 服务器将文件写入 HDFS。 Hadoop集群在Linux上。 我尝试在所有地方进行研究,我得到了可以使用“hadoop jar”运行的 java 代码
有人可以帮助我了解如何运行 HDFS 文件并从 Windows 编写 Java 代码吗? Windows 框需要什么?即使是正确的链接也可以。
最佳答案
您只需编写一个简单的 java 程序并像普通 .jar 文件一样运行它。
项目中需要导入hadoop库
这是一个工作示例 Maven 项目(我在我的集群上测试过它):
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class WriteFileToHdfs {
public static void main(String[] args) throws IOException, URISyntaxException {
String dataNameLocation = "hdfs://[your-namenode-ip]:[the-port-where-hadoop-is-listening]/";
Configuration configuration = new Configuration();
FileSystem hdfs = FileSystem.get( new URI( dataNameLocation ), configuration );
Path file = new Path(dataNameLocation+"/myFile.txt");
FSDataOutputStream out = hdfs.create(file);
out.writeUTF("Some text ...");
out.close();
hdfs.close();
}
}
请记住将依赖项放入您的 pom.xml 以及为主类构建 list 文件的说明:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<mainClass>your.cool.package.WriteFileToHdfs</mainClass>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
只需使用命令启动程序:
java -jar nameOfTheJarFile.jar
当然你需要用你的包名和namenode ip地址来编辑代码。
关于linux - 从 Windows 服务器读取/写入文件到 HDFS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33455888/