文章目录1 官方说明2 使用时涉及到的的包3 df.col , df['col'] , col('col')之间的区别3.1 df.col3.2 df['col']3.3 col('col')
1 官方说明
df("columnName") // On a specific
DataFrame
.
col("columnName") // A generic column no yet associated with a
DataFrame
.
col("colum.
一、datafram描述
DataFrame
是一种不可变的分布式数据集,这种数据集被组织成指定的
列
,类似于关系数据库中的表。数据集的每一
列
都带有名称和类型,对于数据的内部结构又很强的描述性。
二、datafram特点
1、支持 KB 级到 PB 级得到数据处理
2、支持多种数据格式和存储系统
3、通过
Spark
SQL Catalyst 优化器可以进行...
Spark
SQL中的
DataFrame
类似于一张关系型数据表。在关系型数据库中对单表或进行的查询操作,在
DataFrame
中都可以通过调用其API接口来实现。可以参考,Scala提供的
DataFrame
API。
本文中的代码基于
Spark
-1.6.2的文档实现。
一、
DataFrame
对象的生成
Spark
-SQL可以以其他RDD对象、parquet文...
很久没有更新博客了,因为最近工作确实也很忙,不过忙碌的工作也让我收获了很多新的知识,趁着忙碌的间隙,来记录一下自己的成长。
这次的场景是:需要单独取出
DataFrame
中的某一
列
的所有值供Java程序使用。
下面的demo包含两个例子:
1.column value Type -> String, 即 row->String ,需要用到mkString这个方法
2.column value Type -> WrappedArray, 即Seq(String) ->String, 需要
import
spark
.implicits._
import org.apache.
spark
.sql.functions._
import org.apache.
spark
.sql.Column
官方说明:
df("columnName") // On a specific
DataFrame
.
col("columnName") // A generic column no yet associated with
我之前写的代码大体功能如下:
val map= new mutable.HashMap[String, String]
val df:
DataFrame
= DbDataApi.requestColMetaInfo(dataId)
df.foreach(row =>{
map.put(row.getAs[String](fieldName = "colName"),row.getAs[String](fieldName = "col1"))
运行后发现
df = df.withColumnRenamed("old_col1", "new_col1") \
.withColumnRenamed("old_col2", "new_col2") \
.withColumnRenamed("old_col3", "new_col3")
其中,df是一个
Spark
DataFrame
对象,withColumnRenamed()方法用于重命名
列
名,第一个参数是旧
列
名,第二个参数是新
列
名。通过连续调用withColumnRenamed()方法,可以同时重命名多
列
。
### 回答2:
Spark
DataFrame
是一个分布式的数据结构,采用类 SQL 语言的API进行操作,可以进行各种数据分析处理,如数据筛选,数据排序,数据过滤等。在对
Spark
DataFrame
进行操作时,经常需要进行
列
重命名。可以使用 withColumnRenamed 方法来重命名单
列
。但是,当需要同时重命名多
列
时,该方法就不再适用,需要使用到 selectExpr 方法。
使用 selectExpr 方法可以实现同时给多个
列
重命名,语法结构如下:
df.selectExpr("col1 as newcol1", "col2 as newcol2", "col3 as newcol3", ...)
其中,col1、col2、col3 为原始的
列
名,newcol1、newcol2、newcol3 为修改后的
列
名。
例如,有如下的
Spark
DataFrame
。
+---+---+------+-----+
| id|age|gender|score|
+---+---+------+-----+
| 1| 18| M | 85|
| 2| 20| F | 92|
| 3| 22| M | 88|
+---+---+------+-----+
同时将 id、age、gender
列
重命名为 student_id、student_age、student_gender,则可以使用如下的 selectExpr 方法:
df.selectExpr("id as student_id", "age as student_age", "gender as student_gender", "score")
对于重命名后的
DataFrame
,输出结果如下:
+----------+-----------+---------------+-----+
|student_id|student_age|student_gender|score|
+----------+-----------+---------------+-----+
| 1| 18| M | 85|
| 2| 20| F | 92|
| 3| 22| M | 88|
+----------+-----------+---------------+-----+
### 回答3:
在
Spark
DataFrame
中,我们可以使用withColumnRenamed()方法来对单个
列
进行重命名。但是如果想在一次操作中重命名多个
列
,我们应该如何做呢?
答案是使用select()方法,并且将每个
列
都传递给一个别名。例如,假设我们有一个名为df的
DataFrame
,它有三
列
:col1、col2和col3。我们想要将它们重命名为newCol1、newCol2和newCol3。我们可以采用以下代码:
val newDf = df.select(col("col1").alias("newCol1"), col("col2").alias("newCol2"), col("col3").alias("newCol3"))
这里,我们首先使用col()方法
获取
每个
列
的引用,并为它们指定新的别名。然后,我们使用select()方法选择这三
列
并将它们重命名为新的
列
名。返回值是包含重命名
列
的新
DataFrame
newDf。
当然,如果我们有很多
列
需要重命名,手动指定每个别名会很麻烦。在这种情况下,我们可以使用
Spark
的for循环和
列
表推导式来创建别名
列
表,然后将它们传递给select()方法。例如,我们可以采用以下代码:
val oldColumns = Seq("col1", "col2", "col3")
val newColumns = Seq("newCol1", "newCol2", "newCol3")
val selectExprs = for(i <- 0 until oldColumns.length) yield col(oldColumns(i)).alias(newColumns(i))
val newDf = df.select(selectExprs:_*)
首先,我们定义两个
列
表:oldColumns包含所有要重命名的
列
,newColumns包含这些
列
的新名称。然后我们使用
列
表推导式和for循环来创建一个包含所有别名的表达式
列
表。最后,我们将表达式
列
表作为参数传递给select()方法,并使用_*语法将其展开为一系
列
表达式。这样做的结果是与前一个示例中相同的newDf
DataFrame
,即包含重命名
列
的新
DataFrame
。
总之,重命名
Spark
DataFrame
中的多
列
需要使用select()方法和alias()方法。我们可以手动指定每个别名,或者使用for循环和
列
表推导式来自动创建别名
列
表。无论哪种方法,最终结果都是一个包含重命名
列
的新
DataFrame
。
Exception in thread "main" org.apache.spark.sql.AnalysisException: "to_account_date" is not a numeri
基层码工:
Exception in thread "main" org.apache.spark.sql.AnalysisException: "to_account_date" is not a numeri
qq_42915509:
mysql 定时器用法总结、实例、时间实例
不放弃的梦:
Apache Phoenix自定义函数(UDF)实践
YELLOES: