WrapedArray 的 WrappedArray 到 java 数组
WrappedArray of WrapedArray to java array
我有一个类型集的列,我使用 collect_set()
的 spark 数据集 API 其中 returns 一个包装数组的包装数组。我想要一个来自嵌套包装数组的所有值的单个数组。我该怎么做?
例如。卡桑德拉 table:
Col1
{1,2,3}
{1,5}
我正在使用 Spark 数据集 API。
row.get(0)
returns 一个包装数组的包装数组。
如果你有数据框,你可以使用 udf 来扁平化列表
下面是简单的例子
import spark.implicits._
import org.apache.spark.sql.functions._
//create a dummy data
val df = Seq(
(1, List(1,2,3)),
(1, List (5,7,9)),
(2, List(4,5,6)),
(2,List(7,8,9))
).toDF("id", "list")
val df1 = df.groupBy("id").agg(collect_set($"list").as("col1"))
df1.show(false)
df1 的输出:
+---+----------------------------------------------+
|id |col1 |
+---+----------------------------------------------+
|1 |[WrappedArray(1, 2, 3), WrappedArray(5, 7, 9)]|
|2 |[WrappedArray(7, 8, 9), WrappedArray(4, 5, 6)]|
+---+----------------------------------------------+
val testUDF = udf((list: Seq[Seq[Integer]]) => {list.flatten})
df1.withColumn("newCol", testUDF($"col1")).show(false)
输出
+---+----------------------------------------------+------------------+
|id |col1 |newCol |
+---+----------------------------------------------+------------------+
|1 |[WrappedArray(1, 2, 3), WrappedArray(5, 7, 9)]|[1, 2, 3, 5, 7, 9]|
|2 |[WrappedArray(7, 8, 9), WrappedArray(4, 5, 6)]|[7, 8, 9, 4, 5, 6]|
+---+----------------------------------------------+------------------+
希望对您有所帮助!
假设您有 Dataset<Row> ds
,其中有 value
列。
+-----------------------+
|value |
+-----------------------+
|[WrappedArray(1, 2, 3)]|
+-----------------------+
它具有以下架构
root
|-- value: array (nullable = true)
| |-- element: array (containsNull = true)
| | |-- element: integer (containsNull = false)
使用 UDF
定义 UDF1
如下所示。
static UDF1<WrappedArray<WrappedArray<Integer>>, List<Integer>> getValue = new UDF1<WrappedArray<WrappedArray<Integer>>, List<Integer>>() {
public List<Integer> call(WrappedArray<WrappedArray<Integer>> data) throws Exception {
List<Integer> intList = new ArrayList<Integer>();
for(int i=0; i<data.size(); i++){
intList.addAll(JavaConversions.seqAsJavaList(data.apply(i)));
}
return intList;
}
};
注册并拨打 UDF1
如下所示
import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.callUDF;
import scala.collection.JavaConversions;
//register UDF
spark.udf().register("getValue", getValue, DataTypes.createArrayType(DataTypes.IntegerType));
//Call UDF
Dataset<Row> ds1 = ds.select(col("*"), callUDF("getValue", col("value")).as("udf-value"));
ds1.show();
使用分解函数
import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.explode;
Dataset<Row> ds2 = ds.select(explode(col("value")).as("explode-value"));
ds2.show(false);
我有一个类型集的列,我使用 collect_set()
的 spark 数据集 API 其中 returns 一个包装数组的包装数组。我想要一个来自嵌套包装数组的所有值的单个数组。我该怎么做?
例如。卡桑德拉 table:
Col1
{1,2,3}
{1,5}
我正在使用 Spark 数据集 API。
row.get(0)
returns 一个包装数组的包装数组。
如果你有数据框,你可以使用 udf 来扁平化列表 下面是简单的例子
import spark.implicits._
import org.apache.spark.sql.functions._
//create a dummy data
val df = Seq(
(1, List(1,2,3)),
(1, List (5,7,9)),
(2, List(4,5,6)),
(2,List(7,8,9))
).toDF("id", "list")
val df1 = df.groupBy("id").agg(collect_set($"list").as("col1"))
df1.show(false)
df1 的输出:
+---+----------------------------------------------+
|id |col1 |
+---+----------------------------------------------+
|1 |[WrappedArray(1, 2, 3), WrappedArray(5, 7, 9)]|
|2 |[WrappedArray(7, 8, 9), WrappedArray(4, 5, 6)]|
+---+----------------------------------------------+
val testUDF = udf((list: Seq[Seq[Integer]]) => {list.flatten})
df1.withColumn("newCol", testUDF($"col1")).show(false)
输出
+---+----------------------------------------------+------------------+
|id |col1 |newCol |
+---+----------------------------------------------+------------------+
|1 |[WrappedArray(1, 2, 3), WrappedArray(5, 7, 9)]|[1, 2, 3, 5, 7, 9]|
|2 |[WrappedArray(7, 8, 9), WrappedArray(4, 5, 6)]|[7, 8, 9, 4, 5, 6]|
+---+----------------------------------------------+------------------+
希望对您有所帮助!
假设您有 Dataset<Row> ds
,其中有 value
列。
+-----------------------+
|value |
+-----------------------+
|[WrappedArray(1, 2, 3)]|
+-----------------------+
它具有以下架构
root
|-- value: array (nullable = true)
| |-- element: array (containsNull = true)
| | |-- element: integer (containsNull = false)
使用 UDF
定义 UDF1
如下所示。
static UDF1<WrappedArray<WrappedArray<Integer>>, List<Integer>> getValue = new UDF1<WrappedArray<WrappedArray<Integer>>, List<Integer>>() {
public List<Integer> call(WrappedArray<WrappedArray<Integer>> data) throws Exception {
List<Integer> intList = new ArrayList<Integer>();
for(int i=0; i<data.size(); i++){
intList.addAll(JavaConversions.seqAsJavaList(data.apply(i)));
}
return intList;
}
};
注册并拨打 UDF1
如下所示
import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.callUDF;
import scala.collection.JavaConversions;
//register UDF
spark.udf().register("getValue", getValue, DataTypes.createArrayType(DataTypes.IntegerType));
//Call UDF
Dataset<Row> ds1 = ds.select(col("*"), callUDF("getValue", col("value")).as("udf-value"));
ds1.show();
使用分解函数
import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.explode;
Dataset<Row> ds2 = ds.select(explode(col("value")).as("explode-value"));
ds2.show(false);