1. 程式人生 > >解決 Error:Unable to find encoder for type stored in a Dataset

解決 Error:Unable to find encoder for type stored in a Dataset

Error: 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.

錯誤:無法找到儲存在資料集中的型別的編碼器。原始型別(Int,String等)和產品型別(case類)由匯入spark.implicits支援。支援對其他型別的序列化,將在以後的版本中新增。

問題解決如下:

在開發過程中,我想將已經儲存在HDFS上的文字檔案儲存成CSV格式的檔案。

以前檔案格式如下(這裡使用自己造的假資料)每一列所對應的是 id,name,no,sp,ep
3303龍順 JD8赤壁 湛江
5426程凡 G58龍巖 苗栗

我想處理成下面的格式,因為下面的格式是CSV預設格式
id,name,no,sp,ep
1309,項敬,BKZ,韶關,湖北
3507,寧風晨,KY7,河源,資陽

處理程式碼如下

def main(args: Array[String]) {
	val sparkSession = SparkSession.builder().appName("Spark shell").getOrCreate()
	//檔案路徑
	val path = "hdfs://master:9000/TestData/aviation9"
	//儲存路徑
	val savePath = "hdfs://master:9000/TestData/aviation10/"
	val file = sparkSession.read.textFile(path)
	//處理資料,拆分
	val rd = file.map(line => {
	  val arr = line.split("\t")
	  (arr(0), arr(1), arr(2), arr(3), arr(4))
	})
	//給DataFrame新增表頭,
	val res = rd.toDF("id", "name", "no", "sp", "ep")
	//儲存有表的檔案
	res.repartition(1).write.mode(SaveMode.Append).format("csv").option("header", true).save(savePath)
}

準備打包執行,在打包的時候總是出現上面的錯誤

無法找到儲存在資料集中的型別的編碼器。
原始型別(Int,String等)和產品型別(case類)由匯入spark.implicits支援。
支援對其他型別的序列化,將在以後的版本中新增。
所以需要我們自己在Dataset中新增元組這樣的編碼

解決辦法:處理資料之間新增下面一行程式碼
implicit val matchError = org.apache.spark.sql.Encoders.tuple( Encoders.STRING, Encoders.STRING, Encoders.STRING, Encoders.STRING, Encoders.STRING)

最後的全部程式碼是

def main(args: Array[String]) {
	val sparkSession = SparkSession.builder().appName("Spark shell").getOrCreate()
	//檔案路徑
	val path = "hdfs://master:9000/TestData/aviation9"
	//儲存路徑
	val savePath = "hdfs://master:9000/TestData/aviation10/"
	val file = sparkSession.read.textFile(path)
	implicit val matchError = org.apache.spark.sql.Encoders.tuple( Encoders.STRING, Encoders.STRING, Encoders.STRING, Encoders.STRING, Encoders.STRING)
	//處理資料,拆分
	val rd = file.map(line => {
	  val arr = line.split("\t")
	  (arr(0), arr(1), arr(2), arr(3), arr(4))
	})
	//給DataFrame新增表頭,
	val res = rd.toDF("id", "name", "no", "sp", "ep")
	//儲存有表的檔案
	res.repartition(1).write.mode(SaveMode.Append).format("csv").option("header", true).save(savePath)
}
在處理這個錯誤中,發現編碼器中預設的tuple最多是5個元素,那如果我們有很多列資料該怎麼處理呢?
比如現在的資料是下面這樣的,多了一列time
5426程凡 G562013-12-24 17:26:23龍巖苗栗
4413相承 TV72014-04-09 20:44:25北票開原

如果在用下面的程式碼又出現同樣的錯誤
implicit val matchError = org.apache.spark.sql.Encoders.tuple( Encoders.STRING, Encoders.STRING, Encoders.STRING, Encoders.STRING, Encoders.STRING)

因為在Encoders中最多支援5個元素的tuple,我們需要將DataSet轉成處理成Row型別的,最後轉成RDD用資料和表頭建立一個DataFrame
def main(args: Array[String]) {
    val sparkSession = SparkSession.builder().appName("Spark shell").getOrCreate()
    val fields = "id,name,no,time,sp,ep"
    val path = "hdfs://master:9000/TestData/aviation9"
    val savePath = "hdfs://master:9000/TestData/aviation10/"
    
    val file: Dataset[String] = sparkSession.read.textFile(path)
    
    implicit val matchError = org.apache.spark.sql.Encoders.kryo[Row]
    //處理成Row
    val ds = file.map(x => {
      val arr = x.split("\t")
      Row.fromSeq(arr.toSeq)
    })
    //建立表頭
    val field_array = fields.split(",")
    val schema = StructType(field_array.map(fieldName => StructField(fieldName, StringType, true)))
    //建立DataFrame
    val df = sparkSession.createDataFrame(ds.rdd, schema)
    df.repartition(1).write.mode(SaveMode.Append).format("csv").option("header", true).save(savePath)
}




相關推薦

解決 Error:Unable to find encoder for type stored in a Dataset

Error: Unable to find encoder for type stored in a Dataset.  Primitive types (Int, String, etc) and Product t

Spark 2.0 DataFrame map操作中Unable to find encoder for type stored in a Dataset.問題的分析與解決

隨著新版本的spark已經逐漸穩定,最近擬將原有框架升級到spark 2.0。還是比較興奮的,特別是SQL的速度真的快了許多。。 然而,在其中一個操作時卻卡住了。主要是dataframe.map操作,這個之前在spark 1.X是可以執行的,然而在spark 2.0上卻無

