示例JAVA代码:

import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.split;
import static org.apache.spark.sql.functions.explode; import java.util.ArrayList;
import java.util.List; import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession; public class TestSparkSqlSplit {
public static void main(String[] args){
SparkSession sparkSession =SparkSession.builder().appName("test").master("local[*]").getOrCreate();
List<MyEntity> items=new ArrayList<MyEntity>();
MyEntity myEntity=new MyEntity();
myEntity.setId("scene_id1,scene_name1;scene_id2,scene_name2|id1");
myEntity.setName("name");
myEntity.setFields("other");
items.add(myEntity); sparkSession.createDataFrame(items, MyEntity.class).createOrReplaceTempView("test"); Dataset<Row> rows=sparkSession.sql("select * from test");
rows = rows.withColumn("id", explode(split(split(col("id"), "\\|").getItem(), ";"))); rows=rows.withColumn("id1",split(rows.col("id"),",").getItem())
.withColumn("name1",split(rows.col("id"),",").getItem()); rows=rows.withColumn("id",rows.col("id1"))
.withColumn("name",rows.col("name1")); rows=rows.drop("id1","name1"); rows.show(); sparkSession.stop();
}
}

MyEntity.java

import java.io.Serializable;

public class MyEntity implements Serializable{
private String id;
private String name;
private String fields;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFields() {
return fields;
}
public void setFields(String fields) {
this.fields = fields;
} }

打印结果:

// :: INFO codegen.CodeGenerator: Code generated in 36.359731 ms
+------+---------+-----------+
|fields| id| name|
+------+---------+-----------+
| other|scene_id1|scene_name1|
| other|scene_id2|scene_name2|
+------+---------+-----------+

Scala实现:

[dx@CDH- ~]$ spark-shell2
-bash: spark-shell2: command not found
[boco@CDH- ~]$ spark2-shell
Setting default log level to "WARN".
...
Spark context available as 'sc' (master = yarn, app id = application_1552012317155_0189).
Spark session available as 'spark'.
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/___/ .__/\_,_/_/ /_/\_\ version 2.2..cloudera1
/_/ Using Scala version 2.11. (Java HotSpot(TM) -Bit Server VM, Java 1.8.0_171)
Type in expressions to have them evaluated.
Type :help for more information. scala>
scala> val df = Seq(
| (, "scene_id1,scene_name1;scene_id2,scene_name2",""),
| (, "scene_id1,scene_name1;scene_id2,scene_name2;scene_id3,scene_name3",""),
| (, "scene_id4,scene_name4;scene_id2,scene_name2",""),
| (, "scene_id6,scene_name6;scene_id5,scene_name5","")
| ).toDF("id", "int_id","name");
df: org.apache.spark.sql.DataFrame = [id: int, int_id: string ... more field]
scala> df.show;
+---+--------------------+----+
| id| int_id|name|
+---+--------------------+----+
| |scene_id1,scene_n...| |
| |scene_id1,scene_n...| |
| |scene_id4,scene_n...| |
| |scene_id6,scene_n...| |
+---+--------------------+----+
scala> df.withColumn("int_id", explode(split(col("int_id"), ";")));
res1: org.apache.spark.sql.DataFrame = [id: int, int_id: string ... more field]
scala> res1.show();
+---+--------------------+----+
| id| int_id|name|
+---+--------------------+----+
| |scene_id1,scene_n...| |
| |scene_id2,scene_n...| |
| |scene_id1,scene_n...| |
| |scene_id2,scene_n...| |
| |scene_id3,scene_n...| |
| |scene_id4,scene_n...| |
| |scene_id2,scene_n...| |
| |scene_id6,scene_n...| |
| |scene_id5,scene_n...| |
+---+--------------------+----+
scala> res1.withColumn("int_id", split(col("int_id"), ",")()).withColumn("name", split(col("int_id"), ",")());
res5: org.apache.spark.sql.DataFrame = [id: int, int_id: string ... more field]
scala> res5.show
+---+---------+----+
| id| int_id|name|
+---+---------+----+
| |scene_id1|null|
| |scene_id2|null|
| |scene_id1|null|
| |scene_id2|null|
| |scene_id3|null|
| |scene_id4|null|
| |scene_id2|null|
| |scene_id6|null|
| |scene_id5|null|
+---+---------+----+
scala> res1.withColumn("name", split(col("int_id"), ",")()).withColumn("int_id", split(col("int_id"), ",")());
res7: org.apache.spark.sql.DataFrame = [id: int, int_id: string ... more field]
scala> res7.show
+---+---------+-----------+
| id| int_id| name|
+---+---------+-----------+
| |scene_id1|scene_name1|
| |scene_id2|scene_name2|
| |scene_id1|scene_name1|
| |scene_id2|scene_name2|
| |scene_id3|scene_name3|
| |scene_id4|scene_name4|
| |scene_id2|scene_name2|
| |scene_id6|scene_name6|
| |scene_id5|scene_name5|
+---+---------+-----------+
scala>

