正在迁移neo4j数据库content/apoc.import.json not working

sycxhyv7  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(72)

我曾经称

call apoc.export.json.all("backup.json",{useTypes:true})

字符串
一方面备份我的数据库内容,另一方面,现在我试图通过先导出到json然后再导入数据来将数据迁移到另一台机器。

call apoc.import.json("backup.json")


根据some neo4j developer,这应该是一个有效的选项。然而,我遇到了一个异常:

Failed to invoke procedure `apoc.import.json`: Caused by: java.lang.RuntimeException: Missing constraint required for import. Execute this query:
CREATE CONSTRAINT FOR (n:MyLabel01) REQUIRE n.neo4jImportId IS UNIQUE;


即使创建这个需求也会导致越来越多的这种类型的错误。
这些错误发生在我尝试将数据从旧的neo4j版本/ apoc版本迁移到新的版本时,也发生在我根据return apoc.version()使用apoc版本5.12.0从 neo4j kernel 5.12.0 enterprise 导出并尝试导入与我导出json相同版本的新数据库时。
所以我试着找一些变通的办法,

CALL apoc.load.json("backup.json") yield value
WHERE value.type = "node"
CALL apoc.create.node(value.labels,value.properties)
yield node return node


但是我也不知道如何导入relashion。

  • 是否有(不同的)标准方法将数据库内容移植到另一台机器上?
  • 或者是否有一些选项可以使用apoc.load.json(“backup.json”)并创建节点和关系?
p5fdfcr1

p5fdfcr11#

解决方法

我写了一个小的python脚本来将导出的json转换成一个cypher CREATE指令,它应该会创建之前导出的数据库内容。
如果导出的json有额外的属性没有被这个脚本处理,它至少会打印一个“错误”列表,这些错误只是json输入中被忽略的部分,这样用户就可以手动检查。

import argparse
import json

def main(file_path):
    with open(file_path) as my_file:
        
        data = []
        for line in my_file:
            data.append(json.loads(line))
        
        instruction = 'CREATE\n'
        for json_obj in data:
            if json_obj['type'] == 'node':
                node_id = json_obj['id']
                json_obj.pop('id')
                label_clause = ''
                if 'labels' in json_obj:
                    for label in json_obj['labels']:
                        label_clause = label_clause + ':' + label
                    json_obj.pop('labels')
                property_clause = ''
                if 'properties' in json_obj:
                    property_clause += '{  '
                    for key in json_obj['properties']:
                        property_clause += key
                        if isinstance(json_obj['properties'][key], str):
                            property_clause += " : \""
                            property_clause += json_obj['properties'][key]
                            property_clause += "\", "
                        else:
                            property_clause += " : "
                            property_clause += str(json_obj['properties'][key])
                            property_clause += ", "
                        
                    property_clause = property_clause[:-2]
                    property_clause += '}'
                    json_obj.pop('properties')
                instruction += ' (node_{}{}{}),\n'.format(node_id, label_clause, property_clause)
        for json_obj in data:
            if json_obj['type'] == 'relationship':
                relationship_id = json_obj['id']
                json_obj.pop('id')
                from_id = json_obj['start']['id']
                json_obj.pop('start')
                to_id = json_obj['end']['id']
                json_obj.pop('end')
                
                label_clause = ''
                if 'label' in json_obj:
                    label_clause = ':{}'.format(json_obj['label'])
                    json_obj.pop('label')
                
                relation_clause = 'relationship_{}{}'.format(relationship_id, label_clause)
                
                instruction += ' (node_{})-[{}]->(node_{}),\n'.format(from_id, relation_clause, to_id)
        print('ERRORS:\n') # should print anything from the original json which has been ignored by the code above
        for json_obj in data:
            json_obj.pop('type')
            if bool(json_obj):
                print(json_obj)
                print()
        
        print()
        print()
        print()
        print()
        
        
        instruction = instruction[:-2] + ';\n'
        
        with open("instruction.txt", 'w') as out_file:
            out_file.write(instruction)
        print(data)
    

    
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='input parser')
    parser.add_argument('--file', metavar='path', required=True,
                        help='the path to the exported json file')
    args = parser.parse_args()
    print(args.file)
    main(file_path=args.file)

字符串

相关问题