Skip to content

Instantly share code, notes, and snippets.

@lawlesst
Created September 10, 2012 20:35
Show Gist options
  • Save lawlesst/3693644 to your computer and use it in GitHub Desktop.
Save lawlesst/3693644 to your computer and use it in GitHub Desktop.
Load an external graph to a Jena SDB model.
#! /usr/bin/env jython
"""
Load a graph into a given SDB model.
"""
import glob
import sys
import os
import optparse
import imp
from datetime import datetime
#Base directory from project
BASE = os.path.dirname(os.path.abspath(__file__))
#Directories containing jars.
libs = [
os.path.join(BASE, 'javalib'),
]
jars = []
#Get all the jars in lib and append to sys.path
for path in libs:
jars += glob.glob('%s/*.jar' % path)
for jar in jars:
pth = os.path.join(BASE, jar)
print>>sys.stderr, pth
sys.path.append(pth)
from java.lang import System
from com.hp.hpl.jena.rdf.model import *
from com.hp.hpl.jena.vocabulary import *
from com.mysql.jdbc import *
from java.sql import *
from com.hp.hpl.jena.db import DBConnection;
from com.hp.hpl.jena.sdb.sql import SDBConnection
from com.hp.hpl.jena.sdb.store import LayoutType
from com.hp.hpl.jena.sdb.store import DatabaseType
from com.hp.hpl.jena.sdb import StoreDesc, SDBFactory
from com.hp.hpl.jena.rdf.model.impl import ResourceImpl
from com.hp.hpl.jena.ontology import OntModel;
from com.hp.hpl.jena.ontology import OntModelSpec;
from com.hp.hpl.jena.util import FileManager;
def attach(settings, graph):
#Default to the default Vivo model
if getattr(settings, 'NAMED_MODEL', None) is None:
NAMED_MODEL = 'http://vitro.mannlib.cornell.edu/default/vitro-kb-2'
#Create database connection
storeDesc = StoreDesc(LayoutType.LayoutTripleNodesHash,
DatabaseType.MySQL);
conn = SDBConnection( settings.DB_URL,
settings.DB_USER,
settings.DB_PASSWD );
store = SDBFactory.connectStore(conn, storeDesc);
dataset = SDBFactory.connectDataset(store);
db_model = dataset.getNamedModel(NAMED_MODEL)
model = FileManager.get().loadModel(graph, settings.graph_format)
#model.write(System.out, settings.graph_format);
#start a transaction
db_model.begin()
#Add the temp graph
db_model.add(model)
db_model.commit()
db_model.close()
store.close()
conn.close()
print>>sys.stderr, "Graph attached sucessfully."
def main(settings, graph):
attach(settings, graph)
if __name__ == "__main__":
p = optparse.OptionParser()
p.add_option('--settings',
'-s',
default='settings.py',
help='Pass in the path to the settings file.\
Defaults to settings.py in the working directory',
)
p.add_option('--testing', '-t',
action='store_true',
dest='test',
help="Pass in -t for testing. Will output only 10\
records per record set.")
p.add_option('--graph', '-g',
dest='graph',
help='Pass in the path to a graph in N3.')
p.add_option('--format', '-f',
dest='format',
default='N3')
options, arguments = p.parse_args()
real_path = os.path.realpath(options.settings)
settings = imp.load_source('*', real_path)
settings.graph_format = options.format
if options.test:
settings.testing=True
else:
settings.testing=False
main(settings, options.graph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment