nebula
nebula copied to clipboard
Using with connection_pool.session_context('root', 'nebula') as session, the data cannot be written to the database
config = Config() # 定义一个配置
config.max_connection_pool_size = 10 # 设置最大连接数
connection_pool = ConnectionPool() # 初始化连接池
ok = connection_pool.init([('1.16.1.37', 9669)], config)
with connection_pool.session_context('root', 'nebula') as session:
# 创建图空间
session.execute('CREATE SPACE IF NOT EXISTS ttt (vid_type = FIXED_STRING(32))')
# 使用nba空间
session.execute('USE ttt')
session.execute('CREATE TAG IF NOT EXISTS player(name string, age int)') # 创建player标签
session.execute('CREATE TAG IF NOT EXISTS team(name string)') # 创建team标签
session.execute('CREATE TAG IF NOT EXISTS bachelor(name string,speciality string)') # 创建bachelor标签
session.execute('CREATE EDGE IF NOT EXISTS like(likeness int)') # 创建like边
session.execute('CREATE EDGE IF NOT EXISTS serve(start_year int, end_year int)') # 创建serve边
session.execute('CREATE EDGE IF NOT EXISTS teammate(start_year int, end_year int)') # 创建teammate边
从CSV文件中读取数据,插入到player标签中
try:
for index, row in df_player.iterrows():
session.execute('INSERT VERTEX IF NOT EXISTS player(name, age) VALUES "{}":("{}", {})'.format(row['player_id'],row['name'],np.int64(row['age'])))
# 从CSV文件中读取数据,插入到team标签中
for index, row in df_team.iterrows():
session.execute('INSERT VERTEX IF NOT EXISTS team(name) VALUES "{}":("{}")'.format(row['teamid'],row['name']))
# 从csv文件中读取数据,插入到bachelor标签中
for index,row in df_bachelor.iterrows():
session.execute('INSERT VERTEX if not exist bachelor(name,speciality) VALUES "{}":("{}","{}")'.format(row['player_id1'],row['player_id2'],row['speciality']))
# 从CSV文件中读取数据,插入到like边中
for index, row in df_like.iterrows():
session.execute('INSERT EDGE IF NOT EXISTS like(likeness) VALUES "{}"->"{}":({})'.format(row['player_id1'], row['player_id2'], np.int64(row['likeness'])))
# 从CSV文件中读取数据,插入到serve边中
for index, row in df_serve.iterrows():
session.execute('INSERT EDGE IF NOT EXISTS serve(start_year, end_year) VALUES "{}"->"{}":({}, {})'.format(row['player_id1'], row['player_id2'], np.int64(row['start_year']), np.int64(row['end_year'])))
# 从CSV文件中读取数据,插入到teammate边中
for index,row in df_teammate.iterrows():
session.execute('INSERT EDGE IF NOT EXISTS teammate(start_year, end_year) VALUES "{}"->"{}":({},{})'.format(row['player_id1'], row['player_id2'], np.int64(row['start_year']), np.int64(row['end_year'])))
except Exception as e:
print(e)
#关闭连接池
connection_pool.close()
使用上述方法,创建写入数据,程序没有报错,但数据就是写不进去,create tag都不成功,是代码的问题吗,这种方式不是官方给的例子吗
here are the problems i see:
- missing sleep after CREATE - https://github.com/vesoft-inc/nebula-python/blob/251c2537f4feffcc5e45603a1cc4b1897c7258ca/example/GraphClientSimpleExample.py#L42
- not checking error with assert after each execute - https://github.com/vesoft-inc/nebula-python/blob/251c2537f4feffcc5e45603a1cc4b1897c7258ca/example/GraphClientSimpleExample.py#L48
so the errors are returned, but you're not checking for them like above.
in the future, you might want to open python issues in the python repo linked above
有报错信息么?
Are there any error messages?