ArrayType(StringType) to IntegerType conversion in spark dataframe
I'm trying to groupBy
column name host
and aggregate average of a column of type ArrayType(StringType)
after type casting it to ArrayType(IntegerType)
.
It throws below error
`cannot resolve `avg(variables)` due to datatype mismatch: function average requires numeric types, not ArrayType(IntegerType,true);
Input Data - sample Dataframe before grouping
|request|time |type |host |service | variables |
|REST |1542111483170|RESTFUL|KAFKA|www.google.com|[Duration, 7,Type] |
|REST |1542111486570|RESTFUL|KAFKA|www.google.com|[Duration, 9, Type]|
How to cast or handle ArrayType(StringType) to IntegerType i.e Column variables is of ArrayType(varaible.variable:String,varaible.value:String,varaible.TypeString)
I want to convert 2nd value of Array varaible.value
to Integer for Aggregation (Average calculation)?
Case Class:
case class ServiceActivity(val request: String, val time: Long, val Type: String, val host: String, val service: String, val variables: Array[Variables])
case class Variables(val variable: String, val value: String, val Type: String)
Code Below:
val report = df.select("*").where(array_contains(df("variables.variable"),"Duration"))
val intermediate = report.withColumn("variables", col(variables.value).cast(org.apache.spark.sql.types.ArrayType(org.apache.spark.sql.types.IntegerType,true)
intermediate.withColumn("duration",$"variables".getItem(2)).drop("variables").withColumnRenamed("duration","variables")
GroupBy Code(error):
intermediate.groupBy(intermediate("host")).agg(Map("variables"->"avg"))
Any workarounds.
Thanks
apache-spark apache-spark-sql
|
show 2 more comments
I'm trying to groupBy
column name host
and aggregate average of a column of type ArrayType(StringType)
after type casting it to ArrayType(IntegerType)
.
It throws below error
`cannot resolve `avg(variables)` due to datatype mismatch: function average requires numeric types, not ArrayType(IntegerType,true);
Input Data - sample Dataframe before grouping
|request|time |type |host |service | variables |
|REST |1542111483170|RESTFUL|KAFKA|www.google.com|[Duration, 7,Type] |
|REST |1542111486570|RESTFUL|KAFKA|www.google.com|[Duration, 9, Type]|
How to cast or handle ArrayType(StringType) to IntegerType i.e Column variables is of ArrayType(varaible.variable:String,varaible.value:String,varaible.TypeString)
I want to convert 2nd value of Array varaible.value
to Integer for Aggregation (Average calculation)?
Case Class:
case class ServiceActivity(val request: String, val time: Long, val Type: String, val host: String, val service: String, val variables: Array[Variables])
case class Variables(val variable: String, val value: String, val Type: String)
Code Below:
val report = df.select("*").where(array_contains(df("variables.variable"),"Duration"))
val intermediate = report.withColumn("variables", col(variables.value).cast(org.apache.spark.sql.types.ArrayType(org.apache.spark.sql.types.IntegerType,true)
intermediate.withColumn("duration",$"variables".getItem(2)).drop("variables").withColumnRenamed("duration","variables")
GroupBy Code(error):
intermediate.groupBy(intermediate("host")).agg(Map("variables"->"avg"))
Any workarounds.
Thanks
apache-spark apache-spark-sql
can you share the schema of dataframedf
?
– Shankar Koirala
Nov 14 '18 at 8:09
Question Updated
– Vicky
Nov 14 '18 at 8:14
@ShankarKoirala, Any Updates.
– Vicky
Nov 14 '18 at 10:14
Could you add more detials on how you are inputing Variables dataframe and combining it with ServiceActivity dataframe? Sample code and data would be helpful to replicate the problem.
– user238607
Nov 14 '18 at 12:35
Can you please update in detail with input data and required output data
– Shankar Koirala
Nov 14 '18 at 13:23
|
show 2 more comments
I'm trying to groupBy
column name host
and aggregate average of a column of type ArrayType(StringType)
after type casting it to ArrayType(IntegerType)
.
It throws below error
`cannot resolve `avg(variables)` due to datatype mismatch: function average requires numeric types, not ArrayType(IntegerType,true);
Input Data - sample Dataframe before grouping
|request|time |type |host |service | variables |
|REST |1542111483170|RESTFUL|KAFKA|www.google.com|[Duration, 7,Type] |
|REST |1542111486570|RESTFUL|KAFKA|www.google.com|[Duration, 9, Type]|
How to cast or handle ArrayType(StringType) to IntegerType i.e Column variables is of ArrayType(varaible.variable:String,varaible.value:String,varaible.TypeString)
I want to convert 2nd value of Array varaible.value
to Integer for Aggregation (Average calculation)?
Case Class:
case class ServiceActivity(val request: String, val time: Long, val Type: String, val host: String, val service: String, val variables: Array[Variables])
case class Variables(val variable: String, val value: String, val Type: String)
Code Below:
val report = df.select("*").where(array_contains(df("variables.variable"),"Duration"))
val intermediate = report.withColumn("variables", col(variables.value).cast(org.apache.spark.sql.types.ArrayType(org.apache.spark.sql.types.IntegerType,true)
intermediate.withColumn("duration",$"variables".getItem(2)).drop("variables").withColumnRenamed("duration","variables")
GroupBy Code(error):
intermediate.groupBy(intermediate("host")).agg(Map("variables"->"avg"))
Any workarounds.
Thanks
apache-spark apache-spark-sql
I'm trying to groupBy
column name host
and aggregate average of a column of type ArrayType(StringType)
after type casting it to ArrayType(IntegerType)
.
It throws below error
`cannot resolve `avg(variables)` due to datatype mismatch: function average requires numeric types, not ArrayType(IntegerType,true);
Input Data - sample Dataframe before grouping
|request|time |type |host |service | variables |
|REST |1542111483170|RESTFUL|KAFKA|www.google.com|[Duration, 7,Type] |
|REST |1542111486570|RESTFUL|KAFKA|www.google.com|[Duration, 9, Type]|
How to cast or handle ArrayType(StringType) to IntegerType i.e Column variables is of ArrayType(varaible.variable:String,varaible.value:String,varaible.TypeString)
I want to convert 2nd value of Array varaible.value
to Integer for Aggregation (Average calculation)?
Case Class:
case class ServiceActivity(val request: String, val time: Long, val Type: String, val host: String, val service: String, val variables: Array[Variables])
case class Variables(val variable: String, val value: String, val Type: String)
Code Below:
val report = df.select("*").where(array_contains(df("variables.variable"),"Duration"))
val intermediate = report.withColumn("variables", col(variables.value).cast(org.apache.spark.sql.types.ArrayType(org.apache.spark.sql.types.IntegerType,true)
intermediate.withColumn("duration",$"variables".getItem(2)).drop("variables").withColumnRenamed("duration","variables")
GroupBy Code(error):
intermediate.groupBy(intermediate("host")).agg(Map("variables"->"avg"))
Any workarounds.
Thanks
apache-spark apache-spark-sql
apache-spark apache-spark-sql
edited Nov 15 '18 at 5:33
Vicky
asked Nov 14 '18 at 8:01
VickyVicky
97
97
can you share the schema of dataframedf
?
– Shankar Koirala
Nov 14 '18 at 8:09
Question Updated
– Vicky
Nov 14 '18 at 8:14
@ShankarKoirala, Any Updates.
– Vicky
Nov 14 '18 at 10:14
Could you add more detials on how you are inputing Variables dataframe and combining it with ServiceActivity dataframe? Sample code and data would be helpful to replicate the problem.
– user238607
Nov 14 '18 at 12:35
Can you please update in detail with input data and required output data
– Shankar Koirala
Nov 14 '18 at 13:23
|
show 2 more comments
can you share the schema of dataframedf
?
– Shankar Koirala
Nov 14 '18 at 8:09
Question Updated
– Vicky
Nov 14 '18 at 8:14
@ShankarKoirala, Any Updates.
– Vicky
Nov 14 '18 at 10:14
Could you add more detials on how you are inputing Variables dataframe and combining it with ServiceActivity dataframe? Sample code and data would be helpful to replicate the problem.
– user238607
Nov 14 '18 at 12:35
Can you please update in detail with input data and required output data
– Shankar Koirala
Nov 14 '18 at 13:23
can you share the schema of dataframe
df
?– Shankar Koirala
Nov 14 '18 at 8:09
can you share the schema of dataframe
df
?– Shankar Koirala
Nov 14 '18 at 8:09
Question Updated
– Vicky
Nov 14 '18 at 8:14
Question Updated
– Vicky
Nov 14 '18 at 8:14
@ShankarKoirala, Any Updates.
– Vicky
Nov 14 '18 at 10:14
@ShankarKoirala, Any Updates.
– Vicky
Nov 14 '18 at 10:14
Could you add more detials on how you are inputing Variables dataframe and combining it with ServiceActivity dataframe? Sample code and data would be helpful to replicate the problem.
– user238607
Nov 14 '18 at 12:35
Could you add more detials on how you are inputing Variables dataframe and combining it with ServiceActivity dataframe? Sample code and data would be helpful to replicate the problem.
– user238607
Nov 14 '18 at 12:35
Can you please update in detail with input data and required output data
– Shankar Koirala
Nov 14 '18 at 13:23
Can you please update in detail with input data and required output data
– Shankar Koirala
Nov 14 '18 at 13:23
|
show 2 more comments
1 Answer
1
active
oldest
votes
Sorted out by splitting the Array and using concat_ws
method
val splitArray = ab.withColumn("Avg_Duration", concat_ws("", ab("variables.value")))
splitArray.groupBy(splitArray("host")).agg(Map("Avg_Duration" -> "avg")).show(false)
Thank you
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295454%2farraytypestringtype-to-integertype-conversion-in-spark-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sorted out by splitting the Array and using concat_ws
method
val splitArray = ab.withColumn("Avg_Duration", concat_ws("", ab("variables.value")))
splitArray.groupBy(splitArray("host")).agg(Map("Avg_Duration" -> "avg")).show(false)
Thank you
add a comment |
Sorted out by splitting the Array and using concat_ws
method
val splitArray = ab.withColumn("Avg_Duration", concat_ws("", ab("variables.value")))
splitArray.groupBy(splitArray("host")).agg(Map("Avg_Duration" -> "avg")).show(false)
Thank you
add a comment |
Sorted out by splitting the Array and using concat_ws
method
val splitArray = ab.withColumn("Avg_Duration", concat_ws("", ab("variables.value")))
splitArray.groupBy(splitArray("host")).agg(Map("Avg_Duration" -> "avg")).show(false)
Thank you
Sorted out by splitting the Array and using concat_ws
method
val splitArray = ab.withColumn("Avg_Duration", concat_ws("", ab("variables.value")))
splitArray.groupBy(splitArray("host")).agg(Map("Avg_Duration" -> "avg")).show(false)
Thank you
answered Nov 15 '18 at 5:34
VickyVicky
97
97
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295454%2farraytypestringtype-to-integertype-conversion-in-spark-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you share the schema of dataframe
df
?– Shankar Koirala
Nov 14 '18 at 8:09
Question Updated
– Vicky
Nov 14 '18 at 8:14
@ShankarKoirala, Any Updates.
– Vicky
Nov 14 '18 at 10:14
Could you add more detials on how you are inputing Variables dataframe and combining it with ServiceActivity dataframe? Sample code and data would be helpful to replicate the problem.
– user238607
Nov 14 '18 at 12:35
Can you please update in detail with input data and required output data
– Shankar Koirala
Nov 14 '18 at 13:23