Provides funcionality to load DEX node or edge types
from relational databases or CSV files as data sources as well as
utilities to dump data.
Specifically, this package contains the following classes:
- {@link edu.upc.dama.dex.io.Loader}
- {@link edu.upc.dama.dex.io.NodeTypeLoader}, to load
node types.
- {@link edu.upc.dama.dex.io.EdgeTypeLoader}, to load
edge types.
- {@link edu.upc.dama.dex.io.RowReader}
- {@link edu.upc.dama.dex.io.CSVReader}, to read data
from CSV files.
- {@link edu.upc.dama.dex.io.JDBCReader}, to read data
from relational databases files.
- {@link edu.upc.dama.dex.io.RowWriter}
- {@link edu.upc.dama.dex.io.CSVWriter}, to write data
to CSV files.
Example
We can suppose there is a table with the following schema:
TABLE_NAME {
ID (INT),
ATTR1 (Integer),
ATTR2 (String)
}
In this sample, we can create a NodeTypeLoader
using a
JDBCReader
. The reader allows for retrieving all
the data as rows and the loader allows for storing these
rows into a DEX node or edge type. We referee the user
to the documentation of these classes to learn in deep how to use
them.
Following code example shows how to load a DEX node type with data
from the given relational table:
public static void main(String[] args) throws IOException {
/*
* DEX graph
*/
DEX dex = new DEX();
GraphPool gp = dex.create("image.dex");
DbGraph dbgraph = gp.getDbGraph();
/*
* Create node type and attributes
*/
int nodeType = dbgraph.newNodeType("NAME");
long attr1 = dbgraph.newAttribute(nodeType, "ATTR1", Value.INT);
long attr2 = dbgraph.newAttribute(nodeType, "ATTR2", Value.STRING);
/*
* Create the JDBC reader using the MySQL sample database
*/
RowReader rr = new JDBCReader("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/sample",
"SELECT * FROM `TABLE_NAME`");
/*
* Create a new loader listener to receive events
*/
LoaderListener ll = new NodeTypeListener();
/*
* Create a new loader
*/
NodeTypeLoader ntl = new NodeTypeLoader(rr, dbgraph, nodeType);
/*
* Assign the two columns (attributes) to the loader
*/
ntl.setAttribute(attr1, 1);
ntl.setAttribute(attr2, 2);
/*
* Register the loader listener to the loader
*/
ntl.registerLoaderListener(ll);
/*
* Start the loader
*/
ntl.run(NodeTypeLoader.Mode.TWO_PHASES, 1);
/*
* Close DEX
*/
gp.close();
dex.close();
}
NodeTypeListener
Implementation of the listener used in this example. This listener
writes a line to the standard output with the total time of the execution.
public class NodeTypeListener implements LoaderListener {
public NodeTypeListener()
{
}
public void loadProgress(ListenerEvent le) {
System.out.println("Node type listener called! Objects loaded: " + le.objects);
}
}