5.Hadoop_HDFS_Java API

准备工作

  1. 解压 hadoop-2.6.5.tar.gz(windows版本) 和hadoop-2.6.5-src.tar.gz

  2. 解压后的hadoop-2.6.5中的jar包 存入同一文件夹下,用于eclipse 外部引用

  3. 把解压后的 hadoop-2.6.5\bin\hadoop.dll 拷贝到c:\windows\system32\ 文件夹中

  4. 配置环境变量HADOOP_HOME地址为解压路径并在path中加入%HADOOP_HOME%\bin

  5. 配置环境变量HADOOP_USER_NAME 值为root

Eclipse 操作

  1. 使用eclipse-mars,查看plugins是否有hadoop-eclipse-plugin-2.6.0.jar 插件

    在这里插入图片描述

  2. 调出 Map/Reduce Locations 在菜单栏 Windows >> Show View >> Order >> MapReduce Tools >> Map/Reduce Locations

    在这里插入图片描述

  3. 设置hadoop路径 在菜单栏 Windows >> Preferences >> Map/Reduce Locations >> Browse… >> 设置hadoop安装的路径

  4. 在 Map/Reduce Locations 右键 新建可视化定位器

    在这里插入图片描述

  5. 新建 host 中填入状态为active 的NN地址以及端口号

    在这里插入图片描述

    1. 点击新创建的host 可以创建目录等操作

代码部分

  1. 创建外部jar包引用 : 菜单栏Windows >> Java >> Build Path >> User Libbraries >> New… >> User Library name: 填入自定义名称(注意 System Library 不要勾选) >> OK >> Add External JARs… >> 选择hadoop解压包中所有的jar包 >> OK

  2. 创建java项目

  3. 引入外部jar包 : 在项目右键 >> Build Path >> Configure Build Path… >> Java Build Path >> Libraries >> Add Library… >> User Libbrary >> Next >> 选择刚刚创建的jars >> Finish >> Apply >> OK

  4. 引入单元测试类jar包 : 在项目右键 >> Build Path >> Configure Build Path… >> Java Build Path >> Libraries >> Add Library… >> JUnit >> JUnit4 >> Finish

  5. core-site.xml和hdfs-site.xml 拷入项目src文件夹中

  6. AIP代码

    package com.chen.hdfs.test;
    
    import java.io.FileInputStream;
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.*;
    import org.apache.hadoop.io.IOUtils;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    public class TestHDFS {
    	Configuration conf = null;
    	FileSystem fs = null;
    
    	@Before
    	public void conn() throws IOException {
    		conf = new Configuration();
    		fs = FileSystem.get(conf);
    	}
    	
    	//创建hdfs目录
    	@Test
    	public void mkdir() throws IOException {
    		Path path = new Path("/mytemp");
    		if(fs.exists(path)){
    			fs.delete(path,true);
    		}
    		fs.mkdirs(path);
    		
    	}
    	
    	//上传文件
    	@Test
    	public void uploadFIle() throws IOException{
    		
    		//文件上传路径
    		Path path = new Path("/mytemp/test.txt");
    		FSDataOutputStream fdos = fs.create(path);   
    		
    		//拿到磁盘文件
    		InputStream is = new BufferedInputStream(new FileInputStream("D:/test.txt"));
    		//hadoop提供的写入工具  参数4关闭io
    		IOUtils.copyBytes(is, fdos, conf,true);
    	}
    	
    	//读取文件
    	@Test
    	public void readFile() throws IOException {
    
    		Path f = new Path("/mytemp/test.txt");
    		FileStatus file = fs.getFileStatus(f);
    		BlockLocation[] blks = fs.getFileBlockLocations(file, 0, file.getLen());
    		//遍历数组
    		for (BlockLocation blk : blks){
    			 System.out.println(blk);
    		}
    		
    		//读取文件
    		FSDataInputStream fdis = fs.open(f);
    
    //		fdis.seek(10485756);
    		System.out.println(fdis.readByte());
    
    	}
    
    	@After
    	public void close() {
    		if(fs!=null){
    			try {
    				fs.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    }
    
    
  • 手动上传
    hdfs dfs D dfs.blocksize=1048576 - put test.txt
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值