HDFS client environment ready

1. Installation Configuration maven

Download maven.zip
Here Insert Picture Description
the maven unzip to a specific directory
Here Insert Picture Description
configuration environment variable
New MAVEN_HOME, add% MAVEN_HOME% in the Path / bin
Here Insert Picture Description
Here Insert Picture Description
CMD test whether the configuration
Here Insert Picture Description
configuration maven settings.xml file
into the root directory maven \ conf in a settings.xml file
Here Insert Picture Description
to add Ali cloud images

<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>
<mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
 
<mirror>
    <id>ibiblio</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>
<mirror>
    <id>jboss-public-repository-group</id>
    <mirrorOf>central</mirrorOf>
    <name>JBoss Public Repository Group</name>
    <url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
 
<mirror>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <url>http://repo1.maven.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>
<mirror>
    <id>repo2</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://repo2.maven.org/maven2/</url>
</mirror>

Here Insert Picture Description
Modify the local warehouse location
Here Insert Picture Description

2. installation configuration hadoop

Here Insert Picture Description
Hadoop extract to the specified directory
Here Insert Picture Description
configuration environment variable
New HADOOP_HOME, add% HADOOP_HOME% in the Path / bin
Here Insert Picture Description
Here Insert Picture Description
CMD test whether the configuration
Here Insert Picture Description

IDEA environment configuration

打开File->Settings->Build, Execution, Deployment->Build Tools->Maven
Here Insert Picture Description

HDFS client configuration

Maven new project, File-> new-> project, select the item maven
Here Insert Picture Description
Here Insert Picture Description
configuration pom.xml, double-click into pom.xml
Here Insert Picture Description
import-dependent coordinates

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.8.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.7.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.7.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.7.2</version>
		</dependency>
		<dependency>
			<groupId>jdk.tools</groupId>
			<artifactId>jdk.tools</artifactId>
			<version>1.8</version>
			<scope>system</scope>
			<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
		</dependency>
</dependencies>

Here Insert Picture Description
Start to download the required jar package
click maven in the install
Here Insert Picture Description

Configuration log files in the project src / main / resources directory, create a file named "log4j.properties", fill in the file

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

Creating a Package

Creating HdfsClient class

Code Testing

package hdfs;

import com.google.gson.internal.$Gson$Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsClient {

    public static void main(String[] args) throws Exception,IOException,URISyntaxException {

        Configuration conf = new Configuration();
        //conf.set("fs.defaultFS","hdfs://192.168.186.102:9000");

        //1.获取hdfs客户端对象
        //FileSystem fs = FileSystem.get(conf);
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.186.102:9000"),conf,"hadoop");

        //在hdfs上创建路径
        fs.mkdirs(new Path("/test/dashen/shazi"));

        //3.关闭资源
        fs.close();

        System.out.println("over");

    }
}
Published 26 original articles · won praise 15 · views 503

Guess you like

Origin blog.csdn.net/weixin_43497444/article/details/104510563