hadoop文件系统浏览器

最近一段时间学习了java和hadoop,琢磨着能不能做个简单的文件浏览器,实现文件夹的浏览等简单功能,于是在网上下载了hadoop0.21版本和eclipse,花了一天左右的时间实现了简单的文件和文件夹的浏览功能,eclipse搭建hadoop应用程序的步骤在我的博客上有写过程,http://blog.csdn.net/devil27/article/details/8480345,以下是实现源码,由于java和hadoop都是最近才开始学,里面还有很多需要完善的东西,希望各位高手指点指点,我在hadoop-0.21上测试正常!欢迎拍砖!!!这个帖子我在hadoopor.com上面也有发表

package HadoopFileView.yuht.com;



import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;


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


//import org.apache.hadoop.conf.Configured;


public class HadoopFileView extends JPanel implements ActionListener,
MouseListener {
/**

*/
private static final long serialVersionUID = 1L;
private JButton buttonUP;
private JComboBox comboboxPath;
private JTable table;
private DefaultTableModel dtmFile;
private JLabel jlable;
private String file;
private String currentPath;
private hadoopFile hadoopfile;
private JTextArea text;


// private int currentIndex;


class LocalTableModel extends DefaultTableModel {
/**

*/
private static final long serialVersionUID = 1L;


public boolean isCellEditable(int row, int column) {
return false;
}
}


public HadoopFileView() throws IOException {
super(new BorderLayout());
JPanel jp = new JPanel(new BorderLayout());
buttonUP = new JButton("up");
buttonUP.addActionListener(this);
comboboxPath = new JComboBox();
hadoopfile = new hadoopFile();
comboboxPath.addActionListener(this);
text = new JTextArea();
text.setSize(400, 200);
jp.add(buttonUP, "West");
jp.add(comboboxPath, "Center");
jp.add(new JScrollPane(text), "South");
dtmFile = new LocalTableModel();
dtmFile.addColumn("name");
dtmFile.addColumn("size");
dtmFile.addColumn("type");
dtmFile.addColumn("time");
table = new JTable(dtmFile);
table.setShowGrid(false);
table.addMouseListener(this);
jlable = new JLabel("hadoop", JLabel.CENTER);
add(jp, "North");
add(new JScrollPane(table), "Center");
add(jlable, "South");
// add(new JScrollPane(text),"South");
// path = new File("/");
// listFiles(path);
HadoopShowFiles("hdfs://hadoopmaster:9000");
// HadoopShowFiles("/");
}


private static FileStatus[] listStatus(FileSystem srcFs, FileStatus src) {
if (src.isFile()) {
FileStatus[] files = { src };
return files;
}
Path path = src.getPath();
try {
FileStatus[] files = srcFs.listStatus(path);
return files;
} catch (IOException e) {


}
return null;
}


private int lsfile(FileStatus src, FileSystem srcFs) throws IOException {
final FileStatus[] items = listStatus(srcFs, src);
dtmFile.setRowCount(0);
if (items == null)
return -1;
for (int i = 0; i < items.length; i++) {
FileStatus stat = items[i];
Path cur = stat.getPath();
Long size = stat.getLen();
SimpleDateFormat simpledate = new SimpleDateFormat();
String date = simpledate
.format(new Date(stat.getModificationTime()));
// Long time = stat.getModificationTime();
String path = cur.toUri().getPath();
String name = path.substring(path.lastIndexOf("/") + 1,
path.length());
if (stat.isDirectory()) {
dtmFile.addRow(new String[] { name, "", "Directory", date });
} else {
dtmFile.addRow(new String[] { name, size.toString(), "File",
date });
}
}
return 0;
}


private int HadoopShowFiles(String pathname) throws IOException {
Path path = new Path(pathname + "/");
Configuration conf = new Configuration();
jlable.setText(pathname);
currentPath = pathname;
FileSystem filesys = path.getFileSystem(conf);
FileStatus[] srcs = filesys.globStatus(path);
if ((srcs == null) || (srcs.length == 0)) {
throw new FileNotFoundException("yuht test");
}
for (int i = 0; i < srcs.length; i++) {
lsfile(srcs[i], filesys);
}
return 0;
}


/*
* public boolean listFiles(File path) { String strpath =
* path.getAbsolutePath(); if (path.isDirectory() == false) {
* JOptionPane.showMessageDialog(this, "file is not exit"); return false; }
* currentPath = path.getAbsolutePath(); comboboxPath.removeAllItems();
* File[] roots = File.listRoots(); int index = 0; for (int i = 0; i <
* roots.length; i++) { String rootpath = roots[i].getAbsolutePath();
* comboboxPath.addItem(rootpath); if (currentPath.indexOf(rootpath) != -1)
* { String[] bufpath = currentPath.split("\\\\"); for (int j = 0; j <
* bufpath.length; j++) { String buf = " "; for (int k = 1; k < j; k++) {
* buf += " "; } comboboxPath.addItem(buf + bufpath[j]); index = i + j; } if
* (bufpath.length == 1) { index = i;

* } } } comboboxPath.setSelectedIndex(index); //currentIndex = index;
* dtmFile.setRowCount(0); if (strpath.split("\\\\").length > 1) {
* dtmFile.addRow(new String[] { "up", "", "", "" });

* } File[] files = path.listFiles(); for (int i = 0; i < files.length; i++)
* { String name = files[i].getName(); if (files[i].isDirectory()) {
* dtmFile.addRow(new String[] { name, "", "D", "" }); } else {

* dtmFile.addRow(new String[] { name, "1000", "F", " " }); } } return true;
* }
*/
public void actionPerformed(ActionEvent e) {
int subindex = currentPath.lastIndexOf("/");
if ((currentPath.equals("hdfs://ubuntu:9000")) || (subindex == -1)) {
return;
}
String uppath = currentPath.substring(0, subindex);
try {
HadoopShowFiles(uppath);
} catch (IOException ex) {


}
}


public void mouseClicked(MouseEvent event) {
int clickedtimes = event.getClickCount();
if (clickedtimes == 2) {
int row = table.rowAtPoint(event.getPoint());
String name = (String) table.getValueAt(row, 0);
String path = currentPath + "/" + name;
Path dstPath = new Path(path);
Configuration conf = new Configuration();
try {
FileSystem filesys = dstPath.getFileSystem(conf);
if (filesys.isDirectory(dstPath)) {
HadoopShowFiles(path);
} else {
/*
InputStream in = filesys.open(dstPath);
text.setText("");
byte[] by = new byte[1024];
in.read(by);
String reads = new String(by);
text.setText(reads);
*/
JFrame jf = new JFrame("filecontent");
InputStream in = filesys.open(dstPath);
byte[] by = new byte[1024];
in.read(by);
String reads = new String(by);
jf.setSize(400, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension di = Toolkit.getDefaultToolkit().getScreenSize();
jf.setLocation((int) (di.getWidth() - jf.getWidth()) / 2,
(int) (di.getHeight() - jf.getHeight()) / 2);
JTextArea jt = new JTextArea();
jt.setSize(400, 200);
jt.setText(reads);
jf.add(new JScrollPane(jt));
jf.setVisible(true);
}
} catch (IOException ex) {
}
}
}


public void mouseEntered(MouseEvent event) {
}


public void hadoopDelectFile(String pathfile) throws IOException {
Path path = new Path(pathfile);
Configuration conf = new Configuration();
FileSystem fs = path.getFileSystem(conf);
// FileStatus filestatus = fs.getFileStatus(path);
if (fs.delete(path, true)) {
} else {
throw new IOException("Delect file failed" + path);
}
}


public void hadoopmkdir(String pathfile) throws IOException {
Path path = new Path(pathfile);
Configuration conf = new Configuration();
FileSystem fs = path.getFileSystem(conf);
FileStatus filestatus = null;// fs.getFileStatus(path);
try {
filestatus = fs.getFileStatus(path);
if (filestatus.isDirectory()) {
throw new IOException(pathfile + ":File exists!");
} else {
throw new IOException(pathfile + " exists but "
+ "is not a directory");
}
} catch (FileNotFoundException e) {
if (!fs.mkdirs(path)) {
throw new IOException(path + "Failed to create!" + pathfile);
}
}
}


public void mousePressed(MouseEvent event) {
if (SwingUtilities.isRightMouseButton(event)) {
int row = table.rowAtPoint(event.getPoint());
if (row != -1) {
JPopupMenu popup = new JPopupMenu();
JMenuItem delect = new JMenuItem("delect");
String name = (String) table.getValueAt(row, 0);
file = currentPath + "/" + name;
delect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ac) {
// Path dstPath = new Path(file);
// Configuration conf = new Configuration();
try {
// FileSystem filesys = dstPath.getFileSystem(conf);
hadoopDelectFile(file);
HadoopShowFiles(currentPath);
/*
* if (filesys.isDirectory(dstPath)) {
* HadoopShowFiles(file); }
*/


} catch (IOException ex) {
}
}
});
JMenuItem newdirectory = new JMenuItem("new directory");
newdirectory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ac) {
try {
hadoopmkdir("hdfs://ubuntu:9000/input");
HadoopShowFiles(currentPath);
/*
* if (filesys.isDirectory(dstPath)) {
* HadoopShowFiles(file); }
*/


} catch (IOException ex) {
}
}
});
JMenuItem newfile = new JMenuItem("new file");
popup.add(newfile);
popup.add(newdirectory);
popup.add(delect);
popup.show(event.getComponent(), event.getX(), event.getY());
}
}
}


public void mouseReleased(MouseEvent e) {
}


public void mouseExited(MouseEvent e) {
}


public static void main(String[] args) throws IOException {
JFrame jf = new JFrame("hadoop");
jf.setSize(800, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension di = Toolkit.getDefaultToolkit().getScreenSize();
jf.setLocation((int) (di.getWidth() - jf.getWidth()) / 2,
(int) (di.getHeight() - jf.getHeight()) / 2);
jf.add(new HadoopFileView());
jf.setVisible(true);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值