简体   繁体   中英

Strings concatenation in Spark SQL query

I'm experimenting with Spark and Spark SQL and I need to concatenate a value at the beginning of a string field that I retrieve as output from a select (with a join) like the following:

val result = sim.as('s)   
    .join(
        event.as('e),
        Inner,
        Option("s.codeA".attr === "e.codeA".attr))   
    .select("1"+"s.codeA".attr, "e.name".attr)  

Let's say my tables contain:

sim :

codeA,codeB
0001,abcd
0002,efgh

events :

codeA,name
0001,freddie
0002,mercury

And I would want as output:

10001,freddie
10002,mercury

In SQL or HiveQL I know I have the concat function available, but it seems Spark SQL doesn't support this feature. Can somebody suggest me a workaround for my issue?

Thank you.

Note : I'm using Language Integrated Queries but I could use just a "standard" Spark SQL query, in case of eventual solution.

The output you add in the end does not seem to be part of your selection, or your SQL logic, if I understand correctly. Why don't you proceed by formatting the output stream as a further step ?

val results = sqlContext.sql("SELECT s.codeA, e.code FROM foobar")
results.map(t => "1" + t(0), t(1)).collect()

It's relatively easy to implement new Expression types directly in your project. Here's what I'm using:

case class Concat(children: Expression*) extends Expression {
  override type EvaluatedType = String

  override def foldable: Boolean = children.forall(_.foldable)
  def nullable: Boolean = children.exists(_.nullable)
  def dataType: DataType = StringType

  def eval(input: Row = null): EvaluatedType = {
    children.map(_.eval(input)).mkString
  }
}

val result = sim.as('s)
    .join(
        event.as('e),
        Inner,
        Option("s.codeA".attr === "e.codeA".attr))
    .select(Concat("1", "s.codeA".attr), "e.name".attr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM