当前位置:网站首页>Spark DF adds a column
Spark DF adds a column
2022-07-06 00:28:00 【The south wind knows what I mean】
List of articles
- Method 1 : utilize createDataFrame Method , The process of adding new columns is included in building rdd and schema in
- Method 2 : utilize withColumn Method , The process of adding new columns is included in udf Function
- Method 3 : utilize SQL Code , The process of adding new columns is written directly to SQL In the code
- Method four : The above three are to add a judged column , If you want to add a unique sequence number , have access to monotonically_increasing_id
Method 1 : utilize createDataFrame Method , The process of adding new columns is included in building rdd and schema in
val trdd = input.select(targetColumns).rdd.map(x=>{
if (x.get(0).toString().toDouble > critValueR || x.get(0).toString().toDouble < critValueL)
Row(x.get(0).toString().toDouble,"F")
else Row(x.get(0).toString().toDouble,"T")
})
val schema = input.select(targetColumns).schema.add("flag", StringType, true)
val sample3 = ss.createDataFrame(trdd, schema).distinct().withColumnRenamed(targetColumns, "idx")
Method 2 : utilize withColumn Method , The process of adding new columns is included in udf Function
val code :(Int => String) = (arg: Int) => {
if (arg > critValueR || arg < critValueL) "F" else "T"}
val addCol = udf(code)
val sample3 = input.select(targetColumns).withColumn("flag", addCol(input(targetColumns)))
.withColumnRenamed(targetColumns, "idx")
Method 3 : utilize SQL Code , The process of adding new columns is written directly to SQL In the code
input.select(targetColumns).createOrReplaceTempView("tmp")
val sample3 = ss.sqlContext.sql("select distinct "+targetColname+
" as idx,case when "+targetColname+">"+critValueR+" then 'F'"+
" when "+targetColname+"<"+critValueL+" then 'F' else 'T' end as flag from tmp")
Method four : The above three are to add a judged column , If you want to add a unique sequence number , have access to monotonically_increasing_id
// Add sequence number column add a column method 4
import org.apache.spark.sql.functions.monotonically_increasing_id
val inputnew = input.withColumn("idx", monotonically_increasing_id)
边栏推荐
- Browser local storage
- Pointer pointer array, array pointer
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
- 【DesignMode】装饰者模式(Decorator pattern)
- Spark获取DataFrame中列的方式--col,$,column,apply
- MySQL存储引擎
- Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
- Set data real-time update during MDK debug
- Priority queue (heap)
- 2022-02-13 work record -- PHP parsing rich text
猜你喜欢
Mysql - CRUD
剖面测量之提取剖面数据
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Transport layer protocol ----- UDP protocol
wx. Getlocation (object object) application method, latest version
Atcoder beginer contest 254 [VP record]
Browser local storage
FFmpeg学习——核心模块
AtCoder Beginner Contest 258【比赛记录】
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
随机推荐
Basic introduction and source code analysis of webrtc threads
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
LeetCode 6006. Take out the least number of magic beans
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
FFT learning notes (I think it is detailed)
小程序技术优势与产业互联网相结合的分析
An understanding of & array names
Configuring OSPF load sharing for Huawei devices
QT -- thread
【线上小工具】开发过程中会用到的线上小工具合集
Notepad + + regular expression replace String
Configuring OSPF GR features for Huawei devices
[designmode] adapter pattern
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
如何制作自己的机器人
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
Key structure of ffmpeg -- AVCodecContext
Search (DFS and BFS)
[designmode] composite mode
时间戳的拓展及应用实例