HDFS:3.客户端操作

目录

1 准备Windows关于Hadoop的开发环境

1.1 准备开发环境

1.1.1 Windows的依赖目录,打开:

1.1.2 配置HADOOP_HOME环境变量。

1.1.3配置Path环境变量。%HADOOP_HOME%/bin然后重启电脑

1.1.4创建一个Maven工程HdfsClientDemo,并导入相应的依赖坐标+日志添加

2 HDFS的API操作

2.1 HDFS文件上传(测试参数优先级)

2.2 HDFS文件下载

2.3HDFS删除文件和目录

2.4文件的更名或者移动

2.5查看文件详情 

2.6判断是文件还是目录


1 准备Windows关于Hadoop的开发环境

1.1 准备开发环境

1.1.1 Windows的依赖目录,打开:

        选择Hadoop-3.1.0,拷贝到其他地方(比如d:\)。

1.1.2 配置HADOOP_HOME环境变量。

1.1.3配置Path环境变量。%HADOOP_HOME%/bin然后重启电脑

1.1.4创建一个Maven工程HdfsClientDemo,并导入相应的依赖坐标+日志添加

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.12.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>3.1.3</version>
    </dependency>
</dependencies>

在项目的src/main/resources目录下,新建一个文件,命名为“log4j2.xml”,在文件中填入 

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" strict="true" name="XMLConfig">
    <Appenders>
        <!-- 类型名为Console,名称为必须属性 -->
        <Appender type="Console" name="STDOUT">
            <!-- 布局为PatternLayout的方式,
            输出样式为[INFO] [2018-01-22 17:34:01][org.test.Console]I'm here -->
            <Layout type="PatternLayout"
                    pattern="[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c{10}]%m%n" />
        </Appender>

    </Appenders>

    <Loggers>
        <!-- 可加性为false -->
        <Logger name="test" level="info" additivity="false">
            <AppenderRef ref="STDOUT" />
        </Logger>

        <!-- root loggerConfig设置 -->
        <Root level="info">
            <AppenderRef ref="STDOUT" />
        </Root>
    </Loggers>
</Configuration>

5)创建包名:com.atguigu.hdfs

6)创建HdfsClient

课件代码:

public class HdfsClient{	
@Test
public void testMkdirs() throws IOException, InterruptedException, URISyntaxException{
		
		// 1 获取文件系统
		Configuration configuration = new Configuration();
		// 配置在集群上运行
		// configuration.set("fs.defaultFS", "hdfs://hadoop102:9820");
		// FileSystem fs = FileSystem.get(configuration);

		FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9820"), configuration, "atguigu");
		
		// 2 创建目录
		fs.mkdirs(new Path("/1108/daxian/banzhang"));
		
		// 3 关闭资源
		fs.close();
	}
}

7)执行程序

运行时需要配置用户名称

客户端去操作HDFS时,是有一个用户身份的。默认情况下,HDFS客户端API会从JVM中获取一个参数来作为自己的用户身份:-DHADOOP_USER_NAME=atguigu,atguigu为用户名称。

实操代码

  /**
     * 获取HDFS的客户端连接对象
     * * @param uri HDFS的访问路径 hdfs://hadoop102:9820
     * * @param conf 配置对象
     * * @param user 操作的用户(用哪个用户操作的HDFS)
     *
     */
    @Test
    public void testCreateHdfsClient() throws IOException, InterruptedException {
        // HDFS的访问路径 hdfs://hadoop102:9820
        URI uri = URI.create("hdfs://hadoop102:9820");
        // conf 配置对象
        Configuration conf = new Configuration();
        // user 操作的用户(用哪个用户操作的HDFS)
        String user = "atguigu";
        //获取HDFS的客户端链接对象(文件系统对象)
        FileSystem fileSystem = FileSystem.get(uri, conf, user);
        System.out.println(fileSystem.getClass().getName());
        //关闭资源
        fileSystem.close();
    }

2 HDFS的API操作

2.1 HDFS文件上传(测试参数优先级)

 /**
     * 上传文件
     * 测试配置的优先级:Configuration>hdfs-site.xml>hdfs-default.xml
     */
    @Test
    public void testCopyFromLocal() throws IOException {
        fs.copyFromLocalFile(false,true,
                new Path("D:\\Java\\hadoop\\wcinput\\hello.txt"),
                new Path("/client_test"));
    }

    /**
     * 获取FileSystem对象
     * @throws IOException
     * @throws InterruptedException
     */
    @Before
    public void init() throws IOException, InterruptedException {
        // HDFS的访问路径 hdfs://hadoop102:9820
        URI uri = URI.create("hdfs://hadoop102:9820");
        // conf 配置对象
        Configuration conf = new Configuration();
        conf.set("dfs.replication","6");
        // user 操作的用户(用哪个用户操作的HDFS)
        String user = "atguigu";
        //获取HDFS的客户端链接对象(文件系统对象)
        fs = FileSystem.get(uri, conf, user);

    }

    /**
     * 关闭资源
     * @throws IOException
     */
    @After
    public void close() throws IOException {
        fs.close();
    }

将hdfs-site.xml拷贝到项目的根目录下

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>
	<property>
		<name>dfs.replication</name>
         <value>1</value>
	</property>
</configuration>

参数优先级排序:(1)客户端代码中设置的值 >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的自定义配置(xxx-site.xml) >(4)服务器的默认配置(xxx-default.xml)

2.2 HDFS文件下载

    /**
     * 下载文件
     */
    @Test
    public void testCopyToLocal() throws IOException {
        fs.copyToLocalFile(false,
                new Path("/client_test/hello.txt"),
                new Path("D:\\Java\\hadoop\\upLoadFile"),
                true);
    }

2.3HDFS删除文件和目录

    /**
     * 删除文件及目录
     * @throws IOException
     */
    @Test
    public void testDelete() throws IOException {
        fs.delete(new Path("/client_test/hello.txt"),true);
    }

2.4文件的更名或者移动

   /**
     * 文件的更名或者移动
     * @throws IOException
     */
    @Test
    public void testRename() throws IOException {
        //移动文件
//        fs.rename(new Path("/sanguo/liubei.txt"),new Path("/client_test"));
        //改名
        fs.rename(new Path("/client_test/liubei.txt"),
                new Path("/client_test/xiaoqiao.txt"));

    }

2.5查看文件详情 

    /**
     * 查看文件详情
     * @throws IOException
     */
    @Test
    public void testListFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> listFiles =
                fs.listFiles(new Path("/"), true);
        while(listFiles.hasNext()){
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println("文件名:"+fileStatus.getPath().getName());
            System.out.println("块大小:"+fileStatus.getBlockSize());
            System.out.println("副本数:"+fileStatus.getReplication());
            System.out.println("权限信息:"+fileStatus.getPermission());
        }

    }

2.6判断是文件还是目录

    /**
     * 判断是文件还是目录
     * @throws IOException
     */
    @Test
    public void testListStatus() throws IOException {
        FileStatus[] listStatus = fs.listStatus(new Path("/client_test"));
        for (FileStatus status : listStatus) {
            if(status.isDirectory()){
                System.out.println("");
            }
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值