How to import csv and mapping file in stardog database using Java?

How to import csv and mapping file in Stardog database using Java ?

If you have access to the Stardog client jars, something like this should work:

	void importFile(String dbName) throws Exception {
		String aServer = "http://localhost:5820/" + dbName;

		AdminConnectionConfiguration aAdminConnectionConfiguration = AdminConnectionConfiguration.toServer(aServer);
		aAdminConnectionConfiguration = aAdminConnectionConfiguration.credentials("admin", "admin"));

		AdminConnection result = aAdminConnectionConfiguration.connect();

		VirtualGraphAdminConnection vgConn = result.as(VirtualGraphAdminConnection.class);

		Properties properties = new Properties();

		properties.put(VirtualGraphOptions.MAPPINGS_SYNTAX, VirtualGraphMappingSyntax.SMS2.toString());

		String mappingsString = "PREFIX : <http://example.com/>\n" +
		                        "\n" +
		                        "mapping\n" +
		                        "from csv {\n" +
		                        "}\n" +
		                        "to {\n" +
		                        "  ?subj a :Person ;\n" +
		                        "  :name ?name ;\n" +
		                        "  :firstNameUsingSourceField ?first_name_using_sourceField ;\n" +
		                        "  :firstNameUsingSanitizedVar ?First_Name ;\n" +
		                        "  :age ?age ;\n" +
		                        "  .\n" +
		                        "}\n" +
		                        "where {\n" +
		                        "  # Template allows a source field with a space in it\n" +
		                        "  bind(template(\"http://example.com/{First Name}\") as ?subj)\n" +
		                        "  # sourceField not at root of expression tree\n" +
		                        "  bind(concat(sourceField(\"First Name\"), \" \", sourceField(\"Last-Name\")) as ?name)\n" +
		                        "  # sourceField at root of expression tree\n" +
		                        "  bind(sourceField(\"First Name\") as ?first_name_using_sourceField)\n" +
		                        "}\n";

		vgConn.importFile(mappingsString, mProperties, dbName, Values.iri(mNamedGraph), mInputFile, InputFileType.DELIMITED);
	}

The import API is a rest call that takes a multi-part form input. The API is documented here:
https://stardog-union.github.io/http-docs/#operation/importDb

You can read about the property options here:

I recommend starting with the CLI command in order to make sure your properties and mappings are correct before tackling the Java API.

-Paul

1 Like

Thanks for your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.