Example usage for org.apache.mahout.common IOUtils quietClose

List of usage examples for org.apache.mahout.common IOUtils quietClose

Introduction

In this page you can find the example usage for org.apache.mahout.common IOUtils quietClose.

Prototype

public static void quietClose(Connection closeable) 

Source Link

Usage

From source file:de.tuberlin.dima.aim.exercises.HadoopAndPactTestcase.java

License:Open Source License

public List<String> readLines(String path) throws IOException {
    List<String> lines = new ArrayList<String>();
    BufferedReader reader = null;
    try {/*from w  w w  . j  a  v  a2 s .  c  o  m*/
        reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(path)));
        String line;
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
    } finally {
        IOUtils.quietClose(reader);
    }
    return lines;
}

From source file:org.plista.kornakapi.core.storage.MySqlMaxPersistentStorage.java

License:Apache License

@Override
public void setPreference(long userID, long itemID, float value) throws IOException {
    Connection conn = null;/*from   w  w  w.j  a  va 2 s .  c  o  m*/
    PreparedStatement stmt = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement(IMPORT_QUERY_MAX);
        stmt.setLong(1, userID);
        stmt.setLong(2, itemID);
        stmt.setFloat(3, value);
        stmt.execute();
    } catch (SQLException e) {
        throw new IOException(e);
    } finally {
        IOUtils.quietClose(stmt);
        IOUtils.quietClose(conn);
    }
}

From source file:org.plista.kornakapi.core.storage.MySqlMaxPersistentStorage.java

License:Apache License

@Override
public void batchSetPreferences(Iterator<Preference> preferences, int batchSize) throws IOException {
    Connection conn = null;//from w ww . ja v  a  2  s  .c o m
    PreparedStatement stmt = null;

    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement(IMPORT_QUERY_MAX);

        int recordsQueued = 0;

        while (preferences.hasNext()) {
            Preference preference = preferences.next();
            stmt.setLong(1, preference.getUserID());
            stmt.setLong(2, preference.getItemID());
            stmt.setFloat(3, preference.getValue());
            stmt.addBatch();

            if (++recordsQueued % batchSize == 0) {
                stmt.executeBatch();
                log.info("imported {} records in batch", recordsQueued);
            }
        }

        if (recordsQueued % batchSize != 0) {
            stmt.executeBatch();
            log.info("imported {} records in batch. done.", recordsQueued);
        }

    } catch (SQLException e) {
        throw new IOException(e);
    } finally {
        IOUtils.quietClose(stmt);
        IOUtils.quietClose(conn);
    }
}

From source file:org.plista.kornakapi.core.storage.MySqlStorage.java

License:Apache License

@Override
public void batchSetPreferences(Iterator<Preference> preferences, int batchSize) throws IOException {
    Connection conn = null;/* ww  w .  j av a  2  s .c o m*/
    PreparedStatement stmt = null;

    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement(IMPORT_QUERY);

        int recordsQueued = 0;

        while (preferences.hasNext()) {
            Preference preference = preferences.next();
            stmt.setLong(1, preference.getUserID());
            stmt.setLong(2, preference.getItemID());
            stmt.setFloat(3, preference.getValue());
            stmt.addBatch();

            if (++recordsQueued % batchSize == 0) {
                stmt.executeBatch();
                log.info("imported {} records in batch", recordsQueued);
            }
        }

        if (recordsQueued % batchSize != 0) {
            stmt.executeBatch();
            log.info("imported {} records in batch. done.", recordsQueued);
        }

    } catch (SQLException e) {
        throw new IOException(e);
    } finally {
        IOUtils.quietClose(stmt);
        IOUtils.quietClose(conn);
    }
}

From source file:org.plista.kornakapi.core.storage.MySqlStorage.java

License:Apache License

/**
 port from PHP code:/*from w  w w  .j a  v  a2s  . c o m*/
        
 $hours=  (int)(time()/3600)  ;
 $selDay=  $hours%  72;
 $selHalfDay=  (int)($selDay/  12);
 $sql=  'alter table taste_preferences modify timeframe int default'. $selHalfDay;
 $stmt=  new  PDO('mysql:host=localhost;dbname=kornakapi;charset=utf8','dbname','dbpwd');
 $stmt->exec($sql);
 $olttable=  ($selHalfDay+1)  %  6;
 $sql=  'ALTER TABLE taste_preferences TRUNCATE PARTITION p'.$olttable;
 $stmt=  new  PDO('mysql:host=localhost;dbname=kornakapi;charset=utf8','dbname','dbpwd');
 $stmt->exec($sql);
        
 * @throws IOException
 */
