Import.pnl_import_vcf.java Source code

Java tutorial

Introduction

Here is the source code for Import.pnl_import_vcf.java

Source

/*
 * 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 Import;

import Create.pnl_create_hive_link;
import columnFamily.family;
import com.jidesoft.grid.AutoFilterTableHeader;
import com.jidesoft.list.DefaultDualListModel;
import com.jidesoft.swing.CheckBoxList;
import importToNewTable.createDataJob;
import importToTable.importDataJob;
import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import org.apache.commons.io.FilenameUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;

/**
 *
 * @author yujia1986
 */
public class pnl_import_vcf extends javax.swing.JPanel {

    /**
     * Creates new form pnl_import_vcf
     */
    public pnl_import_vcf() {
        initComponents();
        pnl_import_vcf.showHBaseTables runabletask = new pnl_import_vcf.showHBaseTables();
        new Thread(runabletask).start();
    }

    /**
     * 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() {

        btn_group_choose_key = new javax.swing.ButtonGroup();
        btn_group_choose_import_type = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_hbase_tables = new com.jidesoft.grid.SortableTable();
        chooseLable = new javax.swing.JLabel();
        filePathField = new javax.swing.JTextField();
        chooseFile = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        list_dual_hbase_column_family = new com.jidesoft.list.DualList();
        jPanel5 = new javax.swing.JPanel();
        rbt_new_tbl = new javax.swing.JRadioButton();
        rbt_existing_tbl = new javax.swing.JRadioButton();
        btn_import = new javax.swing.JButton();

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("HBase Tables Available"));

        tbl_hbase_tables.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] {

        }));
        jScrollPane1.setViewportView(tbl_hbase_tables);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE));

        chooseLable.setText("Please specify the corresponding VCF file:");

        chooseFile.setText("Choose");
        chooseFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseFileActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(list_dual_hbase_column_family);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout
                        .createSequentialGroup().addContainerGap().addComponent(jScrollPane3).addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Choose Import Type"));

        btn_group_choose_import_type.add(rbt_new_tbl);
        rbt_new_tbl.setSelected(true);
        rbt_new_tbl.setText("Import to a new HBase table");

        btn_group_choose_import_type.add(rbt_existing_tbl);
        rbt_existing_tbl.setText("Import to an existing HBase table");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout
                .setHorizontalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap().addComponent(rbt_new_tbl)
                                .addGap(18, 18, 18).addComponent(rbt_existing_tbl)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(rbt_new_tbl).addComponent(rbt_existing_tbl)));

        btn_import.setText("Import");
        btn_import.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_importActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addComponent(chooseLable)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(filePathField)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(chooseFile))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE).addComponent(btn_import)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel1, 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.BASELINE)
                                .addComponent(chooseLable)
                                .addComponent(filePathField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(chooseFile))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_import)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        layout.linkSize(javax.swing.SwingConstants.VERTICAL,
                new java.awt.Component[] { chooseFile, chooseLable, filePathField });

    }// </editor-fold>//GEN-END:initComponents

    private void chooseFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseFileActionPerformed
        try {
            // TODO add your handling code here:
            filePathField.setText(null);
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            String filePath = chooser.getSelectedFile().getAbsolutePath();
            filePathField.setText(filePath);
            DefaultDualListModel dualModel = new DefaultDualListModel();
            columnFamily.family c = new family();
            String[] columns = c.columnfamily(filePath);
            ArrayList<String> families = new ArrayList<>();
            for (int i = 0; i < columns.length; i++) {
                if (i > 7 && !columns[i].equalsIgnoreCase("FORMAT")) {
                    families.add(columns[i] + ", SampleFamily");
                } else if (columns[i].equalsIgnoreCase("INFO")) {
                    families.add(columns[i] + ", INFOFamily");
                } else if (!columns[i].equalsIgnoreCase("FORMAT")) {
                    families.add(columns[i] + ", SingleColumn");
                }
            }
            for (String b : families) {
                dualModel.addElement(b);
            }
            list_dual_hbase_column_family.setModel(dualModel);
        } catch (HeadlessException | IOException ex) {
            Logger.getLogger(pnl_create_hive_link.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_chooseFileActionPerformed

    private void btn_importActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_importActionPerformed
        // TODO add your handling code here:
        String[] singleColumns = singleColumns();
        String[] selectedFamilies = selectedFamilies();
        CheckBoxList checkList = new CheckBoxList();
        DefaultListModel model = new DefaultListModel();
        for (String a : singleColumns) {
            model.addElement(a);
        }
        checkList.setModel(model);
        String filePath = filePathField.getText();
        if (rbt_existing_tbl.isSelected()) {
            try {
                String[] selectedTableFamilies = tableFamilies();
                JTable tableMap = new JTable();
                DefaultTableModel tableMapModel = (DefaultTableModel) tableMap.getModel();
                setMapTable(tableMap, tableMapModel, selectedFamilies, selectedTableFamilies);
                int map = JOptionPane.showConfirmDialog(null, tableMap, "Please map to column",
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null);
                if (map == 2) {
                    return;
                }
                Object[] o = getMapPair(tableMapModel);
                String selectTableName = tbl_hbase_tables.getValueAt(tbl_hbase_tables.getSelectedRow(), 0)
                        .toString();
                String[] mappedFamilies = (String[]) o[1];
                String[] mappedTableFamilies = (String[]) o[0];
                String[] keys = getKey(selectTableName);
                importToTable.importDataJob importData = new importDataJob();
                importData.importData(filePath, selectTableName, mappedTableFamilies, mappedFamilies, keys);
            } catch (Exception ex) {
                Logger.getLogger(pnl_import_vcf.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                int key = JOptionPane.showConfirmDialog(null, checkList, "Please choose Key",
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null);
                if (key == 2) {
                    return;
                }
                Object[] selectedKey = checkList.getCheckBoxListSelectedValues();
                File f = new File(filePath);
                String fname = f.getName();
                String tableName = FilenameUtils.removeExtension(fname);
                writeKey(selectedKey, tableName);
                writeType(tableName);
                createColumnsTxt(tableName);
                ArrayList<String> list = new ArrayList<>();
                for (Object o : selectedKey) {
                    list.add(o.toString());
                }
                String[] keys = list.toArray(new String[list.size()]);
                importToNewTable.createDataJob importData = new createDataJob();
                importData.importData(filePath, selectedFamilies, keys);
            } catch (Exception ex) {
                Logger.getLogger(pnl_import_vcf.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        filePathField.setText(null);
        DefaultDualListModel dualModel = new DefaultDualListModel();
        list_dual_hbase_column_family.setModel(dualModel);
        pnl_import_vcf.showHBaseTables runabletask = new pnl_import_vcf.showHBaseTables();
        new Thread(runabletask).start();
    }//GEN-LAST:event_btn_importActionPerformed

    public class showHBaseTables implements Runnable {

        public HTableDescriptor[] listHBaseTables() throws IOException {
            Configuration conf = HBaseConfiguration.create();
            HBaseAdmin admin = new HBaseAdmin(conf);
            return admin.listTables();
        }

        private void showHBaseTables() {
            try {
                // TODO add your handling code here:
                tbl_hbase_tables.setModel(null);
                HTableDescriptor[] tablesDescriptor = this.listHBaseTables();
                DefaultTableModel model = new DefaultTableModel();
                model.addColumn("Table Name");
                model.addColumn("Table Columnfamily Names");
                AutoFilterTableHeader _header = new AutoFilterTableHeader(tbl_hbase_tables);
                _header.setAutoFilterEnabled(true);
                _header.setReorderingAllowed(false);
                _header.setUseNativeHeaderRenderer(true);
                tbl_hbase_tables.setTableHeader(_header);
                for (HTableDescriptor descriptor : tablesDescriptor) {
                    ArrayList<String> row = new ArrayList<>();
                    row.add(descriptor.getNameAsString());
                    StringBuilder builder = new StringBuilder();
                    HColumnDescriptor[] cDescriptor = descriptor.getColumnFamilies();
                    for (int i = 0; i < cDescriptor.length; i++) {
                        if (i > 0) {
                            builder.append(" ,");
                        }
                        builder.append(cDescriptor[i].getNameAsString());
                    }
                    row.add(builder.toString());
                    String[] rowArray = row.toArray(new String[row.size()]);
                    model.addRow(rowArray);
                }
                tbl_hbase_tables.setModel(model);
            } catch (IOException ex) {
                Logger.getLogger(pnl_import_vcf.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @Override
        public void run() {
            showHBaseTables();
        }
    }

    public String[] tableFamilies() {
        String families = tbl_hbase_tables.getValueAt(tbl_hbase_tables.getSelectedRow(), 1).toString();
        String[] selectedTableFamilies = families.split(",", -1);
        for (int i = 0; i < selectedTableFamilies.length; i++) {
            selectedTableFamilies[i] = selectedTableFamilies[i].trim();
        }
        return selectedTableFamilies;
    }

    public String[] selectedFamilies() {
        Object[] selectedObject = list_dual_hbase_column_family.getSelectedValues();
        String[] selectedFamilies = null;
        if (selectedObject.length != 0) {
            ArrayList<String> selected = new ArrayList<>();
            for (Object c : selectedObject) {
                selected.add(c.toString());
            }
            selectedFamilies = selected.toArray(new String[selected.size()]);
            if (selectedFamilies != null) {
                for (int i = 0; i < selectedFamilies.length; i++) {
                    String[] tmp = selectedFamilies[i].split(",", -1);
                    selectedFamilies[i] = tmp[0];
                }
            }
        }
        return selectedFamilies;
    }

    public String[] singleColumns() {
        Object[] selectedObject = list_dual_hbase_column_family.getSelectedValues();
        String[] selectedFamilies = null;
        if (selectedObject.length != 0) {
            ArrayList<String> selected = new ArrayList<>();
            for (Object c : selectedObject) {
                if (!c.toString().contains("Family Column")) {
                    selected.add(c.toString());
                }
            }
            selectedFamilies = selected.toArray(new String[selected.size()]);
            if (selectedFamilies != null) {
                for (int i = 0; i < selectedFamilies.length; i++) {
                    String[] tmp = selectedFamilies[i].split(",", -1);
                    selectedFamilies[i] = tmp[0];
                }
            }
        }
        return selectedFamilies;
    }

    public void setMapTable(JTable table, DefaultTableModel model, String[] selectedFamilies,
            String[] selectedTableFamilies) {
        model.setRowCount(0);
        model.addColumn("Import Column");
        model.addColumn("Map to");
        TableColumn sportColumn = table.getColumnModel().getColumn(1);
        JComboBox comboBox = new JComboBox();
        for (String a : selectedFamilies) {
            comboBox.addItem(a);
        }
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
        for (String b : selectedTableFamilies) {
            String[] tmp = new String[1];
            tmp[0] = b;
            model.addRow(tmp);
        }
    }

    public String[] getKey(String tableName) throws FileNotFoundException, IOException {
        String path = System.getProperty("user.dir");
        path = path + "/HVCF/HBase/" + tableName + "/key.txt";
        FileInputStream input = new FileInputStream(path);
        DataInputStream data = new DataInputStream(input);
        InputStreamReader line = new InputStreamReader(data);
        BufferedReader br = new BufferedReader(line);
        String fLine;
        String[] key = null;
        while ((fLine = br.readLine()) != null) {
            key = fLine.split(",", -1);
        }
        return key;
    }

    public Object[] getMapPair(DefaultTableModel model) {
        ArrayList<String> list1 = new ArrayList<>();
        ArrayList<String> list2 = new ArrayList<>();
        for (int i = 0; i < model.getRowCount(); i++) {
            list1.add(model.getValueAt(i, 0).toString());
            list2.add(model.getValueAt(i, 1).toString());
        }
        String[] array1 = list1.toArray(new String[list1.size()]);
        String[] array2 = list2.toArray(new String[list2.size()]);
        Object[] o = { array1, array2 };
        return o;
    }

    public void writeKey(Object[] key, String tableName) throws IOException {
        String path = System.getProperty("user.dir");
        path = path + "/HVCF/HBase/" + tableName;
        File keyPath = new File(path);
        if (!keyPath.exists()) {
            keyPath.mkdirs();
        }
        File f = new File(path + "/key.txt");
        try (FileWriter out = new FileWriter(f)) {
            for (int i = 0; i < key.length; i++) {
                if (i > 0) {
                    out.write("," + key[i].toString());
                } else {
                    out.write(key[i].toString());
                }
            }
        }
    }

    public void writeType(String tableName) throws IOException {
        String path = System.getProperty("user.dir");
        path = path + "/HVCF/HBase/" + tableName;
        File keyPath = new File(path);
        if (!keyPath.exists()) {
            keyPath.mkdirs();
        }
        File f = new File(path + "/typeStructure.txt");
        Object[] selectedObject = list_dual_hbase_column_family.getSelectedValues();
        try (FileWriter out = new FileWriter(f)) {
            for (int i = 0; i < selectedObject.length; i++) {
                out.write(selectedObject[i].toString());
                if (i != selectedObject.length - 1)
                    out.write("\n");
            }
            out.close();
        }
    }

    public void createColumnsTxt(String tableName) throws IOException {
        String path = System.getProperty("user.dir");
        path = path + "/HVCF/HBase/" + tableName + "/columns.txt";
        File f = new File(path);
        try (FileWriter out = new FileWriter(f)) {
            out.write(",");
            out.close();
        }
    }

    public void progressBar() {
        JProgressBar progressBar = new JProgressBar();
        TitledBorder border = BorderFactory.createTitledBorder("Importing...");
        progressBar.setBorder(border);
        progressBar.setIndeterminate(true);
        JDialog dialog = new JDialog();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btn_group_choose_import_type;
    private javax.swing.ButtonGroup btn_group_choose_key;
    private javax.swing.JButton btn_import;
    private javax.swing.JButton chooseFile;
    private javax.swing.JLabel chooseLable;
    private javax.swing.JTextField filePathField;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private com.jidesoft.list.DualList list_dual_hbase_column_family;
    private javax.swing.JRadioButton rbt_existing_tbl;
    private javax.swing.JRadioButton rbt_new_tbl;
    private com.jidesoft.grid.SortableTable tbl_hbase_tables;
    // End of variables declaration//GEN-END:variables
}