Android Studio 3.1.4,gradle 4.4解決Error:Unable to resolve dependency for ':@debug/compileClasspath'問題

    最近把Android studio升級到3.1.4,可是新建一個空專案在build的時候都出現問題,本來不是很重視,把不需要的直接注掉即可,但是因為匯入公司的一個專案來編譯,結果還是報了同樣的錯(引入的依賴庫出現了問題),最終在網上找了很多方法,最終解決了,記錄下過程

Android Studio 解決 Error:Unable to find method 'org.gradle.api.tasks.TaskInputs.file(Ljava/lang/Object;)Lorg/gradle/api/tasks

Gradle編譯異常資訊 Error:Unable to find method 'org.gradle.api.tasks.TaskInputs.file(Ljava/lang/Object;)Lorg/gradle/api/tasks/TaskInputFilePropertyB

徹底解決 error: Unable to find vcvarsall.bat

轉自:http://blog.csdn.net/secretx/article/details/17472107 1.windows上做Python開發,搭環境還真不比Linux容易。error: Unable to find vcvarsall.bat這個錯誤眼熟吧? 凡

轉載:徹底解決 error: Unable to find vcvarsall.bat

轉自:http://blog.csdn.net/secretx/article/details/17472107 侵權刪 1.windows上做Python開發,搭環境還真不比Linux容易。error: Unable to find vcvarsall.bat這

xcrun: error: unable to find utility "PackageApplication", not a developer tool or in PATH

view chmod baidu aid log nts pac mod ica Xcode升級到8.3後 用命令進行打包 提示下面這個錯誤 xcrun: error: unable to find utility "PackageApplication", not a

[vscode react-native] xcrun: error: unable to find utility "instruments", not a developer tool or in

執行環境:Mac + vscode + xcode IOS ReactNative執行的時候出現這個錯誤 使用vscode執行reactnative 專案時報錯:xcrun: error: unable to find utility "instruments", not a develop

解決Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List

解決新增Butterknife8.8.1依賴後出錯的問題 1.在根目錄下的build.gradle中的dependencies新增如下: 這裡注意是8.4.0的版本!! classpath 'com.jakewharton:butterknife-gradle-pl

studio3.0關於Error:Unable to resolve dependency for ':@debug/compileClasspath'的解決方案

如果你在老專案遷移到studio3.0時遇到了同樣的問題,希望可以幫到你。 —-首先出現這個問題肯定是studio升級到3.0和Gradle升級到4.1導致的,百度一波後,大致知道是怎麼造成的了,

Python3 pip 解決問題: error: Unable to find vcvarsall.bat

當我給 python3.5 安裝 第三方庫 charset 時:pip install charset,出現了錯誤: D:\WorkSpace\python_ws\python-large-web-crawler\firstdemo>pip

error: Unable to find vcvarsall.bat

error: Unable to find vcvarsall.bat faster_rcnn進行訓練編譯時,報這個錯誤 faster_rcnn 2015, 專案地址:https://github.com/dBeker/Faster-RCNN-TensorFlow-Python3.5

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityU

在新建asp.net core 應用後, 添加了自定義的ApplicationDbContext 和ApplicationUser ,並添加了Identity認證後, 會出現 InvalidOperationException: Unable to resolve service for type 'Micr

無需安裝VS,一行命令解決"Unable to find vcvarsall.bat"

執行環境 Windows 10 (64-bit) Python 3.7 問題描述 在執行帶Cython模組的py檔案時,有可能輸出如下報錯資訊: error: Unable to find vcvarsall.bat 在網上查找了大量資料,發現大多數

整合butterknife出現 Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()

錯誤為: Error:Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'. Possible causes for this unexpected erro

Android Studio 3.0.1 gradle編譯報錯 Error : unable to resolve dependency for <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0c1

在app目錄下build.gradle中引用了第三方類庫,gradle編譯時不斷報錯,無法resolve第三方類庫,或者無法download第三方類庫dependencies { implementation fileTree(include: ['*.jar'],

windows install cython error: "unable to find vcvarsall.bat"

cython作為python的超集,可以同時編譯python和C的程式碼,檔案字尾名為pyx,編譯後匯出pyd檔案(windows環境)或者是so檔案(類unix環境)。具體流程主要為: 1. 將pyx的程式碼編譯成.C檔案; 2. 利用系統

Android 專案報錯 Error:Unable to find optional library: org.apache.http.legacy

Eclipse 專案移植到AndroidStduio 後 執行報錯Error:Unable to find optional library: org.apache.http.legacy Android 6.0版本已經已經基本將Apahce Http Client 移

Android stuido 錯誤:Error:Unable to find optional library: org.apache.http.legacy

今天在匯入新專案的時候出現了一個從來都沒有見過的錯誤,讓我鬱悶了很久,在網上也找了很多一樣錯誤解決方法,但是最終也是沒有解決,不能說網上的解決方法是錯誤,只能說網上的解決辦法是最基礎的,有時候不能夠解決問題,錯誤的截圖如下: 網上的解決辦法是: 1.   API23

RabbitMQ_____error rabbitMQ:Error: unable to perform an operation on node '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail

Error: unable to perform an operation on node '[email protected]' 將登入身份改為指定帳戶,重啟RabbitMq服務 Error: unable to perform an operation on node '