HDFS基本操作

HDFS基本操作

HDFS基本操作

一、hadoop搭建     

1、修改主机名     

2、ip等     

3、主机映射     

4、关闭防火墙(两种)     

5、ssh免密登录(免密脚本)     

6、安装jdk 配置环境变量     

7、安装hadoop         

配置文件的修改         

  hadoop-env.sh    JAVA_HOME         

  core-site.xml    客户端参数    namenode在哪里         

  hdfs-site.xml    namenode和datanode存放的目录         

  mapred-site.xml    经过重命名之后才得到的文件,提交任务到哪里         

  yarn-site.xml    配置resourcemanager在哪里,资源的多少         

  HADOOP环境变量     

8、安装程序分发集群         

  hosts文件         

  jdk安装文件    /etc.profile         

  hadoop安装文件     

9、namenode进行格式化         

  hadoop namenode -format     

10、启动测试         

  start-dfs.sh         

  hadoop-deams.sh    单独启动单台机器的进程         

  start-yarn        是在resourcemaneger启动的

二、shell命令操作hdfs

     启动hadoop :start-dfs.sh

常用命令参数介绍     

  Shell客户端启动  

  Hadoop fs                 hdfs dfs

-help                      

  功能:输出这个命令参数手册     

-ls                           

  功能:显示目录信息         

  示例: hadoop fs -ls hdfs://hadoop-server01:9000/         

  备注:这些参数中,所有的hdfs路径都可以简写         

  -->hadoop fs -ls /   等同于上一条命令的效果     

-mkdir                       

  功能:在hdfs上创建目录         

  示例:hadoop fs  -mkdir  -p  /aaa/bbb/cc/dd     

-moveFromLocal                     

  功能:从本地剪切粘贴到hdfs         

  示例:hadoop  fs  - moveFromLocal  /home/hadoop/a.txt  /aaa/bbb/cc/dd     

-moveToLocal                       

  功能:从hdfs剪切粘贴到本地         

  示例:hadoop  fs  - moveToLocal   /aaa/bbb/cc/dd  /home/hadoop/a.txt      

--appendToFile           

  功能:追加一个文件到已经存在的文件末尾         

  示例:hadoop  fs  -appendToFile  ./hello.txt  hdfs://hadoop-server01:9000/hello.txt         

  可以简写为:         Hadoop  fs  -appendToFile  ./hello.txt  /hello.txt

-cat           

  功能:显示文件内容           

  示例:hadoop fs -cat  /hello.txt

-tail                          

  功能:显示一个文件的末尾         

  示例:hadoop  fs  -tail  /weblog/access_log.1     

-text                           

  功能:以字符形式打印一个文件的内容         

  示例:hadoop  fs  -text  /weblog/access_log.1     

-chgrp      

-chmod     

-chown         

  功能:linux文件系统中的用法一样,对文件所属权限         

  示例:         hadoop  fs  -chmod  666  /hello.txt         hadoop  fs  -chown  someuser:somegrp   /hello.txt     

-copyFromLocal             

  功能:从本地文件系统中拷贝文件到hdfs路径去         

  示例:hadoop  fs  -copyFromLocal  ./jdk.tar.gz  /aaa/     

-copyToLocal               

  功能:从hdfs拷贝到本地         

  示例:hadoop fs -copyToLocal /aaa/jdk.tar.gz     

-cp                       

  功能:从hdfs的一个路径拷贝hdfs的另一个路径         

  示例: hadoop  fs  -cp  /aaa/jdk.tar.gz  /bbb/jdk.tar.gz.2

-mv                              

  功能:在hdfs目录中移动文件         

  示例: hadoop  fs  -mv  /aaa/jdk.tar.gz  /     

-get                       

  功能:等同于copyToLocal,就是从hdfs下载文件到本地         

  示例:hadoop fs -get  /aaa/jdk.tar.gz     

-getmerge                      

  功能:合并下载多个文件         

  示例:比如hdfs的目录 /aaa/下有多个文件:log.1, log.2,log.3,...     hadoop fs -getmerge /aaa/log.* ./log.sum     

-put                         

  功能:等同于copyFromLocal         

  示例:hadoop  fs  -put  /aaa/jdk.tar.gz  /bbb/jdk.tar.gz.2

-rm                         

  功能:删除文件或文件夹         

  示例:hadoop fs -rm -r /aaa/bbb/

-rmdir                          

  功能:删除空目录         

  示例:hadoop  fs  -rmdir   /aaa/bbb/ccc     

-df                        

  功能:统计文件系统的可用空间信息         

  示例:hadoop  fs  -df  -h  /

-du          

  功能:统计文件夹的大小信息         

  示例:         hadoop  fs  -du  -s  -h /aaa/*

-count                  

  功能:统计一个指定目录下的文件节点数量         

  示例:hadoop fs -count /aaa/

-setrep                         

  功能:设置hdfs中文件的副本数量     

  示例:hadoop fs -setrep 3 /aaa/jdk.tar.gz

三、 java操作hdfs(Maven)

1.pom.xml

<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>cn.pengpeng</groupId>
	<artifactId>bigdata36-hadoop</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>4.12</version>
		    <scope>test</scope>
		</dependency>
		
		<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.8.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/log4j/log4j -->
		<dependency>
		    <groupId>log4j</groupId>
		    <artifactId>log4j</artifactId>
		    <version>1.2.17</version>
		</dependency>

	</dependencies>
	
	<!-- maven打包插件jdk的版本  -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

2.简单的增删改查操作

package hdfs.test;

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

import javax.annotation.WillClose;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class hdfsDemo {
	FileSystem fs =null;
	@Before
	public void init() throws Exception{
		//连接hdfs
		Configuration conf = new Configuration();
		fs = FileSystem.get(new URI("hdfs://hadoop01:9000"),conf,"root");
		
	}
	/**
	 * 将本地文件上传到hdfs
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void testUpLoad() throws Exception{
		fs.copyFromLocalFile(new Path("D:\\data\\http.log"), new Path("/"));
	}
	
	/**
	 * 从hdfs下载文件
	 * @throws Exception 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void testDownLoad() throws  Exception{
		fs.copyToLocalFile(new Path("/http.log"), new Path("d:/http"));
	}

	/**
	 * 删除hdfs文件
	 * @throws Exception 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public  void TestDel() throws Exception{
		fs.delete(new Path("/http.log"),true);
	}
	
	
	/**
	 * 创建文件夹
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 * @throws Exception 
	 */
	@Test
	public void testMkdir() throws IllegalArgumentException, IOException{
		fs.mkdirs(new Path("/files"));
	}
	/**
	 * 改名字和移动文件
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 * @throws Exception
	 */
	@Test
	public void rename() throws IllegalArgumentException, IOException{
		//如果文件夹不存在,移动不会成功,也不会报错。
		//fs.rename(new Path("/test.sh"), new Path("/test2.sh"));
		fs.rename(new Path("/test2.sh"), new Path("/files/test.sh"));
	}
	/**
	 * 查看文件状态
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 * @throws Exception
	 */
	@Test
	public void testStatus() throws IllegalArgumentException, IOException{
		/*FileStatus status = fs.getFileStatus(new Path("/files"));
		System.out.println(status);*/
		
		RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),