【大数据】 Neo4J + python flask完成简单后台服务

397 阅读6分钟

image.png

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第23天,点击查看活动详情

前言

这个月已经更新了23天了,虽然没啥营养吧,但也是日常工作和学习的记录,今天也是周末,还是坚持写一些记录一下,在昨天的文章中,记录了对于关系查询按照G6前端组件所需的数据格式,格式并不复杂,分为combos【类别】,edges【边】和nodes【节点】三个组成部分,而前端使用ajax或者axios调用后端数据的时候,往往需要启用服务,一般来说如java springboot、springmvc等微服务框架,而咱们的数据处理过程是使用的python,因此就选择Flask作为后端服务框架吧

Flask

image.png

Flask既然是python的微服务框架,使用该框架就能够完成一个轻量级的web服务,本文很多用例都参考 Flask官方文档,大部分代码也是引用自官方文档。

flask的安装

python的话很简单的,使用pip一下就好

pip install flask

demo

首先导入flask, app = Flask()则是将类实例化,Flask类的构造函数只有一个必须指定的参数name,用于告诉程序该应用的位置,进而找到应用中其他文件的位置。@app.route('/')装饰器定义了一个路由,当我们点击web应用的时候,前端访问后端程序,而路由就是python方法到前端url的映射,当我们运行代码的时候,点击127.0.0.1:5000就能看到hello world。

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'hello world'
if __name__ == '__main__':
    app.run(host='127.0.0.1',port=5000)

运行结果如下:

image.png

变量

@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return 'User %s' % username

@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id

flask允许使用的形式获取请求的参数,包括int型或者string类型等,在web应用中调用对应的url,就能获取相应的请求参数,如下:

image.png

HTTP方法

使用route装饰器的methods参数设置即可完成http方法的接收,如下:

@app.route('/http', methods=['GET', 'POST'])
def testHttp():
    if request.method == 'POST':
        print("使用了Post方法")
    else:
        print("使用了Get方法")

结果如下:

image.png

Request

Request 对象是一个全局对象,利用它的属性和方法,我们可以方便的获取从页面传递过来的参数。

@app.route('/http', methods=['GET', 'POST'])
def testHttp():
    if request.method == 'POST':
        print(request.form.get("key"))
        print("使用了Post方法")
        return "使用了Post方法"
    else:
        print(request.args.get("key"))
        print("使用了Get方法")
        return "使用了Get方法"

结果如下:

image.png

image.png

查询关系接口

有了上面的铺垫,我们将昨天的关系封装为接口,代码如下:
@app.route('/getRelations', methods=['GET'])
def getRelations():
    nodeA = request.args.get("nodeA")
    nodeB = request.args.get("nodeB")
    nodeC = request.args.get("nodeC")
    nodeAprop = request.args.get("nodeAprop")
    nodeBprop = request.args.get("nodeBprop")
    nodeCprop = request.args.get("nodeCprop")
    limit = request.args.get("limit")
    data = dataFormat().formatG6Relations(search4Node().get3PointRel(nodeA,nodeB,nodeC,nodeAprop,nodeBprop,nodeCprop,limit))
    responseData = {"data": data,
                    "msg":"成功",
                    "code":200}
    return responseData

结果如下:

image.png

