我正在Google Cloud上使用Spark处理来自Google Analytics(分析)的数据,但是我不知道如何根据索引选择自定义维度
GA自定义维度的结构具有以下特点:
ARRAY<STRUCT< index: INTEGER, value:STRING >>
通常,在BigQuery中,我会做一个子查询来选择数据,例如
SELECT (select value from customDimensions where index = 2)
但是如here中的子查询中所述,尚不支持select中的子查询。
答案 0 :(得分:2)
对Google Cloud上的Spark一无所知,但是如果它与Apache Spark足够接近,则可以使用element_at函数,如果column为array后跟{{1 }}访问者。
dot
由于未知// create a sample dataset
val structData = Seq((0,"zero"), (1, "one")).toDF("id", "value")
val data = structData
.select(struct("id", "value") as "s")
.groupBy()
.agg(collect_list("s") as "a")
// the schema matches the requirements
scala> data.printSchema
root
|-- a: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- id: integer (nullable = false)
| | |-- value: string (nullable = true)
data.createOrReplaceTempView("customDimensions")
,因此以下查询将不起作用。
index
让我们改用scala> sql("select value from customDimensions where index = 2").show
org.apache.spark.sql.AnalysisException: cannot resolve '`index`' given input columns: [customdimensions.a]; line 1 pos 41;
'Project ['value]
+- 'Filter ('index = 2)
+- SubqueryAlias `customdimensions`
+- Aggregate [collect_list(s#9, 0, 0) AS a#13]
+- Project [named_struct(id, id#5, value, value#6) AS s#9]
+- Project [_1#2 AS id#5, _2#3 AS value#6]
+- LocalRelation [_1#2, _2#3]
...
标准函数。
element_at
“数组”是一个结构,因此您可以使用scala> sql("select element_at(a, 2) from customDimensions").show
+----------------+
|element_at(a, 2)|
+----------------+
| [1, one]|
+----------------+
(点)。
.