简体   繁体   English

使用 spark scala 从 json 结构中获取电影类别

[英]Get category of movie from json struct using spark scala

I have a df_movies and col of geners that look like json format.我有一个 df_movies 和 col of geners 看起来像 json 格式。

|genres | |流派|
[{'id': 28, 'name': 'Action'}, {'id': 12, 'name': 'Adventure'}, {'id': 37, 'name': 'Western'}] [{'id': 28, 'name': 'Action'}, {'id': 12, 'name': 'Adventure'}, {'id': 37, 'name': 'Western'}]

How can I extract the first field of 'name': val?如何提取“名称”的第一个字段:val?

way #1方式#1

df_movies.withColumn
    ("genres_extract",regexp_extract(col("genres"),
    """ 'name': (\w+)""",1)).show(false)

way #2方式#2

df_movies.withColumn
("genres_extract",regexp_extract(col("genres"),
"""[{'id':\s\d,\s 'name':\s(\w+)""",1))

Excepted: Action例外:行动

You can use get_json_object function:您可以使用get_json_object function:

  Seq("""[{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}, {"id": 37, "name": "Western"}]""")
    .toDF("genres")
    .withColumn("genres_extract", get_json_object(col("genres"), "$[0].name" ))
    .show()


+--------------------+--------------+
|              genres|genres_extract|
+--------------------+--------------+
|[{"id": 28, "name...|        Action|
+--------------------+--------------+

Another possibility is using the from_json function together with a self defined schema.另一种可能性是将from_json function 与自定义模式一起使用。 This allows you to "unwrap" the json structure into a dataframe with all of the data in there, so that you can use it however you want!这允许您将 json 结构“解包”为 dataframe,其中包含所有数据,以便您可以随心所欲地使用它!

Something like the following:类似于以下内容:

import org.apache.spark.sql.types._

Seq("""[{"id": 28, "name": "Action"}, {"id": 12, "name": "Adventure"}, {"id": 37, "name": "Western"}]""")
  .toDF("genres")


// Creating the necessary schema for the from_json function
val moviesSchema = ArrayType(
  new StructType()
    .add("id", StringType)
    .add("name", StringType)
  )

// Parsing the json string into our schema, exploding the column to make one row
// per json object in the array and then selecting the wanted columns,
// unwrapping the parsedActions column into separate columns
val parsedDf = df
  .withColumn("parsedMovies", explode(from_json(col("genres"), moviesSchema)))
  .select("parsedMovies.*")

parsedDf.show(false)
+---+---------+                                                                                                                                                                                                                                                                 
| id|     name|                                                                                                                                                                                                                                                                 
+---+---------+                                                                                                                                                                                                                                                                 
| 28|   Action|                                                                                                                                                                                                                                                                 
| 12|Adventure|                                                                                                                                                                                                                                                                 
| 37|  Western|                                                                                                                                                                                                                                                                 
+---+---------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM