关于win10下apche版本与cdh版本hadoop的javaapi差异与思考

apche版本与cdh版本hadoop的javaapi会存在什么差异?

javaapi获取FileSystem

package cn.mrjingchen.hdfs;

import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

public class HdfsOperate {
	
static final String URI_100 = "hdfs://192.168.184.100";
	
	static final String LOCAL_FILE = "D:\\tempFile\\test.txt";
	
	public static void main(String[] args) throws Exception {
		
		HdfsOperate.getFileSystemMethed1();
		HdfsOperate.getFileSystemMethod2();
		HdfsOperate.getFileSystemMethod3();
		HdfsOperate.getFileSystemMethod4();
		
	}
	
	/** 获取文件系统方式1
	 */
	public static void getFileSystemMethed1() throws Exception {
		Configuration configuration = new Configuration();
	    FileSystem fileSystem = FileSystem.get(new URI(URI_100), configuration);
	    System.out.println("---->getFileSystemMethed1--FS:"+fileSystem.toString());
	}
	
	/** 获取文件系统方式2
	 */
	public static void getFileSystemMethod2() throws Exception{
		 Configuration configuration = new Configuration();
		 configuration.set("fs.defaultFS", URI_100);
		 FileSystem fileSystem = FileSystem.get(new URI("/"), configuration);
		 System.out.println("---->getFileSystemMethed2--FS:"+fileSystem.toString());
	}
	
	/** 获取文件系统方式3 */
	public static void getFileSystemMethod3() throws Exception {
		Configuration configuration = new Configuration();
	    FileSystem fileSystem = FileSystem.newInstance(new URI(URI_100), configuration);
	    System.out.println("---->getFileSystemMethed3--FS:"+fileSystem.toString());
	}
	
	/** 获取文件系统方式4 */
	public static void getFileSystemMethod4() throws Exception{
		Configuration configuration = new Configuration();
		configuration.set("fs.defaultFS", URI_100);
		FileSystem fileSystem = FileSystem.newInstance(new URI("/"), configuration);
		System.out.println("---->getFileSystemMethed4--FS:"+fileSystem.toString());
	}
	
}

1.1.apache版本pom依赖

<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>
	<parent>
		<groupId>cn.cbigdate</groupId>
		<artifactId>zk</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>hdfs</artifactId>

	<dependencies>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.6.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.6.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.6.0</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>
	</dependencies>
</project>

1.2 apache版本pom–>console

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
---->getFileSystemMethed1--FS:DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_1593538100_1, ugi=mrcjing (auth:SIMPLE)]]
---->getFileSystemMethed2--FS:DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_1593538100_1, ugi=mrcjing (auth:SIMPLE)]]
---->getFileSystemMethed3--FS:DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_-1892687875_1, ugi=mrcjing (auth:SIMPLE)]]
---->getFileSystemMethed4--FS:DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_1478644527_1, ugi=mrcjing (auth:SIMPLE)]]

2.1.cdh版本pom依赖

<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>
	<parent>
		<groupId>cn.cbigdate</groupId>
		<artifactId>zk</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>hdfs</artifactId>

	<repositories>
		<repository>
			<id>cloudera</id>
			<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.6.0-mr1-cdh5.14.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.6.0-cdh5.14.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.6.0-cdh5.14.0</version>
		</dependency>
	</dependencies>
</project>

2.2 cdh版本pom–>console

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
	at org.apache.hadoop.conf.Configuration$DeprecationDelta.<init>(Configuration.java:338)
	at org.apache.hadoop.conf.Configuration$DeprecationDelta.<init>(Configuration.java:351)
	at org.apache.hadoop.conf.Configuration.<clinit>(Configuration.java:433)
	at cn.mrjingchen.hdfs.HdfsOperate.getFileSystemMethed1(HdfsOperate.java:77)
	at cn.mrjingchen.hdfs.apacheVersion.main(apacheVersion.java:16)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Preconditions
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 5 more

2.3 cdh版本解决如上异常:pom增加依赖

<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>
	<parent>
		<groupId>cn.cbigdate</groupId>
		<artifactId>zk</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>hdfs</artifactId>

	<repositories>
		<repository>
			<id>cloudera</id>
			<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.6.0-mr1-cdh5.14.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.6.0-cdh5.14.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.6.0-cdh5.14.0</version>
		</dependency>
		
		<!-- 解决异常:Caused by: java.lang.ClassNotFoundException: com.google.common.base.Preconditions -->
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>13.0-rc1</version>
		</dependency>
		
		<!-- 解决异常:Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.UnmodifiableMap -->
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.2</version>
		</dependency>
	
		<!-- 解决异常:Caused by: java.lang.ClassNotFoundException: org.apache.commons.configuration.Configuration -->
		<dependency>
			<groupId>commons-configuration</groupId>
			<artifactId>commons-configuration</artifactId>
			<version>1.6</version>
		</dependency>
		
		<!-- 解决异常:Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.util.PlatformName -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-auth</artifactId>
			<version>2.6.0-cdh5.14.0</version>
		</dependency>
		
		<!-- 解决异常:Caused by: java.lang.ClassNotFoundException: org.apache.htrace.core.Tracer$Builder -->
		<dependency>
			<groupId>org.apache.htrace</groupId>
			<artifactId>htrace-core4</artifactId>
			<version>4.2.0-incubating</version>
		</dependency>
		
		<!-- 解决异常:java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries. -->
		<!-- 异常解决说明:CDH版本需要在本机安装hadoop客户端,并需要hadoop.dll和winutils.exe支持。本机安装cdh版本的hadoop2.6.0客户端省略 -->
		
		<!-- 本机安装cdh版本的hadoop2.6.0客户端后,解决新异常: Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException-->
		<dependency>
			<groupId>commons-cli</groupId>
			<artifactId>commons-cli</artifactId>
			<version>1.2</version>
		</dependency>
		
		<!-- 解决异常:Caused by: java.lang.ClassNotFoundException: com.google.protobuf.ServiceException -->
		<dependency>
			<groupId>com.google.protobuf</groupId>
			<artifactId>protobuf-java</artifactId>
			<version>2.5.0</version>
		</dependency>
		
	</dependencies>
</project>

2.4 cdh版本新pom–>console

19/03/17 12:41:14 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
---->getFileSystemMethed1--FS:DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_515185435_1, ugi=mrcjing (auth:SIMPLE)]]
---->getFileSystemMethed2--FS:DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_515185435_1, ugi=mrcjing (auth:SIMPLE)]]
---->getFileSystemMethed3--FS:DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_-1910117984_1, ugi=mrcjing (auth:SIMPLE)]]
---->getFileSystemMethed4--FS:DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_275527283_1, ugi=mrcjing (auth:SIMPLE)]]

1、  apache版pom,本机无需安装hadoop客户端,可直接操作服务端hadoop;
2、  cdh版pom,本机安装hadoop客户端,才能操作服务端hadoop?客户端又解决了什么

源码解读…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值