hadoop学习----hadoop api的使用(java版)

一、包的导入

          在项目目录下创建lib文件夹来保存复制的hadoop的jar包

           一般所需的jar包都在hadoop下的share目录下,把里面的都导进去,我用的是hadoop 2.6版本,不够hadoop 2.x版本都能使用差不多版本的jar包。像我使用的*-client-*.jar就是拿的hadoop2.8版本的。

           懒得自己找的,我把它压缩上传了,可以下载https://download.csdn.net/download/qq_21467113/10958238

二、API的创建

            1.先把能导的都导了,免得到时候出错

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

import org.apache.hadoop.conf.Configuration;
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.Before;
import org.junit.Test;

             2.创建client连接

       // new Configuration()会从项目的classpath中加载core-default.xml hdfs-default.xml core-site.xml hdfs-site.xml等文件
		Configuration conf = new Configuration();
		
		// 指定本客户端上传文件到hdfs时需要保存的副本数为:2
		conf.set("dfs.replication", "2");
		// 指定本客户端上传文件到hdfs时切块的规格大小:64M
		conf.set("dfs.blocksize", "4m");
		
		// 构造一个访问指定HDFS系统的客户端对象: 参数1:——HDFS系统的URI,参数2:——客户端要特别指定的参数,参数3:客户端的身份(用户名)
		FileSystem fs = FileSystem.get(new URI("hdfs://192.168.56.6:9000/"), conf, "hadoop");

        3.简单例子,上传文件

      // 上传一个文件到HDFS中
		fs.copyFromLocalFile(new Path("d:/logger.zip"), new Path("/wordcount/input/"));
		
 		fs.close();

三、API的功能使用 

          1.从HDFS中下载文件到客户端本地磁盘

public void testGet() throws IllegalArgumentException, IOException{
		
		fs.copyToLocalFile(new Path("/test.txt"), new Path("f:/"));
		fs.close();
		
	}

            2. 在hdfs内部移动文件\修改名称

public void testRename() throws Exception{
		
		fs.rename(new Path("/install.log"), new Path("/aaa/in.log"));
		
		fs.close();
		
	}

             3. 在hdfs中创建文件夹

public void testMkdir() throws Exception{
		
		fs.mkdirs(new Path("/xx/yy/zz"));
		
		fs.close();
	}

             4. 在hdfs中删除文件或文件夹

public void testRm() throws Exception{
		
		fs.delete(new Path("/aaa"), true);
		
		fs.close();
	}

            5. 查询hdfs指定目录下的文件信息

public void testLs() throws Exception{
		// 只查询文件的信息,不返回文件夹的信息
		RemoteIterator<LocatedFileStatus> iter = fs.listFiles(new Path("/"), true);
		
		while(iter.hasNext()){
			LocatedFileStatus status = iter.next();
			System.out.println("文件全路径:"+status.getPath());
			System.out.println("块大小:"+status.getBlockSize());
			System.out.println("文件长度:"+status.getLen());
			System.out.println("副本数量:"+status.getReplication());
			System.out.println("块信息:"+Arrays.toString(status.getBlockLocations()));
			
			System.out.println("--------------------------------");
		}
		fs.close();
	}

            6. 查询hdfs指定目录下的文件和文件夹信息

public void testLs2() throws Exception{
		FileStatus[] listStatus = fs.listStatus(new Path("/"));
		
		for(FileStatus status:listStatus){
			System.out.println("文件全路径:"+status.getPath());
			System.out.println(status.isDirectory()?"这是文件夹":"这是文件");
			System.out.println("块大小:"+status.getBlockSize());
			System.out.println("文件长度:"+status.getLen());
			System.out.println("副本数量:"+status.getReplication());
			
			System.out.println("--------------------------------");
		}
		fs.close();
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值