Skip to content

Instantly share code, notes, and snippets.

@AlexanderThiele
Last active August 29, 2015 14:04
Show Gist options
  • Save AlexanderThiele/e5343a70712afb06bb2f to your computer and use it in GitHub Desktop.
Save AlexanderThiele/e5343a70712afb06bb2f to your computer and use it in GitHub Desktop.
Upload your CSV-data to Structr using Schematypes

#CSV File format

To import a Node:

  • SchemaTypeName;id;Value:AttributeName:Type;value:AttributeName:type;...

To import a Relationship:

  • Relationship;id1;id2;RelationshipType

##Example CSV-file

Beer;0;Krombacher Pils:name:String
Beer;1;Jever Pilsener:name:String
City;2;Jever:name:String
City;3;Kreuztal Krombach:name:String
Country;4;Germany:name:String;DE:isoCode:String
Relationship;0;3;IN
Relationship;1;2;IN
Relationship;2;4;LOCATED_IN
Relationship;3;4;LOCATED_IN

#Request to start the import curl the maintenance command (schemaDataImport):

  • send the csv-file-path ** f.e.: {file:"/file/path/beer.csv"}

  • optional: the csv seperator (default is ',') ** f.e.: {file:"/file/path/beer.csv",seperator:";"}

    curl -HX-user:admin -HX-password:admin -i localhost:8082/structr/rest/maintenance/schemaDataImport -XPOST -d '{file:"/file/path/beer.csv",seperator:";"}'

#My first import My first import contained about 16.000 new nodes and 12.000 new Relationships. The request took 1 hour and 40 minutes (6060 seconds)

#Usage copy this file into the package org.structr.importer or a different one (but change the package inside the file)

package org.structr.importer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.structr.common.error.FrameworkException;
import org.structr.core.app.App;
import org.structr.core.app.StructrApp;
import org.structr.core.graph.MaintenanceCommand;
import org.structr.core.graph.NodeInterface;
import org.structr.core.graph.NodeServiceCommand;
import org.structr.core.graph.Tx;
import org.structr.core.property.DoubleProperty;
import org.structr.core.property.IntProperty;
import org.structr.core.property.PropertyMap;
import org.structr.core.property.StringProperty;
import org.structr.rest.resource.MaintenanceParameterResource;
import org.structr.schema.SchemaHelper;
/**
*
* @author Alexander Thiele
*/
public class SchemaDataImport extends NodeServiceCommand implements MaintenanceCommand {
private static final Logger logger = Logger.getLogger(SchemaDataImport.class.getName());
static {
MaintenanceParameterResource.registerMaintenanceCommand("schemaDataImport", SchemaDataImport.class);
}
@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
//CSVformat className,id,val:name:type,...,...
//CSVformat RELATIONSHIP,idFrom,idTo,relName
final int logAtLine = 1000;
Map<String, NodeInterface> relMapping = new HashMap<>();
final App app = StructrApp.getInstance(securityContext);
String filePath = (String)attributes.get("file");
String mainSeperator = (String)attributes.get("seperator");
if(mainSeperator == null)
mainSeperator = ",";
if(filePath != null && filePath.length() > 0){
File f = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
try (final Tx tx = app.tx()) {
int counter = 0;
while ((line = br.readLine()) != null) {
counter++;
if(counter%logAtLine == 0){
logger.log(Level.INFO, "Current import line: " + counter);
}
String[] splitted = line.split(mainSeperator);
if(splitted != null && splitted.length > 2){
String classType = splitted[0];
if(classType.equals("Relationship")){
NodeInterface from = relMapping.get(splitted[1]);
NodeInterface to = relMapping.get(splitted[2]);
if(from != null && to != null){
String relName = from.getType() + splitted[3] + to.getType();
Class relType = SchemaHelper.getEntityClassForRawType(relName);
if(relType != null){
app.create(from, to, relType);
}else{
logger.log(Level.INFO, "No relationship class found: " + relName);
}
}else{
logger.log(Level.INFO, "From (" + splitted[1] + ") or To (" + splitted[2] + ") not found");
}
}else{
Class entityClass = SchemaHelper.getEntityClassForRawType(classType);
if(entityClass != null){
PropertyMap propertyMap = new PropertyMap();
String id = splitted[1];
for(int i=2; i<splitted.length;i++){
String[] valSplit = splitted[i].split(":");
if(valSplit != null && valSplit.length > 2){
String val = valSplit[0];
String name = valSplit[1];
String type = valSplit[2];
if(type.equals("String")){
StringProperty sProperty = new StringProperty(name);
propertyMap.put(sProperty, val);
}else if(type.equals("Integer")){
IntProperty iProperty = new IntProperty(name);
propertyMap.put(iProperty, Integer.parseInt(val));
}else if(type.equals("Double")){
DoubleProperty dProperty = new DoubleProperty(name);
propertyMap.put(dProperty, Double.parseDouble(val));
}else{
logger.log(Level.INFO, "No such type yet implemented: " + type);
}
}
}
relMapping.put(id, app.create(entityClass, propertyMap));
}else{
logger.log(Level.INFO, "Entityclass not found: " + classType);
}
}
}
}
relMapping = null;
logger.log(Level.INFO, "Transaction will now commit");
tx.success();
} catch (FrameworkException e) {
Logger.getLogger(SchemaDataImport.class.getName()).log(Level.SEVERE, null, e);
}
br.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(SchemaDataImport.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SchemaDataImport.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public boolean requiresEnclosingTransaction() {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment