Converting a SQLite Database to a triple store

流过昼夜 提交于 2019-12-05 18:32:17

This is a more complicated question then it seemed when I asked it, but the simple answer is that you normalize your database completely. After it is completely normalized each table stands for a predicate, one columns values represent the subject and one columns values represent the object. You can convert an arbitrary sql database to a triplestore on this basis.

The function transform to triple convert any kind of relational data into triple format:

   def transform_to_triple(source,db_name,table,result):
    #get the list of relations for the selected DB
    max_records = 100
    response = []
    x_print = lambda *x : response.append("(%s)\n" %("".join(["%s"%(v) for v in x])))

    id = 1

    x_print(id,',(db_name:string),',db_name)
    logger.info("(%s,(db_name,string), %s)" %(id,db_name))

    tables = []
    table_list = [table,]
    for i, _table in enumerate(table_list):
        _table_id = id + i + 1
        x_print(id,',(rel:id),', _table_id)
        logger.info("(%s,(rel, id), %s)" %(id, _table_id))

        _schema = get_column_list(source, db_name,_table)
        tables.append((_table_id, _table, _schema))
    for _table in tables:
        _table_id = _table[0]
        x_print(_table_id,',(rel_name:string),',_table[1])
        for j,row in enumerate(result):
            #lets assume there is always less than 10 k tuples in a table
            _tuple_id = _table_id * max_records + j + 1
            x_print(_table[0],',(tuple:id),', _tuple_id)
            logger.info("(%s,(tuple, id), %s)" %(_table[0],_tuple_id))
        for j,row in enumerate(result):
            _tuple_id = _table_id * max_records + j + 1
            for k,value in enumerate(row):
                x_print(_tuple_id, ",(%s : %s)," %(_table[2][k][0], _table[2][k][1]), value)   
    return "%s" %("".join(response))

get_column_list function returns the list of columns within a database tables:

def get_column_list(src_name,db_name,table_name):
     cur = get_connect() #Connecting with tool DB
     query = '''select db_name, host, user_name, password from "DataSource" where src_name = '%s' and db_name = '%s' '''%(src_name, db_name)
     cur.execute(query)
     data  = cur.fetchall()
     (db, host, username, password) = data[0]
     _module = get_module(src_name)
     cursor = _module.get_connection(db, host, username, password)
     try:
          _column_query = _module.COLUMN_LIST_QUERY %(db_name, table_name)
     except TypeError, e:
          try:
               _column_query = _module.COLUMN_LIST_QUERY %(table_name)
          except TypeError, e:
               _column_query = _module.COLUMN_LIST_QUERY

     cursor.execute(_column_query)
     column_list = cursor.fetchall()
     return column_list
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!