2

我正在尝试使用 graphql api 公开火花聚合结果。查询每次都返回 null ,

from flask import Flask, request
from graphene import ObjectType, Schema, String, Int, Field, Float, List
from flask_graphql import GraphQL
import json
from pyspark import SparkContext, SparkConf
from pyspark.sql import SQLContext
import pyspark.sql.functions as func
from pyspark.sql.functions import col

app = Flask(__name__)

conf = SparkConf().setAppName('csv viewer')
sc = SparkContext(conf=conf)
sqlContext = SQLContext(sc)
csv_file = '/path/to/file'
df = sqlContext.read.format('com.databricks.spark.csv').options(header='true',inferschema='true').load(csv_file)

class Aggr(ObjectType):
    country = String()
    percentile = Int()


class Query(ObjectType):
    aggr = List(of_type= Aggr)

    def resolve_aggr(self,info):
        df1 = df.where(col('country') != 'null')
        result = df1.groupby('country').agg(
            func.expr('percentile_approx(responseTime, array(0.95))')[0].alias('percentile')).collect()
        return result

schema = Schema(query = Query)


@app.route("/spark", methods = ['POST'])
def aggr():
    print(request.data) 
    data = json.loads(request.data)
    print(json.loads(request.data)['query'])
    return json.dumps(schema.execute(data["query"]).data)

当我发送一个包含{"query": "{aggr}"}邮递员正文的 POST 请求时,我得到的响应为空。我对 GraphQL 和石墨烯非常陌生,知道我在哪里出错了吗?

4

0 回答 0