当前位置:网站首页>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)
边栏推荐
- 【DesignMode】组合模式(composite mode)
- Configuring OSPF GR features for Huawei devices
- Spark SQL空值Null,NaN判断和处理
- Room cannot create an SQLite connection to verify the queries
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
- 小程序技术优势与产业互联网相结合的分析
- [designmode] Decorator Pattern
- OS i/o devices and device controllers
- Go learning - dependency injection
猜你喜欢
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
Classic CTF topic about FTP protocol
Problems and solutions of converting date into specified string in date class
Notepad++ regular expression replacement string
How to use the flutter framework to develop and run small programs
Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
Huawei equipment is configured with OSPF and BFD linkage
Spark SQL空值Null,NaN判断和处理
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
随机推荐
Atcoder beginer contest 254 [VP record]
DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
Extension and application of timestamp
Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
STM32按键消抖——入门状态机思维
Leetcode:20220213 week race (less bugs, top 10% 555)
About the slmgr command
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
数据分析思维分析方法和业务知识——分析方法(二)
Key structure of ffmpeg - avformatcontext
Notepad++ regular expression replacement string
Opencv classic 100 questions
Common API classes and exception systems
LeetCode 1189. Maximum number of "balloons"
常用API类及异常体系
7.5模拟赛总结
LeetCode 6004. Get operands of 0
FFMPEG关键结构体——AVFormatContext
MySQL storage engine
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)