代码之家  ›  专栏  ›  技术社区  ›  ankush reddy

spark处理csv文件中的json数据

  •  1
  • ankush reddy  · 技术社区  · 8 年前

    如何处理csv文件中的json数据,我试图使用的是from_json,但是我需要指定我的模式在哪里,因为我的模式一直在变化。

    样本输入:-

    userid   type          data
    26594    p.v    {}                                                                                                                                                                                                                                                                                                                                                             
    26594    s.s    {"sIds":["1173","1342","12345"]}
    
    26594    s.r    {"bp":"sw"}                                                                                                                                                                                                                                                                                                                                
    26594    p.v      {}                                                                                                                                                                                                                                                                                                                                                             
    26594    s.r     {"c":"tracking","action":"n","label":"ank"}                                                                                                                                                                                                                                                                            
    26593    p.v     {}                                                                                                                                                                                                                                                                                                                                                             
    26594    p.sr     {"pId":"11234","pName":"sahkas","s":"n","is":"F","totalCount":0,"scount":0}  
    

    我想把它转换成一个数据框架,我们可以用它来查询json。

    寻找如下输出:-

     userid    type    data_sids    data_bp    data_c    data_action    data_label
     26594     p.v      null         null       null     null    null
     26594     s.s      1173         null       null     null    null
     26594     s.s      1173         null       null     null    null  
     26594     s.s      1342         null       null     null    null
     26594     s.s      12345         null       null     null    null  
     26594      s.r     null          sw          null    null     null
    

    这可行吗?

    你能帮我做这个吗。

    谢谢,

    安库什·雷迪。

    1 回复  |  直到 8 年前
        1
  •  1
  •   dogli980    8 年前

    我的建议是与RDD合作完成这项任务。 像这样写:

    rdd = # collection of Rows with the following fields: userid, type, data - your CSV
    def flatten_json(userid, type, data_json):
        final_row = {"userid": userid, "type": type, "data_sids": data_json["sids"], ...}
        return Row(**final_row)
    rdd = rdd.map(lambda row: flatten_json(row["userid"], row["type"], row["data"]))
    df = spark.createDataFrame(rdd)
    

    就这样:)

    推荐文章