操作HDFS常用的两种客户端:
1.通过自带的shell方式进行操作
2.借助Hadoop提供的Java API进行操作
一、hadoop shell常用基本操作命令:
1> 查看HDFS指定的目录
命令:hdfs dfs -ls -R /input
2> 创建指定目录
命令:hdfs dfs -mkdir /output
3> 创建级联目录
命令:hdfs dfs -mkdir -p /output/data
4> 上传HDFS集群服务器文件
命令:hdfs dfs -put test.txt /input
5> 打开HDFS服务器文件
命令:hdfs dfs -cat /input/test.txt
6> 打开HDFS服务器文件
命令:hdfs dfs -tail /input/test.txt
7> 删除HDFS服务器文件
命令:hdfs dfs -rm /input/test.txt
8> 删除HDFS服务器文件夹目录
命令:hdfs dfs -rm -r /input
9> 下载文件到本地
命令:hdfs dfs -get /input/test.txt
10> HDFS文件目录授权
命令:hdfs dfs -chmod 777 /input
二、通过Hadoop Java API客户端操作HDFS文件:
1> 创建普通java项目
2> 导入需要的hadoop的jar包,直接复制hadoop/share/目录下面的所有jar添加到自己创建的java项目中并进行build path。
3> 创建测试类。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
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.apache.hadoop.fs.permission.FsAction;
import org.apache.hadoop.fs.permission.FsPermission;
import org.junit.Before;
import org.junit.Test;public class HDFSDemo {
FileSystem client = null;
/**
* 初始化连接hadoop的HDFS服务器
*/
@Before
public void init() {
// 初始化配置类
Configuration conf = new Configuration();
try {
// 初始化hadoop客户端
client = FileSystem.get(new URI("hdfs://192.168.248.100:9000"), conf, "root");
if (client != null) {
System.out.println("连接hadoop hdfs集群成功!" + client);
}
} catch (Exception e) {
e.printStackTrace();
}
}/**
* 从HDFS集群下载文件到本地目录
*/
@Test
public void downFile() {
System.out.println("获取文件");
try {
client.copyToLocalFile(false, new Path("/input/test.txt"), new Path("E:\\"), true);
client.close();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}/**
* 从本地上传文件到HDFS服务器
*/
@Test
public void uploadFile() {
try {
client.copyFromLocalFile(new Path("E://hello.txt"), new Path("/input"));
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}/**
* 删除HDFS集群目录或文件 true表示递归删除目录 (path里可以是路径,也可以是文件)
*
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void deleteDir() throws IllegalArgumentException, IOException {
client.delete(new Path("/input"), true);
client.close();
}/**
* 创建目录,可以多级目录
*/
@Test
public void createDirHDFS() throws IllegalArgumentException, IOException {
// client.mkdirs(new Path("/hadoopIn"), new FsPermission((short) 777));
// client.close();
FsPermission filePermission = null;
// 当前用户权限,所在组用户权限,其它组用户权限
filePermission = new FsPermission(FsAction.ALL, FsAction.EXECUTE, FsAction.READ);
client.mkdirs(new Path("/input1"), filePermission);
client.close();}
/**
* 对目录或者文件重命名 (源文件,目的)
*
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void renameDirName() throws IllegalArgumentException, IOException {
client.rename(new Path("/input/hello.txt"), new Path("/input/hdfs.txt"));
client.close();
}/**
* 读取文件元信息
*
* @throws FileNotFoundException
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void readDir() throws FileNotFoundException, IllegalArgumentException, IOException {
RemoteIterator<LocatedFileStatus> files = client.listFiles(new Path("/input"), true);
while (files.hasNext()) {
LocatedFileStatus file = files.next();
System.out.println("最近访问时间:" + file.getAccessTime());
System.out.println("块大小:" + file.getBlockSize());
System.out.println("文件大小:" + file.getLen());
System.out.println("文件所有者:" + file.getOwner());
System.out.println("文件副本数:" + file.getReplication());
System.out.println("文件全路径:" + file.getPath());
System.out.println("---本地块信息---:" + Arrays.toString(file.getBlockLocations()));
}
client.close();
}/**
* 查找指定路径下的所有文件夹(当前目录下的一级目录或者文件,不遍历子目录)
* @throws FileNotFoundException
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void readDir2() throws FileNotFoundException, IllegalArgumentException, IOException {
FileStatus[] listStatus = client.listStatus(new Path("/"));
for (FileStatus file : listStatus) {
System.out.println(file.getPath());
System.out.println(file.isDirectory() == true ? "File" : "Dir" + "----");
}
client.close();}
/**
* 读取文件中的部分内容( 从文件开始位置读取,读取20个字节)
* @throws IllegalArgumentException
* @throws IOException
*/
@Test
public void readFilePart() throws IllegalArgumentException, IOException {
FSDataInputStream in = client.open(new Path("/hadoop-pro/input/part-m-00000"));
FileOutputStream out = new FileOutputStream("d:/test.txt");
byte[] b = new byte[20];
int len = 0;
long count = 0;
while ((len = in.read(b)) != -1) {
out.write(b);
count += len;
if (count >= 20)
break;
}
out.flush();
out.close();
in.close();
client.close();}
}