Skip to content

Instantly share code, notes, and snippets.

@slok
Created July 14, 2011 13:11
Show Gist options
  • Save slok/1082419 to your computer and use it in GitHub Desktop.
Save slok/1082419 to your computer and use it in GitHub Desktop.
Mysql and Redland test in python
#Copyright (C) 2011 Iraide (Sharem) Diaz <iraide.diaz [at] gmail [dot] com>
#Copyright (C) 2011 Xabier (sLoK) Larrakoetxea <slok69 [at] gmail [dot] com>
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
import RDF
import Redland
import MySQLdb
def create_mysql_db(user, password, db):
"""
Creates a mysql database
"""
connDb=MySQLdb.connect(host="localhost", user=user, passwd=password)
c=connDb.cursor()
c.execute('CREATE DATABASE ' + db)
print("[" + db +" DATABASE CREATED ]")
def connect_librdf_mysql(user, password, db):
"""
Connects Redland(libRDF) and mysql
returns Storage instance
"""
options = ''
#options += 'contexts=\'yes\', '
options += 'write=\'false\', '
options += 'host=\'localhost\', '
options += 'database=\'' + db + '\', '
options += 'user=\'' + user + '\', '
options += 'password=\'' + password + '\''
try:
storage=RDF.Storage(storage_name="mysql",
name=db,
options_string=options)
except:
create_mysql_db(user, password, db)
# if the new = true is always on, then the 'new' ruins the stored data every time we connect
# (but the data continues in the datasabase, stored) is a weird "issue", so we only put the
# flag to true, the first time that the database needs to be created otherwise the queries always are "Null"
options += ', new=\'true\''
storage=RDF.Storage(storage_name="mysql",
name=db,
options_string=options)
print("[ CONNETED TO "+ db +" DATABASE ]")
return storage
def sparql_query(query, user, password, db):
"""
Executes an SparQL query in the mysql database eschema
and returns the results (in string or in stream, depends on
the type of the result)
"""
st= connect_librdf_mysql(user, password, db)
model=RDF.Model(st)
q1 = RDF.Query(query ,query_language='sparql')
q1Result = q1.execute(model)
#q1Result = model.execute(q1)
print("[ SPARQL QUERY DONE ]")
#Redland.librdf_free_storage(st._storage);
#return in str, then we can parse the string(XML) and serialize to other formats
return q1Result.to_string()
def store_RDF(rdfPath, user, password, db):
"""
Stores an RDF file in the MySQL endpoint
"""
st= connect_librdf_mysql(user, password, db)
model=RDF.Model(st)
#Redland.librdf_model_transaction_start(model._model)
try:
# Do something
parser=RDF.Parser(name="rdfxml",mime_type="application/rdf+xml")
parser.parse_into_model(model, rdfPath)
#Redland.librdf_model_transaction_commit(model._model)
#model.sync()
except:
pass
#Redland.librdf_model_transaction_rollback(model._model)
print("[ RDF STORED ]")
#Redland.librdf_free_storage(st._storage);
def parse_link(link, parserType='rdfxml'):
"""parses a link and returns stream statements
link to the file
parser type: rdfxml, ntriples, turtle, trig, guess, rss-tag-soup, rdfa, nquads, grddl
"""
parser = RDF.Parser(name=parserType)
return parser.parse_as_stream(link)
def parse_string(strContent, url, parserType='rdfxml'):
"""
"""
parser = RDF.Parser(name=parserType)
return parser.parse_string_as_stream(strContent, url)
def serialize_stream(stream, serializerType='rdfxml'):
"""changes the format(serialize) from stream object(Redland) to a format
returns a string
link to the file
serializer type: rdfxml, rdfxml-abbrev, turtle, ntriples, rss-1.0, dot, html, json, atom, nquads
"""
ser = RDF.Serializer(name=serializerType)
return ser.serialize_stream_to_string(stream)
if __name__ == '__main__':
user = 'root'
password = 'xxxxxxxxxxx'
db = 'newRedland'
query = 'PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?aname ?bname WHERE { ?a foaf:knows ?b . ?a foaf:name ?aname . ?b foaf:name ?bname . }'
#STORE SOME RDF
#store_RDF('http://paginaspersonales.deusto.es/dipina/resources/foaf.rdf', user, password, db)
#store_RDF('http://paginaspersonales.deusto.es/josuka/foaf.rdf', user, password, db)
#SOME QUERIES
x = sparql_query(query, user, password, db)
print x
#SAVE QUERIES IN FILE
#f = open('./sparql.rdf', 'wb')
#f.write(x)
#f.close()
#TEST TO SERIALIZE IN OTHER FORMATS (JSON, DOT...) [DOESN'T WORK -.-]
#strRes = sparql_query(query, user, password, db)
#strSer = parse_string(strRes, 'http://paginaspersonales.deusto.es/')
#print serialize_stream(strSer, 'dot')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment