zoukankan      html  css  js  c++  java
  • spark2.0 DataSet操作的一些问题记录

    随着新版本的Spark已经逐渐稳定,最近拟将原有框架升级到spark 2.0。还是比较兴奋的,特别是SQL的速度真的快了许多。。

    然而,在其中一个操作时却卡住了。主要是dataframe.map操作,这个之前在spark 1.X是可以运行的,然而在spark 2.0上却无法通过。。

    看了提醒的问题,主要是:

    Error:(18, 54) Unable to find encoder for type stored in a Dataset. Primitive types (Int, String, etc) and Product types (case classes) are supported by importing spark.implicits._ Support for serializing other types will be added in future releases.
    val dsAvgTemp = ds.filter(d => {d.temp > 20}).map(d => (d.temp,d.humidity,d.cca3)).groupBy($"_3").avg()

    针对这个问题,网上所得获取的资料还真不多。不过想着肯定是dataset统一了datframe与rdd之后就出现了新的要求。

    经过查看spark官方文档,对spark有了一条这样的描述。

    Dataset is Spark SQL’s strongly-typed API for working with structured data, i.e. records with a known schema.

    Datasets are lazy and structured query expressions are only triggered when an action is invoked. Internally, aDataset represents a logical plan that describes the computation query required to produce the data (for a givenSpark SQL session).

    A Dataset is a result of executing a query expression against data storage like files, Hive tables or JDBC databases. The structured query expression can be described by a SQL query, a Column-based SQL expression or a Scala/Java lambda function. And that is why Dataset operations are available in three variants.

    从这可以看出,要想对dataset进行操作,需要进行相应的encode操作。特别是官网给的例子

    // No pre-defined encoders for Dataset[Map[K,V]], define explicitly
    implicit val mapEncoder = org.apache.spark.sql.Encoders.kryo[Map[String, Any]]
    // Primitive types and case classes can be also defined as
    // implicit val stringIntMapEncoder: Encoder[Map[String, Any]] = ExpressionEncoder()
    
    // row.getValuesMap[T] retrieves multiple columns at once into a Map[String, T]
    teenagersDF.map(teenager => teenager.getValuesMap[Any](List("name", "age"))).collect()
    // Array(Map("name" -> "Justin", "age" -> 19))

    从这看出,要进行map操作,要先定义一个Encoder。

     这就增加了系统升级繁重的工作量了。为了更简单一些,幸运的dataset也提供了转化RDD的操作。因此只需要将之前dataframe.map

    在中间修改为:dataframe.rdd.map即可。

    本文来自:http://blog.csdn.net/sparkexpert/article/details/52871000

  • 相关阅读:
    20169219 实验二 Java面向对象程序设计
    20169219 2016-2017-2 《移动平台开发》第七周作业
    20169219 实验一 Java开发环境的熟悉(Linux + IDEA)
    20169219 2016-2017-2《网络攻防》第七周作业
    文件下载之ServletOutputStream
    DOM对象之window
    webservice接口的开发和调用
    utf-8与unicode是什么关系
    hibernate注解之@Onetomany、@Manytoone、@JoinColumn
    ajax不执行success的问题
  • 原文地址:https://www.cnblogs.com/ios1988/p/6689904.html
Copyright © 2011-2022 走看看