int_id(string类型)为null,会自动转化为空字符串,如果filter中写过滤条件col("int_id").notEqual(null),将会过滤掉所有数据:

// MARK:如果int_id(string类型)为null,会自动转化为空字符串,如果filter中写过滤条件col("int_id").notEqual(null),将会过滤掉所有数据。

scala> val df = Seq(
| (1, null,""),
| (2, "-1",""),
| (3, "scene_id4,scene_name4;scene_id2,scene_name2",""),
| (4, "scene_id6,scene_name6;scene_id5,scene_name5","")
| ).toDF("id", "int_id","name");
df: org.apache.spark.sql.DataFrame = [id: int, int_id: string ... 1 more field] scala> df.filter(col("int_id").notEqual(null).and(col("int_id").notEqual("-1")));
res5: org.apache.spark.sql.Dataset[org.apache.spark.sql.Row] = [id: int, int_id: string ... 1 more field] scala> res5.show;
+---+------+----+
| id|int_id|name|
+---+------+----+
+---+------+----+ scala> df.filter(col("int_id").notEqual("").and(col("int_id").notEqual("-1")));
res7: org.apache.spark.sql.Dataset[org.apache.spark.sql.Row] = [id: int, int_id: string ... 1 more field] scala> res7.show;
+---+--------------------+----+
| id| int_id|name|
+---+--------------------+----+
| 3|scene_id4,scene_n...| |
| 4|scene_id6,scene_n...| |
+---+--------------------+----+

int_id如果不包含列传行的条件,数据不会丢失:

scala> 

scala> val df = Seq(
| (, null,""),
| (, "-1",""),
| (, "scene_id4,scene_name4;scene_id2,scene_name2",""),
| (, "scene_id6,scene_name6;scene_id5,scene_name5","")
| ).toDF("id", "int_id","name");
df: org.apache.spark.sql.DataFrame = [id: int, int_id: string ... more field] scala> scala> df.withColumn("name", split(col("int_id"), ",")()).withColumn("int_id", split(col("int_id"), ",")());
res0: org.apache.spark.sql.DataFrame = [id: int, int_id: string ... more field] scala> res0.show;
+---+---------+--------------------+
| id| int_id| name|
+---+---------+--------------------+
| | null| null|
| | -| null|
| |scene_id4|scene_name4;scene...|
| |scene_id6|scene_name6;scene...|
+---+---------+--------------------+ scala>

