Hadoop学习之java连接HDFS文件系统

1、配置环境变量HADOOPHOME,不然会报错。
2、windows上的权限系统和linux上的权限系统,测试期间为了简单起见可以关闭权限检查 在namenode的hdfs-site.xml上,添加配置:

<property>    
    <name>dfs.permissions.enabled</name>    
    <value>false</value>
</property>

3、这里需要引入Hadoop的lib,引入有2种方式:
HADOOPHOME\share\hadoop\hdfsjarlibjarHDFSjar H A D O O P H O M E \share \hadoop \hdfs 下 的 j a r , 和 l i b 下 的 j a r 包 统 一 导 入 , 这 里 面 H D F S 必 要 的 j a r 包 ; 另 外 还 需 要 {HADOOP_HOME}\share\hadoop\common下的jar,和lib下的jar包,这个通用jar包的支持。
根据自己的版本,在http://mvnrepository.com/ maven仓库找到自己自己的版本引入,这里使用的是2.7.3,根据自己需要改变版本,pom引入在文章结尾。

测试代码:

package learn.hadoop.hdsf.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author CX
 */
public class HdfsTest {
    Configuration conf = null;
    FileSystem fs = null;

    @org.junit.Before
    public void conn () throws Exception{
        conf =new Configuration(false);
        conf.set("fs.defaultFS", "hdfs://cx:8020");
        conf.set("dfs.replication", "1");
        conf.set("dfs.block.size", "3145728");
        conf.set("dfs.client.use.datanode.hostname", "true");
        fs = FileSystem.get(conf);
    }

    @org.junit.After
    public void close() throws Exception{
        fs.close();
    }

    /**
     * 链接测试
     */
    @Test
    public void testCon(){
        System.out.println(conf.get("fs.defaultFS"));
    }

    /**
     * 创建目录
     * @throws Exception
     */
    @Test
    public void mkdir () throws Exception{
        Path tmp = new Path("/temp");
        if(!fs.exists(tmp)){
            fs.mkdirs(tmp );
        }
    }

    /**
     * 上传文件
     * @throws Exception
     */
    @Test
    public void uploadFile() throws Exception{
        Path file = new Path("/hello.txt");
        FSDataOutputStream output = fs.create(file );
        InputStream  input  = new BufferedInputStream(new FileInputStream(new File("D:\\IDEA\\myself\\src\\main\\java\\learn\\hadoop\\hdsf\\test\\hello.txt")));
        IOUtils.copyBytes(input, output, conf, true);
    }

    /**
     * 删除文件
     * @throws Exception
     */
    @Test
    public void delete() throws Exception {
        Path p =new Path("/input.txt");
        if(fs.exists(p)){
            fs.delete(p);
        }
    }

    /**
     * 读取文件 并打印文件
     * @throws Exception
     */
    @Test
    public void read() throws Exception{
        Path f=new Path("/hello.txt");
        FileStatus file= fs.getFileStatus(f);
        BlockLocation[] bls = fs.getFileBlockLocations(file, 0, file.getLen());

        for (BlockLocation bl : bls) {
            System.err.println(bl);
        }
        FSDataInputStream in = fs.open(f);
        /*input.seek(1048578);
        System.out.println((char)input.readByte());*/
        IOUtils.copyBytes(in,System.out,4096,true);

    }


}

pom文件,这里的${hadoop.version} 配置的是 :

 <properties>
        <hadoop.version>2.7.3</hadoop.version>
  </properties>

所有依赖:

<dependencies>
        <!--Hadoop-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>${hadoop.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-annotations</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-minicluster</artifactId>
            <version>${hadoop.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>1.2.1</version>
        </dependency>
</dependencies>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值