need
Execute the k-means algorithm on a field of a table in a MySQL database and write the processed data into the new table.
Source code and driver
kmeans_jb51.rar
Source code
import java.sql.*;import java.util.*;/** * @author tianshl * @version 2018/1/13 11:13 am */public class Kmeans { // Source data private List<Integer> origins = new ArrayList<>(); // Grouped data private Map<Double, List<Integer>> grouped; // Initial centroid list private List<Double> cores; // Data source private String tableName; private String colName; /** * Constructor method* * @param tableName Source data table name* @param colName Source data column name* @param cores Center of mass list*/ private Kmeans(String tableName, String colName,List<Double> cores){ this.cores = cores; this.tableName = tableName; this.colName = colName; } /** * Recalculate the center of mass * * @return New list of centroids*/ private List<Double> newCores(){ List<Double> newCores = new ArrayList<>(); for(List<Integer> v: grouped.values()){ newCores.add(v.stream().reduce(0, (sum, num) -> sum + num) / (v.size() + 0.0)); } Collections.sort(newCores); return newCores; } /** * Determine whether it ends* * @return bool */ private Boolean isOver(){ List<Double> _cores = newCores(); for(int i=0, len=cores.size(); i<len; i++){ if(!cores.get(i).toString().equals(_cores.get(i).toString())){ // Use new centroid cores = _cores; return false; } } return true; } /** * Data grouping*/ private void setGrouped(){ grouped = new HashMap<>(); Double core; for (Integer origin: origins) { core = getCore(origin); if (!grouped.containsKey(core)) { grouped.put(core, new ArrayList<>()); } grouped.get(core).add(origin); } } /** * Select the centroid* * @param num Data to be grouped* @return centroid*/ private Double getCore(Integer num){ // Difference List<Double> diffs = new ArrayList<>(); // Calculate the difference for(Double core: cores){ diffs.add(Math.abs(num - core)); } // Minimum difference-> Index-> Corresponding centroid return cores.get(diffs.indexOf(Collections.min(diffs))); } /** * Establish a database connection* @return connection */ private Connection getConn(){ try { // The URL points to the database name to be accessed mydata String url = "jdbc:mysql://localhost:3306/data_analysis_dev"; // Username during MySQL configuration String user = "root"; // Password during MySQL configuration String password = "root"; // Load the driver Class.forName("com.mysql.jdbc.Driver"); // Declare the Connection object Connection conn = DriverManager.getConnection(url, user, password); if(conn.isClosed()){ System.out.println("Connecting to the database failed!"); return null; } System.out.println("Connecting to the database successfully!"); return conn; } catch (Exception e) { System.out.println("Conn connection failed!"); e.printStackTrace(); } return null; } /** * Close the database connection* * @param conn Connection*/ private void close(Connection conn){ try { if(conn != null && !conn.isClosed()) conn.close(); } catch (Exception e){ e.printStackTrace(); } } /** * Get source data*/ private void getOrigins(){ Connection conn = null; try { conn = getConn(); if(conn == null) return; Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery(String.format("select %s from %s", colName, tableName)); while(rs.next()){ origins.add(rs.getInt(1)); } conn.close(); } catch (Exception e){ e.printStackTrace(); } finally { close(conn); } } /** * Write data to the new table*/ private void write(){ Connection conn = null; try { conn = getConn(); if(conn == null) return; // Create table Statement statement = conn.createStatement(); // Delete the old data table statement.execute("DROP TABLE IF EXISTS k_means; "); // Create new table statement.execute("CREATE TABLE IF NOT EXISTS k_means(`core` DECIMAL(11, 7), `col` INTEGER(11));"); // Automatic submission of conn.setAutoCommit(false); PreparedStatement ps = conn.prepareStatement("INSERT INTO k_means VALUES (?, ?)"); for(Map.Entry<Double, List<Integer>> entry: grouped.entrySet()){ Double core = entry.getKey(); for(Integer value: entry.getValue()){ ps.setDouble(1, core); ps.setInt(2, value); ps.addBatch(); } } // Batch execution of ps.executeBatch(); // commit transaction conn.commit(); // Close connection conn.close(); } catch (Exception e){ e.printStackTrace(); } finally { close(conn); } } /** * Process data*/ private void run(){ System.out.println("Get source data"); // Get source data getOrigins(); // Stop grouping Boolean isOver = false; System.out.println("Data grouping processing"); while(!isOver) { // Data grouping setGrouped(); // Determine whether to stop grouping isOver = isOver(); } System.out.println("Write processed data to the database"); // Write grouped data to a new table write(); System.out.println("Write data to complete"); } public static void main(String[] args){ List<Double> cores = new ArrayList<>(); cores.add(260.0); cores.add(600.0); // Table name, column name, centroid list new Kmeans("attributes", "attr_length", cores).run(); }}Source File
Kmeans.java
Compilation
javac Kmeans.java
run
# Specify dependency library java -Djava.ext.dirs=./lib Kmeans
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.