Spark:实现行转列的更多相关文章

  1. spark 累加历史 + 统计全部 + 行转列

    spark 累加历史主要用到了窗口函数,而进行全部统计,则需要用到rollup函数 1  应用场景: 1.我们需要统计用户的总使用时长(累加历史) 2.前台展现页面需要对多个维度进行查询,如:产品.地 ...

  2. Spark基于自定义聚合函数实现【列转行、行转列】

    一.分析 Spark提供了非常丰富的算子,可以实现大部分的逻辑处理,例如,要实现行转列,可以用hiveContext中支持的concat_ws(',', collect_set('字段'))实现.但是 ...

  3. Databricks 第11篇:Spark SQL 查询(行转列、列转行、Lateral View、排序)

    本文分享在Azure Databricks中如何实现行转列和列转行. 一,行转列 在分组中,把每个分组中的某一列的数据连接在一起: collect_list:把一个分组中的列合成为数组,数据不去重,格 ...

  4. SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...

  5. T-SQL 实现行转列

    问题: 我正在寻找一种有效的方式将行转换为SQL服务器中的列 例如,通过下表如何构建出预期结果表. Id  Value   ColumnName 1   John    FirstName 2   2 ...

  6. Oracle行转列、列转行的Sql语句总结

    多行转字符串 这个比较简单,用||或concat函数可以实现  SQL Code  12    select concat(id,username) str from app_userselect i ...

  7. sql的行转列(PIVOT)与列转行(UNPIVOT)

    在做数据统计的时候,行转列,列转行是经常碰到的问题.case when方式太麻烦了,而且可扩展性不强,可以使用 PIVOT,UNPIVOT比较快速实现行转列,列转行,而且可扩展性强 一.行转列 1.测 ...

  8. 做图表统计你需要掌握SQL Server 行转列和列转行

    说在前面 做一个数据统计和分析的项目,每天面对着各种数据,经过存储过程从源表计算汇总后需要写入中间结果表以提高数据使用效率,那么此时就需要用到行转列和列转行. 1.列转行 数据经过计算加工后会直接生成 ...

  9. SQL SERVER特殊行转列案列一则

    今天有个同事找我,他说他有个需求,需要进行行转列,但是又跟一般的行转列有些区别,具体需求如下所说,需要将表1的数据转换为表2的显示格式. 我想了一下,给出了一个解决方法,具体如下所示(先给出测试数据) ...

  10. SQL Server中使用PIVOT行转列

    使用PIVOT行转列 1.建表及插入数据 USE [AdventureDB] GO /****** Object: Table [dbo].[Score] Script Date: 11/25/201 ...

随机推荐

  1. LeetCode(51):N皇后

    Hard! 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问 ...

  2. pytest一:pytest 框架介绍

    pytest 是 python 的一种单元测试框架,与python 自带的 unittest测试框架类似,但是比 unittest 框架使用起来更简洁,效率更高.根据pytest 的官方网站介绍,它具 ...

  3. mysql的基本演示

    数据库需要配置 cmd打开doc窗口 net start mysql:启动数据库 net stop mysql :停止数据库 表的定义:列  行  主键

  4. .NetCore下利用Jenkins如何将程序自动打包发布到Docker容器中运行

    说道这一块纠结了我两天时间,感觉真的很心累,Jenkins的安装就不多说了 这里我们最好直接安装到宿主机上,应该pull到的jenkins版本是2.6的,里面很多都不支持,我自己试了在容器中安装的情况 ...

  5. poj 3461 (模式串T在主串S中出现的次数)

    求模式串在主串中出现的次数Sample Input 3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIANSample Output 130 #include <iost ...

  6. Springboot实现热部署

    所谓的热部署:比如项目的热部署,就是在应用程序在不停止的情况下,实现新的部署 而Springboot在我们每次修改完代码之后,可能只是修改下打印的信息,就得重新启动App类,这样太浪费时间,有没有一种 ...

  7. springmvc返回中文乱码问题

    关于springmvc的返回中文乱码的问题,网上可谓是清一色的一样,无外乎就两种,要么在局部类或这方法上解决,类似如下的代码: @GetMapping(value="/error/query ...

  8. Codeforces 1114F Please, another Queries on Array? 线段树

    Please, another Queries on Array? 利用欧拉函数的计算方法, 用线段树搞一搞就好啦. #include<bits/stdc++.h> #define LL ...

  9. Codeforces 631E Product Sum 斜率优化

    我们先把问题分成两部分, 一部分是把元素往前移, 另一部分是把元素往后移.对于一个 i 后的一个位置, 我们考虑前面哪个移到这里来最优. 我们设最优值为val,   val = max(a[ j ] ...

  10. HDU1285 确定名次 拓扑排序

    Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...