【问题标题】:How to prevent titan generate duplicate records with same property?如何防止泰坦生成具有相同属性的重复记录?
【发布时间】:2016-12-07 03:01:44
【问题描述】:

我有 spark 应用程序,它使用 goblin 将数据插入到 Titan 中。但它会插入具有相同名称的重复顶点。测试条件 'if not result:' 不匹配,并且我在同一个会话中。

def savePartition(p):
    print ('savePartition', p)
    from goblin import element, properties

    class Brand(element.Vertex):
        name = properties.Property(properties.String)

    import asyncio

    loop = asyncio.get_event_loop()

    from goblin.app import Goblin
    app = loop.run_until_complete(Goblin.open(loop))
    app.register(Brand)

    async def go(app):
        session = await app.session()

        for i in p:
            if i['brand']:
                traversal = session.traversal(Brand)
                result = await traversal.has(Brand.name, i['brand']).oneOrNone()

                if not result:  # TODO: Remove Duplicates
                    print(i)
                    brand = Brand()
                    brand.name = i['brand']
                    session.add(brand)
                    session.flush()

        await app.close()

    loop.run_until_complete(go(app))

rdd = rdd.foreachPartition(savePartition)

如何解决?非常感谢。

【问题讨论】:

    标签: pyspark titan goblin


    【解决方案1】:

    我不确定这将如何与 Goblin 一起使用,但如果您希望 Titan 基于顶点属性防止重复,您可以使用 Titan composite indices 并指定它们必须是唯一的。例如,您可以执行以下操作:

    mgmt = graph.openManagement()
    name = mgmt.makePropertyKey('name').dataType(String.class).make()
    mgmt.buildIndex('byNameUnique', Vertex.class).addKey(name).unique().buildCompositeIndex()
    mgmt.commit()
    

    上面将指定顶点上的name属性必须是唯一的。

    【讨论】:

    • 谢谢!我还有一个问题。如果我想要标签唯一的名称,如何实现?
    • 如果您询问如何确保标签是唯一的,那么 Titan 将无法帮助您。标签并不意味着是唯一的。结帐this回答以获取更多信息。
    • 我也使用弹性搜索作为外部索引,但泰坦告诉An external index cannot be unique,有什么想法吗?
    • 弹性搜索用于混合索引,如here**Note** Unlike composite indexes, mixed indexes do not support uniqueness. 所述,为了唯一性,最好使用我上面定义的简单复合索引。
    猜你喜欢
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多