首页 > 其他分享 >使用HDFS做一个记事本功能

使用HDFS做一个记事本功能

时间:2022-11-03 21:55:55浏览次数:66  
标签:HDFS 功能 fs System hadoop Path new 记事本 out

 

 

 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>ceshi</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-yarn-api</artifactId>
            <version>3.1.3</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

 

HDFS.java

package lianxi;

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

import java.io.*;
import java.util.Scanner;

public class HDFS {

    public static FileSystem fs = null;
    Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        Scanner sc=new Scanner(System.in);
        Configuration configuration = new Configuration();
        System.setProperty("HADOOP_USER_NAME","hadoop");
        configuration.set("fs.defaultFS","hdfs://hadoop1:8020");
        fs = FileSystem.get(configuration);
        for (int i=0;;i++){
            System.out.println("请选择功能:1.创建文件 2.追加文件内容 3. 删除文件 4. 更改存储位置  5. 退出");
            int n=sc.nextInt();
            if (n==1){
                System.out.print("请输入将要创建文件的路径:");
                String path = sc.next();
                create(path);
            }else if (n==2){
                System.out.print("请输入想要追加文件的路径:");
                String path1 = sc.next();
                update(path1);
            }else if (n==3){
                System.out.println("请输入删除文件地址");
                String w=sc.next();
                delete(w);
            }else if (n==4){
                    System.out.println("请输入原地址");
                    String xinxi=sc.next();
                    String pp=readFromHdfs(xinxi);
                    System.out.print("请输入将要创建文件的路径:");
                    String pathw = sc.next();
                    create(pathw,pp);
                    delete(xinxi);
            }
            else break;
        }
        fs.close();
    }
    public static void create(String path) throws IOException {
        if(!fs.exists(new Path(path))){
            Scanner sc=new Scanner(System.in);
            FSDataOutputStream outputStream = fs.create(new Path(path));
            System.out.println("请输入需要文件中的内容");
            String s=sc.next();
            outputStream.writeUTF(s);
            System.out.println("创建文件夹成功");
            outputStream.flush();
            outputStream.close();
            FSDataInputStream open = fs.open(new Path(path));
            System.out.println(open.readUTF());
            open.close();
        }
    }
    public static void create(String path, String pp) throws IOException {
        if(!fs.exists(new Path(path))){
            Scanner sc=new Scanner(System.in);
            FSDataOutputStream outputStream = fs.create(new Path(path));
            System.out.println("请输入需要文件中的内容");
            outputStream.writeUTF(pp);
            System.out.println("创建文件夹成功");
            outputStream.flush();
            outputStream.close();
            FSDataInputStream open = fs.open(new Path(path));
            System.out.println(open.readUTF());
            open.close();
        }
    }
    public static void update(String path) throws IOException {

        Scanner sc = new Scanner(System.in);
        FSDataOutputStream append = fs.append(new Path(path));
        System.out.print("追加的内容");
        String s1 = sc.next();
        append.writeUTF(s1);
        append.flush();
        append.close();
        FSDataInputStream open = fs.open(new Path(path));
        System.out.println(open.readUTF());
    }
    public static void delete(String w) throws IOException {
        //带删除文件的目录路径
        Path src = new Path(w);
        //删除
        fs.delete(src,true);
        System.out.println("删除成功");
    }
    public static String readFromHdfs(String filePath) throws IOException{
        //读
        Path inFile = new Path(filePath);
        FSDataInputStream inputStream = fs.open(inFile);
        String pp=inputStream.readUTF();
        inputStream.close();
        return pp;
    }
}

 

 

test

