通过Java操作hdfs

1、使用IDEA,在之前创建的bigdata项目里面新建hadoop模块,导入相关hadoop包

<dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.6</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.3</version>
        </dependency>
    </dependencies>

 

通过Java操作hdfs

 

 

 

通过Java操作hdfs

 2、连接Hadoop进行hdfs操作

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

import java.net.URI;

/*
通过Java连接hadoop进行hdfs操作
 */
public class HadoopAPI {
    public static void main(String[] args) throws Exception{
        //自动获取hadoop配置文件
        Configuration conf = new Configuration();
        //设置副本
        conf.set("dfs.replication","1");
        //连接
        URI uri = new URI("hdfs://master:9000");
        //连接文件管理系统,生成一个对象,相当于一个客户端
        FileSystem fileSystem = FileSystem.get(uri, conf);
    }
}

 

通过Java操作hdfs

 

 

 

  通过Java操作hdfs

 

 

 3、为了方便测试使用junit测试方法

  a、创建目录mkdir命令

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;

import java.net.URI;

/*
通过Java连接hadoop进行hdfs操作
 */
public class HadoopAPI {
    FileSystem fs;
    @Before
    //初始化
    public void init() throws Exception{
        //自动获取hadoop配置文件
        Configuration conf = new Configuration();
        //设置副本
        conf.set("dfs.replication","1");
        //连接
        URI uri = new URI("hdfs://master:9000");
        //连接文件管理系统,生成一个对象,相当于一个客户端
        fs = FileSystem.get(uri, conf);
    }

    @Test
    public void mkdir() throws Exception{
        fs.mkdirs(new Path("/javamake "));
    }
}

 

   b、递归删除目录

 @Test
    public  void delete() throws Exception{
//        fs.delete(new Path("/javamake"),false);
        fs.delete(new Path("/javamake"),true);
    }

 

通过Java操作hdfs

 

   c、listStatus 获取多个       listStatus 获取单个

 @Test
    public  void listStatus() throws Exception{
        FileStatus[] fls = fs.listStatus(new Path("/"));
        for (FileStatus fl : fls) {
            System.out.println(fl.getLen());
            System.out.println(fl.getBlockSize());
            System.out.println(fl.getGroup());
        }
    }

 

通过Java操作hdfs

 

   d、从hdfs上读文件

    @Test
    public void load() throws Exception{
        //从hdfs上获取要读文件
        FSDataInputStream path = fs.open(new Path("/data/student/students.txt"));
        //使用字符缓冲流读文件
        BufferedReader br = new BufferedReader(new InputStreamReader(path));
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }

        br.close();
        path.close();
    }

通过Java操作hdfs

 

   e、写文件

  @Test
    public void create() throws Exception{
        FSDataOutputStream fo = fs.create(new Path("/text.txt"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));
        bw.write("你好");
        bw.newLine();
        bw.write("大数据");
        bw.newLine();
        bw.close();
        fo.close();
    }
}

通过Java操作hdfs

 

 通过Java操作hdfs

 

上一篇:HDFS 集群环境搭建


下一篇:HBase 简介