zoukankan      html  css  js  c++  java
  • 11111

     package com.immoc.hadoop;

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.*;
    import org.apache.hadoop.io.IOUtils;
    import org.apache.hadoop.util.Progressable;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;

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

    /**
     * Hadoop HDFS Java API 操作
     */
    public class HDFS_Classtest2 {

        public static final String HDFS_PATH = "hdfs://192.168.16.132:8020";

        FileSystem fileSystem = null;
        Configuration configuration = null;



        /**
         * 创建HDFS目录
         */
        public  void mkdir() throws Exception {
            fileSystem.mkdirs(new Path("/信1605-1班/20163694"));
        }

        /**
         *  [1]:新建:新建文本文件;
         */
        @Test
        public int create() throws Exception {
            FSDataOutputStream output = fileSystem.create(new Path("/信1605-1班/20163694/李婷婷.txt"));
            output.write("hello hadoop 20163694李婷婷 ".getBytes());
            System.out.println("新建:/信1605-1班/20163694/李婷婷.txt成功");
            output.flush();
            output.close();
            return 0;
        }

        /**
         * [2] 打开:打开指定目录下的文本文件,并将文本内容显示在编辑区
         */
        @Test
        public int  cat() throws Exception {
            FSDataInputStream in = fileSystem.open(new Path("/信1605-1班/20163694/李婷婷.txt"));
            System.out.println(" 文件内容为:");
            IOUtils.copyBytes(in, System.out, 1024);
            System.out.println("");
            in.close();
            return 0;
        }


        /**
         * (3) 编辑:向 HDFS 中指定的文件追加内容,由用户指定内容追加到原有文件的开头
         * 或结尾;
         */
        @Test
         public int add(String str ) throws Exception {
            FSDataOutputStream output = fileSystem.create(new Path("/信1605-1班/20163694/李婷婷.txt"));
            String s2 = "hello hadoop 20163694李婷婷";
            String s1 = s2+str;
            output.write(s1.getBytes());
            output.flush();
            output.close();
            return 0;
        }


        /**
         *  移动文件
         */
        @Test
        public int mv() throws Exception {
            Configuration conf = new Configuration();
            conf.set("fs.default.name","hdfs://localhost:9000");
            String remoteFilePath = "hdfs:///user/hadoop/text.txt";    // 源文件 HDFS 路径
            String remoteToFilePath = "hdfs:///user/hadoop/new.txt";    // 目的 HDFS 路径
            FileSystem fs = FileSystem.get(conf);
            Path srcPath = new Path(remoteFilePath);
            Path dstPath = new Path(remoteToFilePath);
            boolean result = fs.rename(srcPath, dstPath);
            fs.close();
            return result;
        }
        @Before
        public void setUp() throws Exception {
            System.out.println("连接到HDFS文件操作系统");
            configuration = new Configuration();
            fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "jasmine");
        }

        @After
        public void tearDown() throws Exception {
            configuration = null;
            fileSystem = null;

            System.out.println("操作结束,已清除缓存 ");
        }

    }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     import com.immoc.hadoop.HDFSApp;
    import com.immoc.hadoop.HDFS_Classtest2;
    import org.junit.Test;

    import java.util.Scanner;

    public class Input {
        public static void main(String[] args)throws Exception {
            int flag =1;
            while(flag !=0){
            System.out.println("欢迎来到HDFS文件操作编辑器: 输入操作功能的序号: ");
            System.out.println("[1]:新建:新建文本文件");
            System.out.println("[2] 打开:打开指定目录下的文本文件,并将文本内容显示在编辑区");
            System.out.println("[3]编辑:向 HDFS 中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;");
            System.out.println("[4]退出系统");
            System.out.println("输入操作功能的序号: ");
            Scanner in = new Scanner(System.in);
            int select = in.nextInt();
            HDFS_Classtest2 hdfs = new HDFS_Classtest2();
            hdfs.setUp();
            if(select==1){
                hdfs.create();
            }else if(select==2){
                hdfs.cat();
            }else if(select==3){
                System.out.println("请输入您想添加的内容:");
               String str = in.next();
               hdfs.add(str);
            }else if(select==4){
               flag=0;
            }else{
                System.out.println("输入错误请重新输入!");
            }

            hdfs.tearDown();

            }
        }
    }


        
  • 相关阅读:
    c语言排序算法
    冒泡 选择排序
    冒泡排序算法
    Pandas数据预处理
    Mongodb的安装和配置
    Mysql练习题
    5 根据过去的行为能否预测当下
    Sklearn逻辑回归
    4 如何通过各种广告组合获取更多的用户
    Sklearn多元线性回归
  • 原文地址:https://www.cnblogs.com/lijing925/p/9760875.html
Copyright © 2011-2022 走看看