package lianxi;

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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class test {
    public static FileSystem fs = null;
    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        conf.set("fs.defaultFS", "hdfs://hadoop1:8020");
        fs = FileSystem.get(conf);
        if(!fs.exists(new Path("/lyh"))){
            fs.create(new Path("/lyh"));
        }

        create();

        copy();

        fs.close();
    }

    public static void create() throws Exception {
        if(!fs.exists(new Path("/ceshi/hdfstest1.txt"))){
            FSDataOutputStream output = fs.create(new Path("/ceshi/hdfstest1.txt"));
            output.write("20203959 李迎辉 HDFS课堂测试".getBytes());
            output.flush();
            System.out.println("创建获得hdfstest1成功");
            output.close();
        }
        else{
            System.out.println("创建获得hdfstest1失败");
        }
    }

    public static void copy() throws Exception {
        FSDataInputStream fin = fs.open(new Path("/test/hdfstest1.txt"));
        BufferedReader in = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
        if(!fs.exists(new Path("/test/hdfstest2.txt"))){
            FSDataOutputStream fout  = fs.create(new Path("/test/hdfstest2.txt"));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fout, "UTF-8"));
            out.write(in.readLine());
            System.out.println("创建hdfstest2文件成功");
            out.flush();
            out.close();
        }else{
            System.out.println("创建hdfstest2文件失败");
        }

    }
}

 

标签:HDFS,功能,fs,System,hadoop,Path,new,记事本,out
From: https://www.cnblogs.com/1774323810com/p/16855981.html

相关文章

  • MSE 风险管理功能发布
    作者:三辰前言大家好,今天给大家带来MSE高可用方向的重要功能——风险管理的发布。阅读这篇文章,你将能够了解以下知识点和能力:熟悉微服务体系高可用如何设计掌握如何......
  • Java实现ip属地功能开发教程 | ip2region2.x使用总结
    ip属地功能开发-ip2region2.x使用总结一、前言如今许多软件如B站、微博、抖音等都加上IP归属地防止恶意评论,境外用户显示的是国家,国内的用户显示的省份。兴致一起,我便......
  • C# 9.0 添加和增强的功能【基础篇】
    一、记录(record)C#9.0引入了记录类型。可使用 record关键字定义一个引用类型,以最简的方式创建不可变类型。这种类型是线程安全的,不需要进行线程同步,非常适合并行计算......
  • 微信小程序 图片上传功能可能会遇到的跨域访问图片问题
    小程序里遇到图片跨域访问的问题时,需要在uploadFile合法域名这里添加图片的访问域名就可以了 ......
  • 【Web开发】Python实现Web图表功能(pyecharts入门学习)
    <fontcolor=purpleface=华文行楷size="5">"柳丝榆荚自芳菲,不管桃飘与李飞;"1、简介APythonEchartsPlottingLibrary.ApacheEcharts是一个由百度开源的数据可视化......
  • 使用MVC实现登录注册功能(数据保存到数据库)详细讲解以及代码
    M:代表模型层,解决问题的功能具体实现。比如向数据库添加数据、查询数据V:代表视图,用户和机器的交互页面。用来展示信息(一般用html,js,css...)C:控制层,用来连接用户提交的操作......
  • 网络交换机的功能和简介
     网络交换机是一个扩展网络的设备,可以在子网中提供更多的连接端口,以便连接更多的计算机。它具有高性价比、高灵活性、相对简单和易于实现的特点。那么,网络交换机有哪些功......
  • kindeditor富文本实现导入word功能
    ​ ueditor粘贴不能粘贴word中的图片是一个很头疼的问题,在我们的业务场景中客户要求必须使用ueditor并且支持word的图片粘贴,因为这个需求头疼了半个月,因为前端方面因为安......
  • 前端多方式登录功能完成
    逻辑导航1.当在前端输入用户名和密码之后,点击登录,后端校验完毕返回前端2.前端拿到需要首先做个判断,判断用户是否输入用户名和密码,未输入则发出提示;输入了则发送post请求......
  • 4款实用的黑科技软件,白嫖党最爱,功能强大到离谱
    闲话少说,直上干货。1、Dism++这是一款国人研发,免费又好用的电脑优化工具,备受全球电脑爱好者追捧,它解决了系统安装与维护两大痛点问题——自定义设置与优化,相当于给电脑请......