您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何進行Hadoop Java API簡單使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
注意:jar版本,務必和遠程的hadoop版本一致。
maven配置文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Hadoop</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <hadoop.version>2.7.1</hadoop.version> </properties> <dependencies> <!--hadoop--> <!-- http://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-yarn-common</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-yarn-api</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-yarn-client</artifactId> <version>${hadoop.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
測試用例:
package com.demo; 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 org.apache.hadoop.io.IOUtils; import org.junit.Before; import org.junit.Test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; public class HadoopTest { FileSystem fileSystem = null; @Before public void init() throws URISyntaxException, IOException, InterruptedException { String p = "hdfs://yarn001:9000"; fileSystem = FileSystem.get(new URI(p), new Configuration(), "root"); } /** * 測試文件下載 * @throws URISyntaxException * @throws IOException */ @Test public void downloadTest() throws URISyntaxException, IOException { Path path = new Path("/hadoop-2.7.1.tar.gz"); InputStream open = fileSystem.open(path); FileOutputStream fileOutputStream = new FileOutputStream("d://hadoop"); IOUtils.copyBytes(open, fileOutputStream, 4096, true); } /** * 測試文件上傳1 * @throws IOException */ @Test public void uploadFileTest1() throws IOException { InputStream fileInputStream = new FileInputStream("d://SpringBoot.mobi"); Path path = new Path("/SpringBoot"); FSDataOutputStream fsDataOutputStream = fileSystem.create(path); IOUtils.copyBytes(fileInputStream,fsDataOutputStream,4096); } /** * 測試文件上傳2 */ @Test public void uploadFileTest2() throws IOException { Path localPath = new Path("d://test.xls"); Path remoterPath = new Path("/testXLS"); fileSystem.copyFromLocalFile(localPath,remoterPath); } /** * 測試刪除文件 */ @Test public void delFileTest() throws IOException { Path path = new Path("/testXLS"); /** * 刪除空目錄 */ boolean delete = fileSystem.delete(path, false); /** * 刪除非空目錄 * 遞歸刪除 */ /* boolean delete1 = fileSystem.delete(path, true); */ System.out.println(delete?"刪除成功":"刪除失敗"); } /** * 創建目錄測試 * @throws IOException */ @Test public void createFolder() throws IOException { Path path = new Path("/testPath2"); boolean mkdirs = fileSystem.mkdirs(path); System.out.println(mkdirs?"success":"fail"); } }
常見異常:
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.tracing.SpanReceiverHost.get(Lorg/apache/hadoop/conf/Configuration;Ljava/lang/String;)Lorg/apache/hadoop/tracing/SpanReceiverHost; at org.apache.hadoop.hdfs.DFSClient.<init>(DFSClient.java:634) at org.apache.hadoop.hdfs.DFSClient.<init>(DFSClient.java:619)
異常處理:
配置maven的pom.xml文件,把本地lib版本和遠程Hadoop 的HDFS版本一致即可。
看完上述內容,你們對如何進行Hadoop Java API簡單使用有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。