網站首頁 編程語言 正文
import org.apache.avro.generic.GenericData.StringType
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{IntegerType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.spark_project.dmg.pmml.True
object TestDataFrame2 {
def main(args: Array[String]): Unit = {
val conf=new SparkConf().setAppName("TestDataFrame2").setMaster("local")
val sc=new SparkContext(conf)
val SQLContext=new SQLContext(sc)
//將本地的數據讀入RDD
val fileRDD=sc.textFile("D:\\Users\\shashahu\\Desktop\\work\\spark-2.4.4\\examples\\src\\main\\resources\\people.txt")
//將RDD數據映射成Row,需要import org.apache.spark.sql.row
val rowRDD:RDD[Row]=fileRDD.map(line=>{
val fields=line.split(",")
Row(fields(0),fields(1).trim.toInt)
})
//創建StructType來定義結構
val structType: StructType = StructType(
//字段名,字段類型,是否可以為空
// StructField("name", StringType, true) ::
StructField("age", IntegerType, true) :: Nil
)
/**
* rows: java.util.List[Row],
* schema: StructType
* */
val df: DataFrame = SQLContext.createDataFrame(rowRDD,structType)
df.createOrReplaceTempView("people")
SQLContext.sql("select * from people").show()
}
}
使用結構體來進行創建表的相關語句。
原文鏈接:https://blog.csdn.net/qq_35561207/article/details/103037917
相關推薦
- 2022-11-07 PostgreSQL常用優化技巧示例介紹_PostgreSQL
- 2022-11-24 Android開發使用Message對象分發必備知識點詳解_Android
- 2022-04-16 WPF框架Prism中區域Region用法介紹_實用技巧
- 2023-02-14 C++關于字符的接收與輸出操作示例_C 語言
- 2022-01-27 laravel6.2多用戶認證分不同用戶表進行驗證登錄認證
- 2022-10-11 python格式化字符串的實戰教程(使用占位符、format方法)_python
- 2022-06-17 flutter監聽app進入前后臺狀態的實現_Android
- 2022-08-03 python如何實現排序,并標上序號_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支