将SQLite数据库转换为三元组存储

3

请问有人能描述将SQLite数据库转换为三元组存储所需的步骤吗?

是否有工具可以完成此任务?

2个回答

4

当我提出这个问题时,它比看起来要复杂得多,但简单的答案是完全规范化你的数据库。在完全规范化后,每个表代表一个谓词,一个列的值代表主语,另一个列的值代表宾语。基于此,您可以将任意SQL数据库转换为三元存储。


这听起来很棒。是真的吗? :-) … 好的,真的指的是:你能引用一篇论文、一个链接或其他什么吗?谢谢! - Benjohn
不,我无法引用任何东西,据我所知这是原创的。它可以用其他方式完成(即一个具有三列A、B和它们之间关系类型的大表格)。但是当你深入思考表格的含义时,你会意识到它可以代表关系类型(或者不代表,视个人意愿而定)。 - tjb
我想知道如果你用SQLite作为三元组存储方式,性能会如何。 - Rei Miyasaka
不知道,可能会很糟糕,在转换数据库后,最好将其导出到专门用于此目的的工具中:https://jena.apache.org/ - tjb

1
该函数将任何类型的关系型数据转换为三元组格式:
   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函数返回数据库表中的列列表:

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

有一个点赞按钮,但没有喜欢按钮!我想要喜欢。 - Benjohn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接