Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package hadoopdemo; /** * * @author ado */ //import java.io.IOException; //import java.util.logging.Level; //import java.util.logging.Logger; //import org.apache.hadoop.conf.Configuration; //import org.apache.hadoop.conf.Configured; //import org.apache.hadoop.fs.FSDataOutputStream; //import org.apache.hadoop.fs.FileSystem; //import org.apache.hadoop.fs.Path; //import org.apache.hadoop.util.Tool; //import org.apache.hadoop.util.ToolRunner; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class Hadoop extends javax.swing.JFrame { /** * Creates new form Hadoop */ public Hadoop() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { newFolderTextField = new javax.swing.JTextField(); addFolderButton = new javax.swing.JButton(); deleteFolderTextField = new javax.swing.JTextField(); deleteFolderButton = new javax.swing.JButton(); addFileTextField = new javax.swing.JTextField(); addFileButton = new javax.swing.JButton(); oldNameTextField = new javax.swing.JTextField(); renameButton = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); hadoopPathTextField = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); newNameTextFIeld = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Hadoop Demo"); setName(""); // NOI18N addFolderButton.setText("Add Folder"); addFolderButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addFolderButtonActionPerformed(evt); } }); deleteFolderButton.setText("Delete Folder"); deleteFolderButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deleteFolderButtonActionPerformed(evt); } }); addFileButton.setText("Add File"); addFileButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addFileButtonActionPerformed(evt); } }); renameButton.setText("Rename"); renameButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { renameButtonActionPerformed(evt); } }); jLabel1.setText("Add File:"); jLabel2.setText("To:"); jLabel3.setText("File:"); jLabel4.setText("Rename to:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3).addComponent(jLabel1).addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(addFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(hadoopPathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18).addComponent(addFileButton)) .addGroup(layout.createSequentialGroup().addGap(32, 32, 32).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(newNameTextFIeld, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(oldNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(renameButton)))) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(newFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(70, 70, 70).addComponent(addFolderButton)) .addGroup(layout.createSequentialGroup() .addComponent(deleteFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(70, 70, 70).addComponent(deleteFolderButton)))) .addComponent(jLabel4)).addContainerGap(34, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(newFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(addFolderButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(deleteFolderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(deleteFolderButton)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(60, 60, 60).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(addFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(hadoopPathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(addFileButton).addGap(15, 15, 15))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent( oldNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(newNameTextFIeld, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(20, 20, 20)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(renameButton).addGap(35, 35, 35))))); pack(); }// </editor-fold>//GEN-END:initComponents private void addFolderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFolderButtonActionPerformed try { String newFolder = newFolderTextField.getText(); System.out.println(newFolder); Configuration conf = new Configuration(); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/core-site.xml")); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/hdfs-site.xml")); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/mapred-site.xml")); FileSystem fileSystem = FileSystem.get(conf); fileSystem.mkdirs(new Path(newFolder)); } catch (IOException ex) { Logger.getLogger(Hadoop.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_addFolderButtonActionPerformed private void deleteFolderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteFolderButtonActionPerformed try { String deleteFolder = deleteFolderTextField.getText(); System.out.println(deleteFolder); Configuration conf = new Configuration(); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/core-site.xml")); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/hdfs-site.xml")); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/mapred-site.xml")); FileSystem fileSystem = FileSystem.get(conf); fileSystem.delete(new Path(deleteFolder)); } catch (IOException ex) { Logger.getLogger(Hadoop.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_deleteFolderButtonActionPerformed private void addFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFileButtonActionPerformed try { String addFile = addFileTextField.getText(); String hadoopPath = hadoopPathTextField.getText(); System.out.println(addFile); Configuration conf = new Configuration(); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/core-site.xml")); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/hdfs-site.xml")); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/mapred-site.xml")); FileSystem fileSystem = FileSystem.get(conf); fileSystem.copyFromLocalFile(new Path(addFile), new Path(hadoopPath)); } catch (IOException ex) { Logger.getLogger(Hadoop.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_addFileButtonActionPerformed private void renameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameButtonActionPerformed try { String oldName = oldNameTextField.getText(); String newName = newNameTextFIeld.getText(); Configuration conf = new Configuration(); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/core-site.xml")); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/hdfs-site.xml")); conf.addResource(new Path("/home/ado/hadoop-2.7.3/etc/hadoop/mapred-site.xml")); FileSystem fileSystem = FileSystem.get(conf); fileSystem.rename(new Path(oldName), new Path(newName)); } catch (IOException ex) { Logger.getLogger(Hadoop.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_renameButtonActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Hadoop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Hadoop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Hadoop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Hadoop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(() -> { new Hadoop().setVisible(true); }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addFileButton; private javax.swing.JTextField addFileTextField; private javax.swing.JButton addFolderButton; private javax.swing.JButton deleteFolderButton; private javax.swing.JTextField deleteFolderTextField; private javax.swing.JTextField hadoopPathTextField; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JTextField newFolderTextField; private javax.swing.JTextField newNameTextFIeld; private javax.swing.JTextField oldNameTextField; private javax.swing.JButton renameButton; // End of variables declaration//GEN-END:variables }