Hadoop2. 8.5 detailed tutorial (x) interface version HDFS control program

Miss Zhu 2022-02-13 08:38:59 阅读数:672

hadoop2. hadoop detailed tutorial interface

This article aims to complete a Swing Interface version HDFS Control procedures :

 Insert picture description here
1. Create a new one testHDFS3 project
 Insert picture description here
2. Because our last project used layered development , So this time HDFSService Class remains the same as the previous project , It just needs to be modified 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;
// File selector for local files 、 Button 、 Show file path label 
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(" Local files :");
// File selector 
fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
localBtn = new JButton(" Select File ");
localBtn.setPreferredSize(new Dimension(120, 30));
localText = new JLabel(" nothing ");
panel1.add(localLabel);
panel1.add(localBtn);
panel1.add(localText);
JLabel hDFSLabel = new JLabel("HDFS file :");
hDFSText = new JTextField();
hDFSText.setPreferredSize(new Dimension(120, 30));
panel2.add(hDFSLabel);
panel2.add(hDFSText);
uploadBtn = new JButton(" Upload ");
uploadBtn.setPreferredSize(new Dimension(120, 30));
downBtn = new JButton(" download ");
downBtn.setPreferredSize(new Dimension(120, 30));
panel3.add(uploadBtn);
panel3.add(downBtn);
frame.setVisible(true);
}
public void setFunction() {

HDFSService service = new HDFSService();
service.init();
// The set local file button allows you to select a local file 
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);
}
}
});
// Upload button 
uploadBtn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

hdfs = hDFSText.getText();
service.put(local, hdfs);
}
});
// Download button 
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 Under bag HDFSService.java It hasn't changed :

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;
}
// Configuration required for download 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. function
windows:
 Insert picture description here

linux:
 Insert picture description here

copyright:author[Miss Zhu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130838569535.html