Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2013 04:57
Show Gist options
  • Save anonymous/6068164 to your computer and use it in GitHub Desktop.
Save anonymous/6068164 to your computer and use it in GitHub Desktop.
Code to generate a CSV file from RDF document. Each row represents an individual with all its property values in columns. The separator is ";"
import com.hp.hpl.jena.rdf.model.ModelFactory
import scala.collection.JavaConversions._
val m = ModelFactory.createOntologyModel
m.read("****INPUT****","N3")
val properties = (m.listDatatypeProperties.toList ++ m.listObjectProperties)
val individualRows = m.listIndividuals.toList.map(i =>
i.getURI + ";" + properties.
map(p => i.listPropertyValues(p).toList.mkString(",")).mkString(";")
)
val printer = new java.io.PrintWriter("out1.csv")
printer.println("URI;" + properties.map(_.getURI).mkString(";"))
printer.println(individualRows.mkString("\n"))
printer.close
@psttf
Copy link

psttf commented Jul 24, 2013

i'm the author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment