# Java 使用 Hadoop、Hdfs 的 ApI

8_4java使用hadoop、hdfs的ApI文件读写

  1. 引入相应的maven依赖
 <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.9.2</version>
 </dependency>
  1. HdfsUtils
public class HdfsUtils{
	//创建mkdir
	public bollean mkdirs(){
		return true;
	}
}

TestClass

public Class HdfsUtilsTest(){
	
	static{
		System.setProperty("HADOOP_USER_NAME","hadoop")
	}
	private final int BUFF_SIZE=4096;
	FileSystem fs;
	@Before
	public void setup(){
		System.out.println("Before execute");	
	}
	
	@After
	public void destory(){
		System.out.println("After execute");
	}
	
	@Test
	public void testMkdir(){
		Configuration conf=new Configuradion();
		conf.set("fs.defaultFS","hdfs://hadoop:9000");
		
		FileSystem fs=FileSystem.get(conf);
		fs.mkdirs(new Part("/a/b"));
		//断言返回true为测试通过
		bollean resuilt=fs.mkdirs(new Path("/b/c"))
		Assert.assertTrue(result);
	}
	//上传
	public void upload(){
		String Filename="pom.xml";
		fs.copyFromLocalFile(new Path("hadoop-hdfs.iml"),new Path("/"));
		
		bollean exists=fs.exists(new Path("/",fileName));
		Assert.assertTure(exists);
	}
	public void dowload(){
		fs.copyToLocalFile("","");
	}
	
	public void delete(){
		fs.delete(null);
	}
	
	//写文件操作
	public void write(){
		Inputstream in=new FileInputStream(new File("D:\\python"));
		//输出流
		FSDataOutputStream out=fs.create(new Path("/upload"));
		byte buf[]=new byte[BUFF_SIZE];
		int bytesRead=in.read(buf);
			while(byteRead>0){
				out.write(buf);
				byteRead=in.read(buf);
			}
		
	}
}
  1. MapReduce
public class WordCountMapper extends Mapper<Object,Text,Text,IntWritable>{
	//Object行数
	@Overrride
	protected void map(Object key,Text value,Context context){
		While(){
		
		}
	}
}

public class WordCountMapper extends Reduce<Text,IntWritable Text,IntWritable>{
	//如果是key相同的数据,进入同一个reduce
	@Overrride
	protected void reduce(Text,Iterable<IntWritable> values,Context context){
		While(){
		
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值