Spark 初始化对象

Spark 1+ 和 Spark 2+ 初始化SparkContext有所区别,现列出Spark 1.5.1 和 Spark 2+中初始化sc方式:

1. Spark 2+

先创建一个SparkSession对象:

通过config方法配置自定义设置,可以写多个config设置

val spark = SparkSession.builder() // Creates a [[SparkSession.Builder]] for constructing a [[SparkSession]].  
  .appName("Word Count") // Sets a name for the application, which will be shown in the Spark web UI. If no application name is set, a randomly generated name will be used.
  .config("spark.some.config.option", "some-value") //Sets a config option. Options set using this method are automatically propagated to both `SparkConf` and SparkSession's own configuration.
  .enableHiveSupport() // 启用HiveContext
  .config("spark.sql.parquet.writeLegacyFormat", true) //use the same convention as Hive for writing the Parquet data
  .getOrCreate()  //Gets an existing [[SparkSession]] or, if there is no existing one, creates a new  one based on the options set in this builder.
  

val sc = spark.sparkContext

import spark.implicits._ 
spark.sql("select 123")  # 调用sql

注意:

Spark sql Insert 数据时可能报错:

Failed with exception java.io.IOException:org.apache.parquet.io.ParquetDecodingException: Can not read value at 1 in block 0 in file hdfs

原因及解决:

config("spark.sql.parquet.writeLegacyFormat", true) 
Root Cause:
This issue is caused because of different parquet conventions used in Hive and Spark. In Hive, the decimal datatype is represented as fixed bytes (INT 32). In Spark 1.4 or later the default convention is to use the Standard Parquet representation for decimal data type. As per the Standard Parquet representation based on the precision of the column datatype, the underlying representation changes.
eg: DECIMAL can be used to annotate the following types: int32: for 1 <= precision <= 9 int64: for 1 <= precision <= 18; precision < 10 will produce a warning

Hence this issue happens only with the usage of datatypes which have different representations in the different Parquet conventions. If the datatype is DECIMAL (10,3), both the conventions represent it as INT32, hence we won't face an issue. If you are not aware of the internal representation of the datatypes it is safe to use the same convention used for writing while reading. With Hive, you do not have the flexibility to choose the Parquet convention. But with Spark, you do.

2. Spark 1.5.0

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值