Skip to content

Instantly share code, notes, and snippets.

@YordanGeorgiev
Last active February 17, 2018 10:29
Show Gist options
  • Save YordanGeorgiev/b230f2847bae294a6b80e58688a11dc6 to your computer and use it in GitHub Desktop.
Save YordanGeorgiev/b230f2847bae294a6b80e58688a11dc6 to your computer and use it in GitHub Desktop.
[create dataframe with schema] how-to create a dataframe obj with schema in scala spark #scala #spark #dataframe
val spark = SparkSession.builder().getOrCreate()
import spark.implicits._
val df = spark
.createDataFrame(
spark.sparkContext.parallelize(
Seq(
Row(
Map(("key1","val1") -> 1)
),
Row(
Map(("key1","val2") -> 2)
)
)),
StructType(
new StructType().add("server_rank_map", MapType(
new StructType()
.add("key_col", StringType , false)
.add("val_col" , StringType , false)
, IntegerType
) //eof MapType
) //eof add
) //eof schema
) //eof createDataFrame
df.printSchema()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment