问题遇到的现象和发生背景
自己实现的一个简单的HDFS查看器源码。使用Eclipse,java编程,在hadoop上运行。
用代码块功能插入代码,请勿粘贴截图
Main.java
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
//import TryTree.MyTreeListener;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.border.EtchedBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
public class Main implements TreeSelectionListener, ActionListener {
JFrame mainFrame;
JPanel panelLeft;
JPanel panelMain;
JPanel panelTop;
JPanel panelBottom;
JTree treeFileSystem;
private JScrollPane jScrollPane1;
private JTree tree;
// 创建根节点
private DefaultMutableTreeNode dmtnRoot;
// 创建树的数据模型
private DefaultTreeModel dtm;
// 用来记录当前选中节点的成员变量
private DefaultMutableTreeNode tempNode;
private DefaultMutableTreeNode selNode = null;
String selPath = null;
JTextArea textFile;
JLabel textStatus;
Map fileMaps;
JButton btnRefresh = new JButton("刷新");
JButton btnAddDir = new JButton("新建目录");
JButton btnDelDir = new JButton("删除");
JButton btnView = new JButton("查看");
private JToolBar toolBarFuncs = new JToolBar();
/**
* @param args
*/
public static void main(String[] args) {
Main mainObj = new Main();
}
public Main() {
// TODO Auto-generated method stub
// 初始化FRAME对象
mainFrame = new JFrame(); // 创建FRAME
mainFrame.setSize(new Dimension(800, 620));// 定义窗口大小
mainFrame.setLocationRelativeTo(null); // 设置居中显示
mainFrame.setTitle("HDFS文件系统查看器");// 定义标题
mainFrame.setLayout(null); // 设置布局方式
// 初始化普通对象
fileMaps = new HashMap();
// 初始化显示面板
panelMain = new JPanel();
mainFrame.add(panelMain); // 加入frame中
panelMain.setBounds(200, 30, 600, 540);// 设置位置
panelMain.setBorder(new EtchedBorder(1)); // 设置边框
panelMain.setLayout(null); // 设置布局
panelMain.setVisible(true);// 设置显示
// 初始化文本框
textFile = new JTextArea(); // 创建文本框
//textFile.setBounds(0, 0, 600, 535);// 设置位置
textFile.setWrapStyleWord(false);
JScrollPane scrollPanetxt = new JScrollPane(textFile);
scrollPanetxt.setBounds(0, 0, 600, 535);// 设置位置
panelMain.add(scrollPanetxt); // 加入panel中
textFile.setLineWrap(true);
textFile.setVisible(true);// 设置显示
// 初始化树面板
panelLeft = new JPanel(); // 创建PANEL
mainFrame.add(panelLeft); // 加入frame中
panelLeft.setBounds(0, 30, 200, 540);// 设置位置
panelLeft.setBorder(new EtchedBorder(1)); // 设置边框
panelLeft.setLayout(null); // 设置布局
panelLeft.setVisible(true);// 设置显示
// 初始化树控件
dmtnRoot = new DefaultMutableTreeNode("/");
dtm = new DefaultTreeModel(dmtnRoot);
tree = new JTree(dtm); // 创建树控件
refreshTreeNodes();// 刷新显示树内容
tree.setShowsRootHandles(true);// 设置JTree将显示根节点的控制图标
JScrollPane scrollPanetree = new JScrollPane(tree);
scrollPanetree.setBounds(0, 0, 200, 540);// 设置位置
panelLeft.add(scrollPanetree); // 加入panel中
tree.setVisible(true);// 设置显示
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);
// 初始化工具栏面板
panelTop = new JPanel(); // 创建PANEL
mainFrame.add(panelTop); // 加入frame中
panelTop.setBounds(0, 0, 800, 30);// 设置位置
panelTop.setBorder(new EtchedBorder(1)); // 设置边框
panelTop.setLayout(null); // 设置布局
panelTop.setVisible(true);// 设置显示
// 初始化按钮控件
toolBarFuncs.add(btnRefresh);
toolBarFuncs.add(btnAddDir);
toolBarFuncs.add(btnDelDir);
toolBarFuncs.add(btnView);
btnRefresh.addActionListener(this);
btnAddDir.addActionListener(this);
btnDelDir.addActionListener(this);
btnView.addActionListener(this);
panelTop.add(toolBarFuncs); // 加入frame中
toolBarFuncs.setBounds(0, 0, 800, 30);// 设置位置
toolBarFuncs.setVisible(true);// 设置显示
// 初始化状态栏面板
panelBottom = new JPanel(); // 创建PANEL
mainFrame.add(panelBottom); // 加入frame中
panelBottom.setBounds(0, 570, 800, 30);// 设置位置
panelBottom.setBorder(new EtchedBorder(1)); // 设置边框
panelBottom.setLayout(null); // 设置布局
panelBottom.setVisible(true);// 设置显示
// 初始化状态文本框
textStatus = new JLabel(); // 创建文本框
textStatus.setBounds(0, 0, 800, 30);// 设置位置
panelBottom.add(textStatus);
textStatus.setEnabled(false);
textStatus.setVisible(true);// 设置显示
// 显示主窗口
mainFrame.setVisible(true);
// mainFrame.addWindowListener(new WindowAdapter() {
// public void windowClosing(WindowEvent e) {
// System.out.println("out");
// }
// });
}
public void refreshTreeNodes() {
// 获取信息
InputStream is = null;
InputStreamReader isr = null;
BufferedReader brStat = null;
StringTokenizer tokenStat = null;
try {
// 清空
fileMaps.clear();
dmtnRoot.removeAllChildren();
// 添加根目录
FileObject rootFile = new FileObject();
rootFile.isFile = false;
rootFile.path = "/";
rootFile.size = "0";
fileMaps.put("/", rootFile);
Process process = Runtime.getRuntime().exec("./hadoop fs -lsr /");
is = process.getInputStream();
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);
// 节点
String strLine = null;
strLine = brStat.readLine();
// 当前节点
DefaultMutableTreeNode tempNode = null;
while (strLine != null) {
tokenStat = new StringTokenizer(strLine);
FileObject file = new FileObject();
file.mode = tokenStat.nextToken();
tokenStat.nextToken();
file.user = tokenStat.nextToken();
file.group = tokenStat.nextToken();
String fileSize = tokenStat.nextToken();
file.size = fileSize;
file.time = tokenStat.nextToken() + " " + tokenStat.nextToken();
String pathName = tokenStat.nextToken();
file.path = pathName;
// 一级目录
if (pathName.lastIndexOf("/") == 0) {
tempNode = new DefaultMutableTreeNode(pathName.substring(1));
dmtnRoot.add(tempNode);
file.isFile = false;
fileMaps.put(pathName, file);
}
// 其它目录
else if (fileSize.equalsIgnoreCase("0")) {
String tmpPath = pathName.substring(0, pathName
.lastIndexOf("/"));
String parPath = tmpPath.substring(
tmpPath.lastIndexOf("/") + 1, tmpPath.length());
String curPath = pathName.substring(pathName
.lastIndexOf("/") + 1, pathName.length());
if (parPath.equalsIgnoreCase(tempNode.toString())) // temp节点就是上级目录
{
DefaultMutableTreeNode temp = new DefaultMutableTreeNode(
curPath);
tempNode.add(temp);
tempNode = temp;
} else {
DefaultMutableTreeNode temp = (DefaultMutableTreeNode) tempNode
.getParent();
while (parPath.equalsIgnoreCase(temp.toString()) == false) {
temp = (DefaultMutableTreeNode) temp.getParent();
}
tempNode = new DefaultMutableTreeNode(curPath);
temp.add(tempNode);
}
file.isFile = false;
fileMaps.put(pathName, file);
}
// 文件
else {
String tmpPath = pathName.substring(0, pathName
.lastIndexOf("/"));
String parPath = tmpPath.substring(
tmpPath.lastIndexOf("/") + 1, tmpPath.length());
String curPath = pathName.substring(pathName
.lastIndexOf("/") + 1, pathName.length());
if (parPath.equalsIgnoreCase(tempNode.toString())) // temp节点就是上级目录
{
DefaultMutableTreeNode temp = new DefaultMutableTreeNode(
curPath);
tempNode.add(temp);
} else {
DefaultMutableTreeNode temp = (DefaultMutableTreeNode) tempNode
.getParent();
while (parPath.equalsIgnoreCase(temp.toString()) == false) {
temp = (DefaultMutableTreeNode) temp.getParent();
}
tempNode = new DefaultMutableTreeNode(curPath);
temp.add(tempNode);
}
file.isFile = true;
fileMaps.put(pathName, file);
}
strLine = brStat.readLine();
}
((DefaultTreeModel) tree.getModel()).reload(dmtnRoot);
tree.expandRow(0);
}
catch (Exception e) {
textStatus.setText(e.toString());
}
}
// 节点选种事件
@Override
public void valueChanged(TreeSelectionEvent e) {
JTree tree = (JTree) e.getSource();
// 利用JTree的getLastSelectedPathComponent()方法取得目前选取的节点.
selNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (selNode == null) {
textStatus.setText("未选中节点!");
selPath = null;
return;
}
String str = e.getPath().toString().substring(1,
e.getPath().toString().length() - 1);
String[] arr = str.split(",");
String pathName = arr.length <= 1 ? "/" : "";
// String pathName = "/";
for (int i = 1; i < arr.length; i++) {
pathName += "/" + arr[i].trim();
}
// pathName = pathName.substring(0, pathName.length()-1);
selPath = pathName;
// textStatus.setText(e.getPath().toString()+". "+selPath+".
// "+arr.toString());
FileObject file = (FileObject) fileMaps.get(pathName);
if (file == null)
return;
if (file.isFile) {
textStatus.setText("文件:" + pathName+", 大小:"+file.size+"字节.");
} else {
textStatus.setText("目录:" + pathName);
}
}
// 按钮监听事件
@Override
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == btnRefresh) {// 刷新
refreshTreeNodes();
} else if (e.getSource() == btnAddDir) {// 新建目录
if (selNode == null
|| ((FileObject) fileMaps.get(selPath)).isFile) {
JOptionPane.showMessageDialog(mainFrame, "请选择目录!!!", "错误",
JOptionPane.WARNING_MESSAGE);
return;
} else {
String tempName = JOptionPane
.showInputDialog(mainFrame, "请输入新目录名称", "请输入:",
JOptionPane.INFORMATION_MESSAGE);
if (checkPathValidate(tempName.trim()) == false) {
JOptionPane.showMessageDialog(mainFrame, "输入目录格式无效!!!",
"错误", JOptionPane.WARNING_MESSAGE);
return;
} else {
// 添加目录
Process process = Runtime.getRuntime()
.exec(
"./hadoop fs -mkdir "
+ (selPath.length() == 1 ? ""
: selPath) + "/"
+ tempName.trim());
// textStatus.setText("./hadoop fs -mkdir
// "+(selPath.length()==1?"":selPath)+"/"+tempName.trim());
refreshTreeNodes();
}
textStatus.setText("文件树已刷新!");
// Process process = Runtime.getRuntime().exec("./hadoop fs
// -mkdir "+pathName);
}
} else if (e.getSource() == btnDelDir) {// 删除
if (selNode == null) {
JOptionPane.showMessageDialog(mainFrame, "请选择目录或文件!!!",
"错误", JOptionPane.WARNING_MESSAGE);
return;
}
int confirm = JOptionPane
.showConfirmDialog(mainFrame, ((FileObject) fileMaps
.get(selPath)).isFile ? "您确定要删除该文件么?"
: "您确定要删除该目录么?", "确认",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
// 确认需要删除执行的代码
if (confirm == JOptionPane.YES_OPTION) {
String temp = selPath;
if (((FileObject) fileMaps.get(selPath)).isFile == false) {
Process process = Runtime.getRuntime().exec(
"./hadoop fs -rmr " + selPath);
refreshTreeNodes();
} else {
if (((FileObject) fileMaps.get(selPath)).isFile) {
Process process = Runtime.getRuntime().exec(
"./hadoop fs -rm " + selPath);
refreshTreeNodes();
}
}
textStatus.setText(temp+"已删除!");
}
} else if (e.getSource() == btnView) {// 查看
if (selNode == null) {
JOptionPane.showMessageDialog(mainFrame, "请选择目录或文件!!!",
"错误", JOptionPane.WARNING_MESSAGE);
return;
}
FileObject file = ((FileObject) fileMaps.get(selPath));
if (file.isFile) {
// 查看文件
InputStream is = null;
InputStreamReader isr = null;
BufferedReader brStat = null;
StringTokenizer tokenStat = null;
try {
Process process = Runtime.getRuntime().exec("./hadoop fs -cat " + selPath);
is = process.getInputStream();
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);
String strLine = null;
textFile.setText("");
if (Long.parseLong(file.size) > 1024 * 1024) {
return;
}
while ((strLine = brStat.readLine()) != null) {
for(int i=0;i<strLine.length();i++) {
int code = strLine.codePointAt(i);
if(code ==12) { //换页
textFile.append("\n");
} else if(code ==27) { //esc
textFile.append("");
} else if(code ==8) { //退格
textFile.setText(textFile.getText().substring(0,textFile.getText().length()-1));
} else {
//测试
//textFile.append(Integer.toString(code)+":");
textFile.append(strLine.substring(i,i+1));
}
}
textFile.append("\n");
}
textFile.enable(false);
//textStatus.setText("文件大小: "+file.size);
} catch (Exception exp) {
textStatus.setText(exp.toString());
}
} else {
// 查看目录,等于是查看ls命令
textFile.setText("");
textFile.append("found "+ selNode.getChildCount() +" items\n");
for(int i=0;i<selNode.getChildCount();i++)
{
String pathName = selPath + "/"+selNode.getChildAt(i).toString();
//获取子目录路径
FileObject fileObj = (FileObject)fileMaps.get(pathName);
if(fileObj != null) {
String line = fileObj.mode + "\t"+
fileObj.user +"\t" +
fileObj.group +"\t"+
fileObj.size +"\t"+
fileObj.time+"\t"+
fileObj.path;
textFile.append(line+"\n\n");
}
}
textFile.enable(false);
}
}
} catch (Exception exp) {
textStatus.setText(exp.toString());
}
}
// true: ok;false: notok
public boolean checkPathValidate(String path) {
boolean res = true;
if (!path.matches("([a-z]|[A-Z])([a-z]|[A-Z]|[0-9]|[-,_,.])*")) {
res = false;
}
return res;
}
private class FileObject {
public String mode;
public String user;
public String group;
public String time;
public String size;
public String path;
public boolean isFile;
}
}
运行结果及报错内容
我的解答思路和尝试过的方法
怀疑未初始化,咱没有找到方法
我想要达到的结果
程序运行正常,实现本地图形化界面对hdfs文件系统的操作