Transferring an arff file into a database
This example transfers a dataset stored in an ARFF file into the MySQL database weka_test on the MySQL server running on the same machine. In order to make this work, the MySQL JDBC driver must be in the CLASSPATH and the DatabaseUtils.props file must be configured accordingly.
Usage:
Source code:import weka.core.*;
import weka.core.converters.*;
import java.io.*;
/**
* A simple API example of transferring an ARFF file into a MySQL table.
* It loads the data into the database "weka_test" on the MySQL server
* running on the same machine. Instead of using the relation name of the
* database as the table name, "mytable" is used instead. The
* DatabaseUtils.props file must be configured accordingly.
*
* Usage: Arff2Database input.arff
*
* @author FracPete (fracpete at waikato dot ac dot nz)
*/
public class Arff2Database {
/**
* loads a dataset into a MySQL database
*
* @param args the commandline arguments
*/
public static void main(String[] args) throws Exception {
Instances data = new Instances(new BufferedReader(new FileReader(args[0])));
data.setClassIndex(data.numAttributes() - 1);
DatabaseSaver save = new DatabaseSaver();
save.setUrl("jdbc:mysql://localhost:3306/weka_test");
save.setUser("fracpete");
save.setPassword("fracpete");
save.setInstances(data);
save.setRelationForTableName(false);
save.setTableName("mytable");
save.connectToDatabase();
save.writeBatch();
}
}
See also#
- Databases - explains how to use databases within Weka
- weka/experiment/DatabaseUtils.props - the properties file explained in detail
Downloads#
- Arff2Database.java
-
The Weka Examples collection contains several example classes:
SaveDataToDbBatch.java
(stable-3.8, developer)SaveDataToDbIncremental.java
(stable-3.8, developer)