设为首页 加入收藏

TOP

JAVA实现批量上传本地文件至HDFS
2019-04-22 00:10:54 】 浏览:58
Tags:JAVA 实现 批量 上传 本地 文件 HDFS

前言

小白一枚,本文简单实现了批量上传遥感影像至HDFS,所以没有实现窗体简单的关闭、缩小、取消等功能。重申这只是简单demo!话不多说直接上代码。

@MarcusPlus
package com.hdfs;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

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


public class Demo {

    public static void main(String[] args) {    
        // 创建 JFrame 实例
        JFrame frame = new JFrame("Demo");
        // Setting the width and height of frame
        frame.setSize(512, 512);
        frame.setLocation(400, 200);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /* 创建面板,这个类似于 HTML 的 div 标签
         * 我们可以创建多个面板并在 JFrame 中指定位置
         * 面板中我们可以添加文本字段,按钮及其他组件。
         */
        JPanel panel = new JPanel();    
        // 添加面板
        frame.add(panel);
        /* 
         * 调用用户定义的方法并添加组件到面板
         */
        placeComponents(panel);

        // 设置界面可见
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {

        /* 
         * 这边设置布局为 null
         */
        panel.setLayout(null);

        // 创建select按钮
        JButton selectButton = new JButton("Select Local File");
        selectButton.setBounds(10, 80, 80, 25);
        panel.add(selectButton);
        selectButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JFileChooser jf = new JFileChooser();  
                jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
                jf.setDialogTitle("请选择要上传的文件夹...");
                jf.showDialog(null,null);

                String srcPath=jf.getSelectedFile().getAbsolutePath()+"/*";
                String[] name=jf.getSelectedFile().getAbsolutePath().split("/");            
                String dstPath="hdfs://127.0.0.1:9000/user/test/"+name[name.length-1]+"/";          

                if(srcPath.isEmpty()){
                    System.out.println("请选择本地路径!");
                }else
                {
                    try {
                        listFile(srcPath, dstPath);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

                System.out.println("Sucess!");
            }           
        });

    }

    protected static void listFile(String srcPath, String dstPath) throws Exception{
        // TODO Auto-generated method stub
        FileSystem fs = null;
        FileSystem local = null;
        //读取配置文件
        Configuration conf=new Configuration();
        //指定HDFS地址
        URI uri=new URI("hdfs://127.0.0.1:9000");
        fs=FileSystem.get(uri,conf);
        // 获取本地文件系统
        local=FileSystem.getLocal(conf);
        //获取文件目录
        FileStatus[] listFile=local.globStatus(new Path(srcPath), new RegxAcceptPathFilter("^.*tif$"));
        //获取文件路径
        Path[]  listPath=FileUtil.stat2Paths(listFile);
        //输出文件路径
        Path outPath=new Path(dstPath);
        boolean result = fs.isDirectory(outPath);

        if(result==true){           
            //循环遍历所有文件路径
            for(Path p:listPath){fs.copyFromLocalFile(p, outPath);}         
            }else{              
                fs.mkdirs(outPath);
                System.out.println("创建路径: "+outPath);
                for(Path p:listPath){fs.copyFromLocalFile(p, outPath);}
            }       
    }
}
@MarcusPlus
package com.hdfs;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;

public class RegxAcceptPathFilter implements PathFilter{
    private  final String regex;
    public RegxAcceptPathFilter(String regex) {
        this.regex=regex;
    }
    @Override
    public boolean accept(Path path) {

        boolean flag=path.toString().matches(regex);
        return flag;
    }
}

解释

RegxAcceptPathFilter是用来将本地文件夹下非.tif文件进行过滤掉,以便顺利建立遥感影像库。虽然界面很丑,但是毕竟是个简单demo,别吐槽我啦~

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Sqoop1.99 从SQL Server导数据到H.. 下一篇通过Java代码对HDFS进行写入与查..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目