{
    "code": 200,
    "data": {
        "combos": [
            {
                "id": "a",
                "label": "company"
            },
            {
                "id": "b",
                "label": "product"
            },
            {
                "id": "c",
                "label": "cveNumber"
            }
        ],
        "edges": [
            {
                "source": 0,
                "target": 1
            },
            {
                "source": 2,
                "target": 3
            },
            {
                "source": 0,
                "target": 4
            },
            {
                "source": 5,
                "target": 6
            },
            {
                "source": 0,
                "target": 7
            },
            {
                "source": 8,
                "target": 9
            },
            {
                "source": 0,
                "target": 10
            },
            {
                "source": 11,
                "target": 12
            },
            {
                "source": 0,
                "target": 10
            },
            {
                "source": 11,
                "target": 13
            },
            {
                "source": 0,
                "target": 14
            },
            {
                "source": 15,
                "target": 16
            },
            {
                "source": 0,
                "target": 17
            },
            {
                "source": 18,
                "target": 19
            },
            {
                "source": 0,
                "target": 20
            },
            {
                "source": 21,
                "target": 19
            },
            {
                "source": 0,
                "target": 22
            },
            {
                "source": 23,
                "target": 19
            },
            {
                "source": 0,
                "target": 24
            },
            {
                "source": 25,
                "target": 19
            },
            {
                "source": 0,
                "target": 26
            },
            {
                "source": 27,
                "target": 28
            },
            {
                "source": 0,
                "target": 29
            },
            {
                "source": 30,
                "target": 31
            },
            {
                "source": 0,
                "target": 29
            },
            {
                "source": 30,
                "target": 28
            },
            {
                "source": 0,
                "target": 29
            },
            {
                "source": 30,
                "target": 32
            },
            {
                "source": 0,
                "target": 29
            },
            {
                "source": 30,
                "target": 33
            },
            {
                "source": 0,
                "target": 34
            },
            {
                "source": 35,
                "target": 31
            },
            {
                "source": 0,
                "target": 34
            },
            {
                "source": 35,
                "target": 28
            },
            {
                "source": 0,
                "target": 34
            },
            {
                "source": 35,
                "target": 32
            },
            {
                "source": 0,
                "target": 34
            },
            {
                "source": 35,
                "target": 33
            },
            {
                "source": 0,
                "target": 36
            },
            {
                "source": 37,
                "target": 38
            },
            {
                "source": 0,
                "target": 36
            },
            {
                "source": 37,
                "target": 39
            },
            {
                "source": 0,
                "target": 36
            },
            {
                "source": 37,
                "target": 40
            },
            {
                "source": 0,
                "target": 36
            },
            {
                "source": 37,
                "target": 41
            },
            {
                "source": 0,
                "target": 36
            },
            {
                "source": 37,
                "target": 42
            },
            {
                "source": 0,
                "target": 43
            },
            {
                "source": 44,
                "target": 38
            },
            {
                "source": 0,
                "target": 43
            },
            {
                "source": 44,
                "target": 39
            },
            {
                "source": 0,
                "target": 43
            },
            {
                "source": 44,
                "target": 40
            },
            {
                "source": 0,
                "target": 43
            },
            {
                "source": 44,
                "target": 41
            },
            {
                "source": 0,
                "target": 43
            },
            {
                "source": 44,
                "target": 42
            },
            {
                "source": 0,
                "target": 45
            },
            {
                "source": 46,
                "target": 38
            },
            {
                "source": 0,
                "target": 45
            },
            {
                "source": 46,
                "target": 39
            },
            {
                "source": 0,
                "target": 45
            },
            {
                "source": 46,
                "target": 41
            },
            {
                "source": 0,
                "target": 45
            },
            {
                "source": 46,
                "target": 42
            },
            {
                "source": 0,
                "target": 47
            },
            {
                "source": 48,
                "target": 49
            },
            {
                "source": 0,
                "target": 50
            },
            {
                "source": 51,
                "target": 49
            },
            {
                "source": 0,
                "target": 52
            },
            {
                "source": 53,
                "target": 54
            },
            {
                "source": 0,
                "target": 55
            },
            {
                "source": 56,
                "target": 54
            }
        ],
        "nodes": [
            {
                "comboId": "a",
                "id": 1
            },
            {
                "comboId": "b",
                "id": 2
            },
            {
                "comboId": "b",
                "id": 3
            },
            {
                "comboId": "c",
                "id": 4
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 5
            },
            {
                "comboId": "b",
                "id": 6
            },
            {
                "comboId": "c",
                "id": 7
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 8
            },
            {
                "comboId": "b",
                "id": 9
            },
            {
                "comboId": "c",
                "id": 10
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 11
            },
            {
                "comboId": "b",
                "id": 12
            },
            {
                "comboId": "c",
                "id": 13
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 10
            },
            {
                "comboId": "b",
                "id": 11
            },
            {
                "comboId": "c",
                "id": 14
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 15
            },
            {
                "comboId": "b",
                "id": 16
            },
            {
                "comboId": "c",
                "id": 17
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 18
            },
            {
                "comboId": "b",
                "id": 19
            },
            {
                "comboId": "c",
                "id": 20
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 21
            },
            {
                "comboId": "b",
                "id": 22
            },
            {
                "comboId": "b",
                "id": 19
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 23
            },
            {
                "comboId": "b",
                "id": 24
            },
            {
                "comboId": "b",
                "id": 19
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 25
            },
            {
                "comboId": "b",
                "id": 26
            },
            {
                "comboId": "b",
                "id": 19
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 27
            },
            {
                "comboId": "b",
                "id": 28
            },
            {
                "comboId": "c",
                "id": 29
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 30
            },
            {
                "comboId": "b",
                "id": 31
            },
            {
                "comboId": "c",
                "id": 32
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 29
            },
            {
                "comboId": "b",
                "id": 30
            },
            {
                "comboId": "b",
                "id": 28
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 29
            },
            {
                "comboId": "b",
                "id": 30
            },
            {
                "comboId": "c",
                "id": 33
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 29
            },
            {
                "comboId": "b",
                "id": 30
            },
            {
                "comboId": "c",
                "id": 34
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 35
            },
            {
                "comboId": "b",
                "id": 36
            },
            {
                "comboId": "b",
                "id": 31
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 34
            },
            {
                "comboId": "b",
                "id": 35
            },
            {
                "comboId": "b",
                "id": 28
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 34
            },
            {
                "comboId": "b",
                "id": 35
            },
            {
                "comboId": "b",
                "id": 32
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 34
            },
            {
                "comboId": "b",
                "id": 35
            },
            {
                "comboId": "b",
                "id": 33
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 37
            },
            {
                "comboId": "b",
                "id": 38
            },
            {
                "comboId": "c",
                "id": 39
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 36
            },
            {
                "comboId": "b",
                "id": 37
            },
            {
                "comboId": "c",
                "id": 40
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 36
            },
            {
                "comboId": "b",
                "id": 37
            },
            {
                "comboId": "c",
                "id": 41
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 36
            },
            {
                "comboId": "b",
                "id": 37
            },
            {
                "comboId": "c",
                "id": 42
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 36
            },
            {
                "comboId": "b",
                "id": 37
            },
            {
                "comboId": "c",
                "id": 43
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 44
            },
            {
                "comboId": "b",
                "id": 45
            },
            {
                "comboId": "b",
                "id": 38
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 43
            },
            {
                "comboId": "b",
                "id": 44
            },
            {
                "comboId": "b",
                "id": 39
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 43
            },
            {
                "comboId": "b",
                "id": 44
            },
            {
                "comboId": "b",
                "id": 40
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 43
            },
            {
                "comboId": "b",
                "id": 44
            },
            {
                "comboId": "b",
                "id": 41
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 43
            },
            {
                "comboId": "b",
                "id": 44
            },
            {
                "comboId": "b",
                "id": 42
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 46
            },
            {
                "comboId": "b",
                "id": 47
            },
            {
                "comboId": "b",
                "id": 38
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 45
            },
            {
                "comboId": "b",
                "id": 46
            },
            {
                "comboId": "b",
                "id": 39
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 45
            },
            {
                "comboId": "b",
                "id": 46
            },
            {
                "comboId": "b",
                "id": 41
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 45
            },
            {
                "comboId": "b",
                "id": 46
            },
            {
                "comboId": "b",
                "id": 42
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 48
            },
            {
                "comboId": "b",
                "id": 49
            },
            {
                "comboId": "c",
                "id": 50
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 51
            },
            {
                "comboId": "b",
                "id": 52
            },
            {
                "comboId": "b",
                "id": 49
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 53
            },
            {
                "comboId": "b",
                "id": 54
            },
            {
                "comboId": "c",
                "id": 55
            },
            {
                "comboId": "a",
                "id": 0
            },
            {
                "comboId": "b",
                "id": 56
            },
            {
                "comboId": "b",
                "id": 57
            },
            {
                "comboId": "b",
                "id": 54
            }
        ]
    },
    "msg": "成功"
}

这样大概的接口就处理完毕了,明天部署之后供前端小姐姐联调~ 蟹蟹~