本篇文章旨在完成一个Swing界面版的HDFS控制程序:
1.新建一个testHDFS3项目
2.因为我们上一个项目是使用分层开发,所以本次HDFSService类还和上个项目保持一样,只需要修改HDFSView:
package view;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import service.HDFSService;
public class HDFSView {
String local;
String hdfs;
// 本地文件的文件选择器、按钮、显示文件路径标签
JFileChooser fileChooser;
JButton localBtn;
JLabel localText;
JButton uploadBtn;
JButton downBtn;
JTextField hDFSText;
public void initView() {
JFrame frame = new JFrame();
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel mainPanel = (JPanel) frame.getContentPane();
BoxLayout layout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
mainPanel.setLayout(layout);
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
mainPanel.add(panel1);
mainPanel.add(panel2);
mainPanel.add(panel3);
JLabel localLabel = new JLabel("本地文件:");
//文件选择器
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
localBtn = new JButton("选择文件");
localBtn.setPreferredSize(new Dimension(120, 30));
localText = new JLabel("无");
panel1.add(localLabel);
panel1.add(localBtn);
panel1.add(localText);
JLabel hDFSLabel = new JLabel("HDFS文件:");
hDFSText = new JTextField();
hDFSText.setPreferredSize(new Dimension(120, 30));
panel2.add(hDFSLabel);
panel2.add(hDFSText);
uploadBtn = new JButton("上传");
uploadBtn.setPreferredSize(new Dimension(120, 30));
downBtn = new JButton("下载");
downBtn.setPreferredSize(new Dimension(120, 30));
panel3.add(uploadBtn);
panel3.add(downBtn);
frame.setVisible(true);
}
public void setFunction() {
HDFSService service = new HDFSService();
service.init();
// 设置本地文件按钮可以选择本地文件
localBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int option = fileChooser.showOpenDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
local = fileChooser.getSelectedFile().getAbsolutePath();
localText.setText(local);
}
}
});
// 上传按钮
uploadBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hdfs = hDFSText.getText();
service.put(local, hdfs);
}
});
// 下载按钮
downBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hdfs = hDFSText.getText();
service.put(local, hdfs);
}
});
}
public static void main(String[] args) {
HDFSView hv = new HDFSView();
hv.initView();
hv.setFunction();
}
}
service包下的HDFSService.java没变:
package service;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HDFSService {
FileSystem fs;
public HDFSService() {
try {
URI uri = new URI("hdfs://192.168.150.21:9000");
Configuration conf = new Configuration();
// conf.set("dfs.replication", "2");
// conf.set("dfs.block.size", "64m");
conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
fs = FileSystem.get(uri, conf, "root");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
public boolean put(String local, String hdfs) {
boolean flag = false;
try {
fs.copyFromLocalFile(new Path(local), new Path(hdfs));
flag = true;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
// 下载需要配置hadoop_home
public boolean get(String local, String hdfs) {
boolean flag = false;
try {
fs.copyToLocalFile(new Path(hdfs), new Path(local));
flag = true;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
}
3.运行
windows:
linux: