# nebulagraph **Repository Path**: hengzz/nebulagraph ## Basic Information - **Project Name**: nebulagraph - **Description**: 图数据库应用实践 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-03-05 - **Last Updated**: 2024-03-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Nebula Graph - 图数据库排行榜:https://db-engines.com/en/ranking/graph+dbms - Nebula Graph:https://www.nebula-graph.com.cn/download ### 使用手册 - 文档中心:https://docs.nebula-graph.com.cn/3.6.0/ ### 部署手册 - 安装部署:https://docs.nebula-graph.com.cn/3.6.0/4.deployment-and-installation/1.resource-preparations/ - 机器要求:https://docs.nebula-graph.com.cn/3.6.0/4.deployment-and-installation/1.resource-preparations/#_7 #### 1. Windows 单机测试环境 Windows下只支持Docker方式安装,或者使用虚拟机(CentOS7.8)。 - https://docs.nebula-graph.com.cn/3.6.0/4.deployment-and-installation/2.compile-and-install-nebula-graph/3.deploy-nebula-graph-with-docker-compose/ ``` bash # 创建服务 git clone -b release-3.6 https://github.com/vesoft-inc/nebula-docker-compose.git cd nebula-docker-compose/ docker-compose up -d # 创建studio 下载 nebula-graph-studio-3.8.0.tar.gz mkdir nebula-graph-studio-3.8.0 && tar -zxvf nebula-graph-studio-3.8.0.tar.gz -C nebula-graph-studio-3.8.0 cd nebula-graph-studio-3.8.0 docker-compose pull docker-compose up -d ``` #### 2. CentOS 单机测试环境 - https://docs.nebula-graph.com.cn/3.6.0/4.deployment-and-installation/2.compile-and-install-nebula-graph/4.install-nebula-graph-from-tar/ ``` bash systemctl stop firewalld.service systemctl disable firewalld.service yum install -y vim wget wget https://oss-cdn.nebula-graph.com.cn/package/3.6.0/nebula-graph-3.6.0.el7.x86_64.tar.gz tar -zxvf nebula-graph-3.6.0.el7.x86_64.tar.gz mkdir /data mv nebula-graph-3.6.0.el7.x86_64 /data/nebula-graph-3.6.0 && rm -f nebula-graph-3.6.0.el7.x86_64.tar.gz # 修改配置文件名以应用配置 # 进入解压出的目录,将子目录etc中的文件nebula-graphd.conf.default、nebula-metad.conf.default和nebula-storaged.conf.default重命名, # 删除.default,即可应用 NebulaGraph 的默认配置。 cd /data/nebula-graph-3.6.0/etc mv nebula-metad.conf.default nebula-metad.conf mv nebula-storaged.conf.default nebula-storaged.conf mv nebula-graphd.conf.default nebula-graphd.conf # 服务启动 /data/nebula-graph-3.6.0/scripts/nebula.service stop all /data/nebula-graph-3.6.0/scripts/nebula.service start all /data/nebula-graph-3.6.0/scripts/nebula.service status all # 命令行客户端、可视化界面客户端 # NebulaGraph版本3.6.0 -> Studio版本3.8.0、3.7.0 # https://docs.nebula-graph.com.cn/3.6.0/nebula-studio/deploy-connect/st-ug-deploy/#tar_studio wget https://oss-cdn.nebula-graph.com.cn/nebula-graph-studio/3.8.0/nebula-graph-studio-3.8.0.x86_64.tar.gz tar -zxvf nebula-graph-studio-3.8.0.x86_64.tar.gz mv nebula-graph-studio /data/ && rm -f nebula-graph-studio-3.8.0.x86_64.tar.gz cd /data/nebula-graph-studio ./server # 停止 nebula-graph-studio kill $(lsof -t -i :7001) # Studio页面用户登陆 # Port:Graphd 服务的端口。默认为9669。 # 如果未启用身份验证,可以填写默认用户名 root 和任意密码。 ``` 注: - 从 3.0.0 版本开始,在配置文件中添加的 Storage 主机无法直接读写,配置文件的作用仅仅是将 Storage 主机注册至 Meta 服务中。必须使用ADD HOSTS命令后,才能正常读写 Storage 主机。 - 增加 Storage 主机在下一个心跳周期之后才能生效,为确保数据同步,请等待 2 个心跳周期(20 秒),然后执行SHOW HOSTS查看是否在线。 ``` bash # 查看Storage的端口 # /data/nebula-graph-3.6.0/scripts/nebula.service status all ADD HOSTS : [,: ...]; DROP HOSTS : [,: ...]; SHOW HOSTS STORAGE; # 实例: ADD HOSTS 127.0.0.1:9669 ``` - 管理服务:https://docs.nebula-graph.com.cn/3.6.0/4.deployment-and-installation/manage-service/ - 连接服务:https://docs.nebula-graph.com.cn/3.6.0/4.deployment-and-installation/connect-to-nebula-graph/ - 客户端:https://docs.nebula-graph.com.cn/3.6.0/14.client/1.nebula-client/ - 管理 Storage 主机: https://docs.nebula-graph.com.cn/3.6.0/4.deployment-and-installation/manage-storage-host/ ### 接口手册 #### NebulaGraph Python ``` bash yum install -y python3 python3-pip pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U setuptools pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U pip pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple nebula3-python ``` ``` python #-*- coding: utf-8 -*- from nebula3.gclient.net import ConnectionPool from nebula3.Config import Config # 定义一个Config对象实例 config = Config() config.timeout = 30000 config.idle_time = 30000 config.interval_check = 3 config.max_connection_pool_size = 10 # 定义一个ConnectionPool对象实例 connection_pool = ConnectionPool() ok = connection_pool.init([('192.168.37.130', 9669)], config) # 使用方式1:用户自己控制释放时机 session = connection_pool.get_session('root', '123456') # select space session.execute('USE xd-test') # show tags result = session.execute('SHOW TAGS') print(result) # release session session.release() # 使用方式2:使用with语法糖自动释放 with connection_pool.session_context('root', '123456') as session: session.execute('USE xd-test') result = session.execute('SHOW TAGS') print(result) # close the pool connection_pool.close() ``` - 示例代码:https://github.com/vesoft-inc/nebula-python/blob/release-3.4/README.md#example-of-using-session-pool - nGQL命令汇总:https://docs.nebula-graph.com.cn/3.6.0/2.quick-start/6.cheatsheet-for-ngql-command/ - nGQL 指南:https://docs.nebula-graph.com.cn/3.6.0/3.ngql-guide/1.nGQL-overview/1.overview/ #### 数据空间及范式创建 ``` python #-*- coding: utf-8 -*- from nebula3.gclient.net import ConnectionPool from nebula3.Config import Config import time config = Config() config.max_connection_pool_size = 10 connection_pool = ConnectionPool() ok = connection_pool.init([('192.168.37.130', 9669)], config) assert ok is not False with connection_pool.session_context('root', '123456') as session: resp = session.execute( 'CREATE SPACE IF NOT EXISTS xd_test(vid_type=FIXED_STRING(100)); USE xd_test;' 'CREATE TAG IF NOT EXISTS person(ID INT, UUID FIXED_STRING(32), Name FIXED_STRING(255), Name1 FIXED_STRING(255), IdCard FIXED_STRING(32));' 'CREATE TAG IF NOT EXISTS phone(ID INT, PhoneNum FIXED_STRING(32), Imsi FIXED_STRING(32), Carrier FIXED_STRING(255));' 'CREATE TAG IF NOT EXISTS jizhan(ID INT, Lac INT, CellId INT, latitude Float, longitude Float);' 'CREATE EDGE IF NOT EXISTS serve (UUID FIXED_STRING(32), Imsi FIXED_STRING(32));' 'CREATE EDGE IF NOT EXISTS huadan(ID INT, CallFrom FIXED_STRING(32), CallTo FIXED_STRING(32), call_time Float, call_datetime datetime, latitude Float, longitude Float, Lac INT, CellId INT);' 'CREATE EDGE IF NOT EXISTS duanxin(ID INT, SmsFrom FIXED_STRING(32), SmsTo FIXED_STRING(32), Imsi FIXED_STRING(32), Sms STRING, SmsTime datetime);' ) assert resp.is_succeeded(), resp.error_msg() with connection_pool.session_context('root', '123456') as session: resp = session.execute( 'USE xd_test;' 'CREATE TAG INDEX IF NOT EXISTS person_index on person();' 'CREATE TAG INDEX IF NOT EXISTS phone_index on phone();' 'CREATE TAG INDEX IF NOT EXISTS jizhan_index on jizhan();' 'CREATE EDGE INDEX IF NOT EXISTS serve_index on serve();' 'CREATE EDGE INDEX IF NOT EXISTS huadan_index on huadan();' 'CREATE EDGE INDEX IF NOT EXISTS duanxin_index on duanxin();' 'REBUILD TAG INDEX;' 'REBUILD EDGE INDEX;' 'SHOW TAG INDEX STATUS;' ) assert resp.is_succeeded(), resp.error_msg() print(resp) time.sleep(6) connection_pool.close() ``` 注: - 请在创建索引后,选择合适的时间为存量数据重建索引!!! - 索引功能不会自动对其创建之前已存在的存量数据生效。 - 索引的重建未完成时,依赖索引的查询仅能使用部分索引,因此不能获得准确结果。 - 可以一次重建多个索引,索引名称之间用英文逗号(,)分隔。如果没有指定索引名称,将会重建所有索引。 - 重建完成后,用户可以使用命令SHOW {TAG | EDGE} INDEX STATUS检查索引是否重建完成。 ### 使用手册 - 数据类型:https://docs.nebula-graph.com.cn/3.6.0/3.ngql-guide/3.data-types/1.numeric/ - 查询语句:https://docs.nebula-graph.com.cn/3.6.0/3.ngql-guide/7.general-query-statements/1.general-query-statements-overview/ ### 使用注意事项 ``` # 注意索引创建情况: SHOW TAG INDEXES; SHOW EDGE INDEXES; SHOW TAG INDEX STATUS; SHOW EDGE INDEX STATUS; # 注意nGQL的使用要求: (1) MATCH的注意事项: NebulaGraph 3.6.0 中MATCH语句的性能和资源占用得到了优化.但对性能要求较高时,仍建议使用 GO, LOOKUP, | 和 FETCH 等来替代MATCH。 (2) LOOKUP的注意事项: 使用LOOKUP语句基于指定属性查询时该属性没有索引,系统将报错,而不会使用其它索引。请确保LOOKUP语句有至少一个索引可用。 (索引会导致写性能大幅降低。请不要随意在生产环境中使用索引,除非很清楚使用索引对业务的影响。) ``` ### 守护进程 创建 ng.bash 脚本,放置于 /data 目录下。 ``` bash #!/bin/bash log=`/data/nebula-graph-3.6.0/scripts/nebula.service status all` if [[ $log == *"Exited"* ]]; then /data/nebula-graph-3.6.0/scripts/nebula.service stop all /data/nebula-graph-3.6.0/scripts/nebula.service start all fi exit 0 ``` 配置crontab ``` bash chmod +x /data/ng.sh echo "*/1 * * * * root sh /data/ng.sh" >> /etc/crontab ``` 注:crontab无法达到秒级任务计划。如需秒级监控需更换其他方式。