在AWS Glue中,数据类别包含以下几种类型:
示例代码:
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
# 定义结构化数据的列和类型
schema = StructType([
StructField("name", StringType(), True),
StructField("age", IntegerType(), True),
])
# 使用定义的结构化数据类型创建数据帧
df = spark.createDataFrame([( "John", 25), ("Alice", 30)], schema)
df.show()
示例代码:
from pyspark.sql.types import ArrayType, StringType
# 定义数组类型的数据
data = [("John", ["apple", "banana", "orange"]), ("Alice", ["grape", "pineapple"])]
# 定义数组元素的数据类型
schema = StructType([
StructField("name", StringType(), True),
StructField("fruits", ArrayType(StringType()), True),
])
# 使用定义的数组类型创建数据帧
df = spark.createDataFrame(data, schema)
df.show()
示例代码:
from pyspark.sql.types import MapType, StringType, IntegerType
# 定义键值对类型的数据
data = [("John", {"apple": 5, "banana": 2}), ("Alice", {"orange": 3, "grape": 4})]
# 定义键值对中键和值的数据类型
schema = StructType([
StructField("name", StringType(), True),
StructField("fruits", MapType(StringType(), IntegerType()), True),
])
# 使用定义的键值对类型创建数据帧
df = spark.createDataFrame(data, schema)
df.show()
以上示例代码展示了在AWS Glue中定义和创建不同类型的数据类别。