Package com.btaz.datautil

Examples of com.btaz.datautil.DataUtilException


            if(! ignoreSet.contains(field)) {
                headerFields.add(field);
            }
        }
        if(idCounter != 2) {
            throw new DataUtilException("Invalid header format. The \"id\" field must be present in both files");
        }
        if(headerFields.size() < h1.length || headerFields.size() < h1.length) {
            throw new DataUtilException("Invalid header format. Repeated header fields with the same name");
        }
        return headerFields;
    }
View Full Code Here


     */
    public static void reduce(File inputFile, File outputFile, Reducer reducer, KeyComparator comparator)
            throws DataUtilException {
        // validations
        if(inputFile == null) {
            throw new DataUtilException("The inputFile parameter can not be a null value");
        }
        if(outputFile == null) {
            throw new DataUtilException("The outputFile parameter can not be a null value");
        }
        if(reducer == null) {
            throw new DataUtilException("The reducable parameter can not be a null value");
        }
        if(comparator == null) {
            throw new DataUtilException("The comparator parameter can not be a null value");
        }

        // reduce all data
        FileInputStream inputStream;
        OutputCollector collector = new OutputCollector(outputFile);
        QueueReader queueReader;

        // Aggregate all items matching the comparator and call the Reducable callback
        try {
            inputStream = new FileInputStream(inputFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,
                    DataUtilDefaults.charSet));
            queueReader = new QueueReader(br);

            // reduce data
            String prev = null;
            String curr;
            ArrayList<String> items = new ArrayList<String>();
            while(true) {
                curr = queueReader.readLine();
                if(curr == null) {
                    // no more data
                    reduceItems(collector, reducer, items);
                    break;
                } else if(prev == null) {
                    // first row in a new batch
                    items.add(curr);
                    prev = curr;
                } else if(comparator.compare(prev, curr) == 0) {
                    // same keys
                    items.add(curr);
                    prev = curr;
                } else {
                    // different keys
                    queueReader.push(curr);
                    reduceItems(collector, reducer, items);
                    items.clear();
                    prev = null;
                }
            }
            collector.close();
            inputStream.close();
        } catch (IOException e) {
            throw new DataUtilException(e);
        } catch (MapReduceException e) {
            throw new DataUtilException("Irrecoverable reduce operation", e);
        }
    }
