示例#1
0
文件: index.py 项目: Sasasu/bgm-graph
def create_client(arg: Arg) -> typing.Optional[GraphClient]:
    connection_pool = ConnectionPool(arg.addr, arg.port)
    client = GraphClient(connection_pool)
    client.set_space('bgm')
    try:
        client.authenticate(arg.u, arg.p)
    except AuthException as e:
        print(e)
        return None

    return client
示例#2
0
def main_test():
    client = None
    try:
        space_name = 'space_' + threading.current_thread().getName()
        print('thread name: %s, space_name : %s' %
              (threading.current_thread().getName(), space_name))
        # Get one client
        client = GraphClient(connection_pool)
        # when connection is broken use the following space to set the current session's space
        client.set_space(space_name)
        auth_resp = client.authenticate('user', 'password')
        if auth_resp.error_code:
            raise AuthException("Auth failed")

        query_resp = client.execute_query('SHOW SPACES')
        if has_space(query_resp.rows, space_name):
            print('has %s, drop it' % space_name)
            do_simple_execute(client, 'DROP SPACE %s' % space_name)

        # Create space mySpace
        do_simple_execute(client, 'CREATE SPACE %s' % space_name)

        do_simple_execute(client, 'USE %s' % space_name)
        time.sleep(1)

        # Create tag and edge
        do_simple_execute(
            client, 'CREATE TAG person(name string, age int); '
            'CREATE EDGE like(likeness double)')

        # It should large than the cycle of loading the schema
        time.sleep(6)

        # Insert vertex and edge
        do_simple_execute(
            client, 'INSERT VERTEX person(name, age) VALUES 1:(\'Bob\', 10)')
        do_simple_execute(
            client, 'INSERT VERTEX person(name, age) VALUES 2:(\'Lily\', 9)')
        do_simple_execute(
            client, 'INSERT VERTEX person(name, age) VALUES 3:(\'Tom\', 10)')
        do_simple_execute(
            client,
            'INSERT VERTEX person(name, age) VALUES 4:(\'Jerry\', 13);INSERT VERTEX person(name, age) VALUES 5:(\'John\', 11)'
        )
        do_simple_execute(client,
                          'INSERT EDGE like(likeness) VALUES 1->2:(80.0)')
        do_simple_execute(client,
                          'INSERT EDGE like(likeness) VALUES 1->3:(70.0)')
        do_simple_execute(
            client,
            'INSERT EDGE like(likeness) VALUES 2->4:(84.0), 3->5:(68.3), 1->5:(97.2)'
        )

        # Query data
        query_resp = client.execute_query(
            'GO FROM 1 OVER like YIELD $$.person.name, '
            '$$.person.age, like.likeness')
        if query_resp.error_code:
            print('Execute failed: %s' % query_resp.error_msg)
            exit(1)

        # Print the result of query
        print(' \n====== The query result of thread[%s]======\n ' %
              threading.current_thread().getName())
        print_value(query_resp.column_names, query_resp.rows)
        client.sign_out()
    except Exception as x:
        print(x)

        client.sign_out()
        exit(1)