Difference between revisions of "PyLoDStorage"

From BITPlan Wiki
Jump to navigation Jump to search
Line 74: Line 74:
 
The resultList will be the same as the original listOfRecords.
 
The resultList will be the same as the original listOfRecords.
 
= JSON =
 
= JSON =
 +
= Apache Jena =
 +
The jena -l and jena -f options will automatically download and unpack the needed Apache jena files.
 +
== Jena load example dataset ==
 +
<source lang='bash'>
 +
scripts/jena -l sampledata/example.ttl
 +
</source>
 +
== Jena fuseki server start ==
 +
<source lang='bash'>
 +
scripts/jena -f example
 +
</source>
 +
You should be able to browse the admin GUI at http://localhost:3030 and have the example dataset ready for you
 +
== Jena fuseki server stop ==
 +
<source lang='bash'>
 +
scripts/jena -k
 +
</source>
 +
 +
== jena script usage ==
 +
<source lang='bash'>
 +
scripts/jena -h
 +
scripts/jena [-f|--fuseki|-h|--help|-k|--kill|-l|--load]
 +
 +
-f | --fuseki [dataset]: download and start fuseki server with the given dataset
 +
-h | --help: show this usage
 +
-k | --kill: kill the running fuseki server
 +
-l | --load [ttl file]: download jena / tdbloader and load given ttl file
 +
</source>

Revision as of 08:01, 22 September 2020

OsProject

OsProject
edit
id  PyLodStorage
state  
owner  Wolfgang Fahl
title  python List of Dict (Table) Storage library
url  https://github.com/WolfgangFahl/pyLodStorage
version  0.0.12
description  
date  2020/09/21
since  
until  

see also DgraphAndWeaviateTest

List of Dicts = Table

a list of dicts(Hashtables) in python can be interpreted as Table which is suitable to be stored

  1. in a relational database like sqlite3
  2. as JSON

Royals example

 @staticmethod
    def getRoyals():
        listOfDicts=[
            {'name': 'Elizabeth Alexandra Mary Windsor', 'born': Sample.dob('1926-04-21'), 'numberInLine': 0, 'wikidataurl': 'https://www.wikidata.org/wiki/Q9682' },
            {'name': 'Charles, Prince of Wales',         'born': Sample.dob('1948-11-14'), 'numberInLine': 1, 'wikidataurl': 'https://www.wikidata.org/wiki/Q43274' },
            {'name': 'George of Cambridge',              'born': Sample.dob('2013-07-22'), 'numberInLine': 3, 'wikidataurl': 'https://www.wikidata.org/wiki/Q1359041'},
            {'name': 'Harry Duke of Sussex',             'born': Sample.dob('1984-09-15'), 'numberInLine': 6, 'wikidataurl': 'https://www.wikidata.org/wiki/Q152316'}
        ]
        today=date.today()
        for person in listOfDicts:
            born=person['born']
            age=(today - born).days / 365.2425
            person['age']=age
            person['ofAge']=age>=18
            person['lastmodified']=datetime.now()
        return listOfDicts

The above list of dict can be stored in a Person table with the following structure:

SQL

The idea is to derive the necessary DDL and SQL command automatically:

CREATE TABLE Family(name TEXT PRIMARY KEY,country TEXT,lastmodified TIMESTAMP)
INSERT INTO Family (name,country,lastmodified) values (:name,:country,:lastmodified)

and use them via simple API

from lodstorage.sample import Sample
from lodstorage.sql import SQLDB, EntityInfo

 listOfRecords=Sample.getRoyals()
 sqlDB=SQLDB()
 entityName='Person'
 primaryKey='name'
 entityInfo=self.sqlDB.createTable(listOfRecords[:10],entityName,primaryKey)
 sqlDB.store(listOfRecords,entityInfo)
 resultList=self.sqlDB.queryAll(entityInfo)

The resultList will be the same as the original listOfRecords.

JSON

Apache Jena

The jena -l and jena -f options will automatically download and unpack the needed Apache jena files.

Jena load example dataset

scripts/jena -l sampledata/example.ttl

Jena fuseki server start

scripts/jena -f example

You should be able to browse the admin GUI at http://localhost:3030 and have the example dataset ready for you

Jena fuseki server stop

scripts/jena -k

jena script usage

scripts/jena -h
scripts/jena [-f|--fuseki|-h|--help|-k|--kill|-l|--load]

-f | --fuseki [dataset]: download and start fuseki server with the given dataset
-h | --help: show this usage
-k | --kill: kill the running fuseki server
-l | --load [ttl file]: download jena / tdbloader and load given ttl file