@Override
public void purgeOldPreferences() throws IOException {
    Connection conn = null;
    PreparedStatement switchStmt = null;
    Statement truncateStmt = null;

    try {
        conn = dataSource.getConnection();

        long hours = System.currentTimeMillis() / (3600 * 1000);
        int selDay = (int) (hours % timeWindow);
        int selfHalfDay = selDay / (timeWindow / 6);

        switchStmt = conn.prepareStatement(SWITCH_CURRENT_PARTITION_QUERY);
        switchStmt.setInt(1, selfHalfDay);
        switchStmt.execute();

        int indexOfPartitionToPurge = (selfHalfDay + 1) % 6;

        truncateStmt = conn.createStatement();
        truncateStmt.execute(TRUNCATE_PREVIOUS_PARTITION_QUERY_FRAGMENT + "p"
                + String.valueOf(indexOfPartitionToPurge) + ";");

    } catch (SQLException e) {
        throw new IOException(e);
    } finally {
        IOUtils.quietClose(switchStmt);
        IOUtils.quietClose(truncateStmt);
        IOUtils.quietClose(conn);
    }
}

From source file:org.plista.kornakapi.core.storage.MySqlStorage.java

License:Apache License

@Override
public void addCandidate(String label, long itemID) throws IOException {
    Connection conn = null;//from   ww w . j  a v  a  2 s  .c o  m
    PreparedStatement stmt = null;

    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement(INSERT_CANDIDATE_QUERY);

        stmt.setString(1, label);
        stmt.setLong(2, itemID);

        stmt.execute();

    } catch (SQLException e) {
        if (log.isInfoEnabled()) {
            log.info(e.getMessage());
        } else {
            throw new IOException(e);
        }

    } finally {
        IOUtils.quietClose(stmt);
        IOUtils.quietClose(conn);
    }
}

From source file:org.plista.kornakapi.core.storage.MySqlStorage.java

License:Apache License

@Override
public Iterable<String> batchAddCandidates(Iterator<Candidate> candidates, int batchSize) throws IOException {

    Set<String> modifiedLabels = Sets.newHashSet();

    Connection conn = null;//from   w ww. ja v a2 s  . c om
    PreparedStatement stmt = null;

    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement(INSERT_CANDIDATE_QUERY);

        int recordsQueued = 0;

        while (candidates.hasNext()) {

            Candidate candidate = candidates.next();

            modifiedLabels.add(candidate.getLabel());

            stmt.setString(1, candidate.getLabel());
            stmt.setLong(2, candidate.getItemID());
            stmt.addBatch();

            if (++recordsQueued % batchSize == 0) {
                stmt.executeBatch();
                log.info("imported {} candidates in batch", recordsQueued);
            }
        }

        if (recordsQueued % batchSize != 0) {
            stmt.executeBatch();
            log.info("imported {} candidates in batch. done.", recordsQueued);
        }

    } catch (SQLException e) {
        throw new IOException(e);
    } finally {
        IOUtils.quietClose(stmt);
        IOUtils.quietClose(conn);
    }

    return modifiedLabels;
}

From source file:org.plista.kornakapi.core.storage.MySqlStorage.java

License:Apache License

@Override
public void deleteCandidate(String label, long itemID) throws IOException {
    Connection conn = null;/*from  w w  w  .j  av a2s. c  om*/
    PreparedStatement stmt = null;

    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement(REMOVE_CANDIDATE_QUERY);

        stmt.setString(1, label);
        stmt.setLong(2, itemID);

        stmt.execute();

    } catch (SQLException e) {
        throw new IOException(e);
    } finally {
        IOUtils.quietClose(stmt);
        IOUtils.quietClose(conn);
    }
}

From source file:org.plista.kornakapi.core.storage.MySqlStorage.java

License:Apache License

@Override
public void deleteAllCandidates(String label) throws IOException {
    Connection conn = null;/*from   w  ww  .j ava  2s .  c  o  m*/
    PreparedStatement stmt = null;

    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement(REMOVE_ALL_CANDIDATES_QUERY);

        stmt.setString(1, label);

        stmt.execute();

    } catch (SQLException e) {
        throw new IOException(e);
    } finally {
        IOUtils.quietClose(stmt);
        IOUtils.quietClose(conn);
    }
}

From source file:org.plista.kornakapi.core.storage.MySqlStorage.java

License:Apache License

@Override
public Iterable<String> batchDeleteCandidates(Iterator<Candidate> candidates, int batchSize)
        throws IOException {

    Set<String> modifiedLabels = Sets.newHashSet();

    Connection conn = null;/*from www  . j a v a2s  .  c om*/
    PreparedStatement stmt = null;

    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement(REMOVE_CANDIDATE_QUERY);

        int recordsQueued = 0;

        while (candidates.hasNext()) {

            Candidate candidate = candidates.next();

            modifiedLabels.add(candidate.getLabel());

            stmt.setString(1, candidate.getLabel());
            stmt.setLong(2, candidate.getItemID());
            stmt.addBatch();

            if (++recordsQueued % batchSize == 0) {
                stmt.executeBatch();
                log.info("deleted {} candidates in batch", recordsQueued);
            }
        }

        if (recordsQueued % batchSize != 0) {
            stmt.executeBatch();
            log.info("deleted {} candidates in batch. done.", recordsQueued);
        }

    } catch (SQLException e) {
        throw new IOException(e);
    } finally {
        IOUtils.quietClose(stmt);
        IOUtils.quietClose(conn);
    }

    return modifiedLabels;
}