View Full Code Here

     * @param comparator comparator to use for sorting
     */
    public static List<File> sort(File sortDir, List<File> files, Comparator<String> comparator) {
        // validations
        if(sortDir == null) {
            throw new DataUtilException("The sort directory parameter can't be a null value");
        } else if(!sortDir.exists()) {
            throw new DataUtilException("The sort directory doesn't exist");
        }
        if(files == null) {
            throw new DataUtilException("The files parameter can't be a null value");
        }

        // sort files
        List<File> sortedFiles = new ArrayList<File>();
        for(File file : files) {
            FileInputStream inputStream = null;
            BufferedWriter writer = null;

            try {
                // readLine part into memory
                ArrayList<String> list = new ArrayList<String>();
                inputStream = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, DataUtilDefaults.charSet));
                String line;
                while((line = br.readLine()) != null) {
                    list.add(line);
                }
                inputStream.close();
                inputStream = null;

                // sort
                Collections.sort(list, comparator);

                // write sorted partial
                File sortedFile = File.createTempFile("sorted-", ".part", sortDir);
                sortedFiles.add(sortedFile);

                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                        sortedFile.getAbsoluteFile(), false),DataUtilDefaults.charSet));

                for(String item : list) {
                    writer.write(item + DataUtilDefaults.lineTerminator);
                }
                writer.flush();
                writer.close();
                writer = null;
            } catch (FileNotFoundException e) {
                throw new DataUtilException(e);
            } catch (UnsupportedEncodingException e) {
                throw new DataUtilException(e);
            } catch (IOException e) {
                throw new DataUtilException(e);
            } finally {
                if(inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
View Full Code Here

     * @param dir directory
     * @param extension filename extension
     */
    public static void deleteFilesByExtension(File dir, String extension) {
        if(extension == null) {
            throw new DataUtilException("Filename extension can not be a null value");
        }
        FilenameFilter filter = new FileExtensionFilenameFilter(extension);
        FileDeleter.deleteFiles(dir, filter);
    }
View Full Code Here

     * @param dir directory
     * @param regex regular expression
     */
    public static void deleteFilesByRegex(File dir, String regex) {
        if(regex == null) {
            throw new DataUtilException("Filename regex can not be null");
        }
        FilenameFilter filter = new RegexFilenameFilter(regex);
        FileDeleter.deleteFiles(dir, filter);
    }
View Full Code Here

     * @param filter filename filter, if null then delete all files
     */
    public static void deleteFiles(File dir, FilenameFilter filter) {
        // validations
        if(dir == null) {
            throw new DataUtilException("The delete directory parameter can not be a null value");
        } else if(!dir.exists() || !dir.isDirectory()) {
            throw new DataUtilException("The delete directory does not exist: " + dir.getAbsolutePath());
        }

        // delete files
        File [] files;
        if(filter == null) {
            files = dir.listFiles();
        } else {
            files = dir.listFiles(filter);
        }
        if (files != null) {
            for(File file : files) {
                if(file.isFile()) {
                    if(!file.delete()) {
                        throw new DataUtilException("Failed to delete file: " + file.getAbsolutePath());
                    }
                }
            }
        }
    }
View Full Code Here

     * @return <code>List</code> of <code>FilePart</code> items
     */
    public static List<File> split(File splitDir, File inputFile, long maxBytes, boolean skipHeader) {
        // validations
        if(splitDir == null) {
            throw new DataUtilException("The split directory parameter can not be a null value");
        } else if(!splitDir.exists()) {
            throw new DataUtilException("The split directory is not a valid path");
        }
        if(inputFile == null) {
            throw new DataUtilException("The input file parameter can not be a null value");
        } else if(!inputFile.exists()) {
            throw new DataUtilException("The input file doesn't exist");
        }
        if(maxBytes < DEFAULT_MIN_BYTES) {
            throw new DataUtilException("Invalid max bytes specification: " + maxBytes + ", minimum is: "
                    + DEFAULT_MIN_BYTES);
        }

        ArrayList<File> splitFiles = new ArrayList<File>();

        // open file
        FileInputStream inputStream = null;
        LinkedList<String> rows = new LinkedList<String>();
        try {
            // create FilePart
            inputStream = new FileInputStream(inputFile);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            long charsTotal = 0;
            String line;

            int maxChunk = 60;
            int rowNumber = 0;
            while((line=br.readLine()) != null) {
                rowNumber += 1;
                if(skipHeader && rowNumber == 1) {
                    continue;
                }
                if(storageCalculation(rows.size(), line.length(), charsTotal) > maxBytes) {
                    if(maxChunk-- <= 0) {
                        break;
                    }
                    // we have to stop or we may exceed max part size
                    File splitFile = writePartToFile(splitDir, rows);
                    splitFiles.add(splitFile);
                    rows.clear();
                    charsTotal = 0;
                }
                rows.add(line);
                charsTotal += line.length() + DataUtilDefaults.lineTerminator.length();
            }
            // no more data, finish up the final part
            if(charsTotal > 0) {
                File splitFile = writePartToFile(splitDir, rows);
                splitFiles.add(splitFile);
            }

            inputStream.close();
            inputStream = null;

        } catch (FileNotFoundException e) {
            throw new DataUtilException(e);
        } catch (UnsupportedEncodingException e) {
            throw new DataUtilException(e);
        } catch (IOException e) {
            throw new DataUtilException(e);
        } finally {
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
View Full Code Here

            }
            writer.flush();
            writer.close();
            writer = null;
        } catch (UnsupportedEncodingException e) {
            throw new DataUtilException(e);
        } catch (FileNotFoundException e) {
            throw new DataUtilException(e);
        } catch (IOException e) {
            throw new DataUtilException(e);
        } finally {
            if(writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
View Full Code Here

     */
    public static List<File> map(File workDir, List<File> inputFiles, Mapper mapper)
            throws DataUtilException {
        // validations
        if(workDir == null) {
            throw new DataUtilException("The workDir parameter can not be a null value");
        }
        if(inputFiles == null) {
            throw new DataUtilException("The inputFiles parameter can not be a null value");
        }
        if(mapper == null) {
            throw new DataUtilException("The mappable parameter can not be a null value");
        }

        // setup output collector
        OutputCollector collector = new OutputCollector(workDir, "map");

        // map all data
        try {
            for(File file : inputFiles) {
                FileInputStream inputStream;

                try {
                    inputStream = new FileInputStream(file);
                    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,
                            DataUtilDefaults.charSet));

                    // map data
                    String row;
                    while((row = br.readLine()) != null) {
                        mapper.map(row, collector);
                    }

                    inputStream.close();
                } catch (IOException e) {
                    throw new DataUtilException(e);
                } catch (MapReduceException e) {
                    throw new DataUtilException("Irrecoverable map operation", e);
                }
            }
        } finally {
            collector.close();
        }
View Full Code Here

     * @return file new file
     * @throws DataUtilException data util exception
     */
    public File createFile(File dir, String filename) throws DataUtilException {
        if(dir == null) {
            throw new DataUtilException("The directory parameter can't be a null value");
        }
        try {
            File file = new File(dir, filename);
            return createFile(file);
        } catch (Exception e) {
            throw new DataUtilException("Invalid file: " + filename);
        }
    }
View Full Code Here

TOP

Related Classes of com.btaz.datautil.DataUtilException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.