【大数据】使用Java api存储数据

1.创建项目,导入hadoop相关的包file—>project structure

2.文件系统开启关闭测试

package hdfs_demo;

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

import java.io.IOException;
import java.net.URI;

public class HDFSTest {
    public static void main(String[] args) throws IOException {

        //FileSystem.get(URI.create(hdfsPath),conf)参数中所要的访问的URL就是hdfs的网址
        //本地IP加端口号9000,获取本地IP——在cmd:ipconfig
        String hdfsPath="hdfs://*********:9000";

        //本地客户端如果要用到hdfs环境,就需要配置文件
        //创建配置对象,主要用于读取配置文件: hdfs-site.xml core-site.xml map.xml
        Configuration conf = new Configuration();

        //访问hdfs需返回一个对象,这个对象是FileSystem
        FileSystem fileSystem =FileSystem.get(URI.create(hdfsPath),conf);

        //关闭对hdfs的访问
        fileSystem.close();
    }
}

3.查看文件或目录是否存在

package hdfs_demo;

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

import java.io.IOException;
import java.net.URI;

public class Hdfs_isExist {
    public static void main(String[] args) throws IOException {

        String hdfsPath="hdfs://localhost:9000";

        //要判断的文件路径hdfs://localhost:9000/test
        Path path = new Path(hdfsPath+"/test");

        //本地客户端如果要用到hdfs环境,就需要配置文件
        //创建配置对象,主要用于读写配置文件: hdfs-site.xml core-site.xml map.xml
        Configuration conf = new Configuration();

        //访问hdfs需返回一个对象,这个对象是FileSystem
        FileSystem fs =FileSystem.get(URI.create(hdfsPath),conf);

        //判断在HDFS上指定路径文件或目录是否存在
        if(fs.exists(path)){
            System.out.println("HDFS  上存在:" + path);
        }else{
            System.out.println("HDFS 上不存在:" + path);
        }
        //关闭HDFS的访问
        fs.close();

    }
}

4.删除文件

fs.delete(path);

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

import java.io.IOException;
import java.net.URI;

public class Hdfs_rmFile {
    public static void main(String[] args) throws IOException {
        String hdfsPath = "hdfs://localhost:9000";
        Path path = new Path(hdfsPath + "/test/start-all.cmd");//hdfs://localhost:9000/test/infiledir
        //本地客户端如果要用到hdfs环境,就需要配置文件
        //创建配置对象,主要用于读写配置文件: hdfs-site.xml core-site.xml map.xml
        Configuration conf = new Configuration();
        //访问hdfs需返回一个对象,这个对象是FileSystem
        FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
        if(fs.deleteOnExit(path)){
            fs.delete(path);
            System.out.println("删除成功:" + path);
        }else{
            System.out.println("文件不存在!");
        }
        fs.close();
    }
}

5.删除目录

fs.delete(path);

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

import java.io.IOException;
import java.net.URI;

public class Hdfs_rmrDir {
    public static void main(String[] args) throws IOException {
        String hdfsPath = "hdfs://localhost:9000";
        Path path = new Path(hdfsPath + "/test/infiledir");//hdfs://localhost:9000/test/infiledir
        //本地客户端如果要用到hdfs环境,就需要配置文件
        //创建配置对象,主要用于读写配置文件: hdfs-site.xml core-site.xml map.xml
        Configuration conf = new Configuration();
        //访问hdfs需返回一个对象,这个对象是FileSystem
        FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
        //删除HDFS上指定路径的目录
        if (fs.exists(path)) {
            fs.delete(path);
            System.out.println("删除成功:" + path);
        } else {
            System.out.println("删除失败,文件不存在:" + path);
        }
        fs.close();
    }
}

6.显示路径的所有内容

FileStatus[] fileList = fs.listStatus(path);

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

import java.io.IOException;
import java.net.URI;

public class HdfsFileList {
    public static void main(String[] args) throws IOException {
        String hdfsPath = "hdfs://localhost:9000";
        Path path = new Path(hdfsPath + "/test");
        //本地客户端如果要用到hdfs环境,就需要配置文件
        //创建配置对象,主要用于读写配置文件: hdfs-site.xml core-site.xml map.xml
        Configuration conf = new Configuration();
        //访问hdfs需返回一个对象,这个对象是FileSystem
        FileSystem fs = FileSystem.get(URI.create(hdfsPath), conf);
        //得到该目录下的全部文件
        FileStatus[] fileList = fs.listStatus(path);
        for (FileStatus f : fileList) {
            System.out.printf("name: %s   |   folder: %s  |   size: %d\n", f.getPath(), f.isDir(), f.getLen());
        }
        fs.close();
    }
}

7.创建目录

fs.mkdirs(path);

public void mkdir(String folder) throws Exception{
		//与hdfs建立联系
		FileSystem fs = FileSystem.get(new URI(HDFS),new Configuration());
		Path path = new Path(folder);
		if (!fs.exists(path)) {
             //在HDFS上指定的路径创建目录
			fs.mkdirs(path);
			System.out.println("创建成功: " + folder);
		}else{
			System.out.println("文件夹已存在:" + folder);
		}
		fs.close();	
	}
	

猜你喜欢

转载自blog.csdn.net/Qmilumilu/article/details/104645092