DataFrame创建


DataFrame/DataSet 创建

  • 读文件接口
import org.apache.spark.sql.SparkSession
val spark = SparkSession
  .builder()
  .appName("Spark SQL basic example")
  .config("spark.some.config.option", "some-value")
  .getOrCreate()
// For implicit conversions like converting RDDs to DataFrames
import spark.implicits._
val df=spark.read.xxx

DataFrame/DataSet 读取数据源文档

spark.read 返回 DataFrameReader

spark.readStream 返回 DataStreamReader

后续读文件操作雷同,可以参考作者的 Structured Streaming 文章

  • RDD 转换成 DataFrame/DataSet
    • 方式1:已知元数据
      val peopleDF = spark.sparkContext
        .textFile("examples/src/main/resources/people.txt")
        .map(_.split(","))
        .map(attributes => Person(attributes(0), attributes(1).trim.toInt))
        .toDF()/toDS
      
    • 方式2:未知元数据
      val schemaString = "name age"
      // Generate the schema based on the string of schema
      val fields = schemaString.split(" ")
        .map(fieldName => StructField(fieldName, StringType, nullable = true))
      val schema = StructType(fields)
      // Convert records of the RDD (people) to Rows
      val rowRDD = peopleRDD
        .map(_.split(","))
        .map(attributes => Row(attributes(0), attributes(1).trim))
      
优质内容筛选与推荐>>
1、@Scope("prototype")创建的对象为什么会一样?
2、第三次作业
3、日志分析 第四章 安装filebeat
4、SSM框架随笔
5、tensorflow实现简单的softmat分类器


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn