Snappydata - 在jobserver上放入的SQL不会聚合值

时间:2017-10-24 08:30:12

标签: spark-streaming snappydata

我正在尝试使用流式传输创建一个在snappy-job shell上运行的jar。 我有聚合功能,它在Windows中完美地工作。但是我需要有一个表,每个键都有一个值。基于github的一个例子创建一个jar文件,现在我遇到了输入sql命令的问题。

我的汇总代码:

val resultStream: SchemaDStream = snsc.registerCQ("select publisher, cast(sum(bid)as int) as bidCount from " +
  "AggrStream window (duration 1 seconds, slide 1 seconds) group by publisher")

val conf = new ConnectionConfBuilder(snsc.snappySession).build()

resultStream.foreachDataFrame(df => {

  df.write.insertInto("windowsAgg")

    println("Data received in streaming window")
    df.show()

    println("Updating table updateTable")
    val conn = ConnectionUtil.getConnection(conf)
    val result = df.collect()

  val stmt = conn.prepareStatement("put into updateTable (publisher, bidCount) values  " +
    "(?,?+(nvl((select bidCount from updateTable where publisher = ?),0)))")

    result.foreach(row => {
      println("row" + row)
      val publisher = row.getString(0)
      println("publisher " + publisher)
      val bidCount = row.getInt(1)
      println("bidcount : " + bidCount)

      stmt.setString(1, publisher)
      stmt.setInt(2, bidCount)
      stmt.setString(3, publisher)

      println("Prepared Statement after bind variables set: " + stmt.toString())

      stmt.addBatch()
    }
    )
    stmt.executeBatch()
    conn.close()
})

snsc.start()
snsc.awaitTermination()
}

我必须更新或插入表updateTable,但在更新命令期间,必须将当前值添加到流中的值。 现在:

执行代码时看到的内容:

select * from updateTable;
PUBLISHER                       |BIDCOUNT   
--------------------------------------------
publisher333                    |10  

然后我发信息给kafka:

1488487984048,publisher333,adv1,web1,geo1,11,c1 

再次从updateTable中选择:

select * from updateTable;
PUBLISHER                       |BIDCOUNT   
--------------------------------------------
publisher333                    |11  

将覆盖Bidcount值而不是添加。 但是当我从snappy-sql shell执行put into命令时,它运行得很好:

put into updateTable (publisher, bidcount) values ('publisher333',4+
(nvl((select bidCount from updateTable where publisher = 
'publisher333'),0)));
1 row inserted/updated/deleted
snappy> select * from updateTable;
PUBLISHER                       |BIDCOUNT   
--------------------------------------------
publisher333                    |15   

你可以帮我解决这个案子吗? Mayby有人使用snappydata进行插入或更新值的其他解决方案吗?

先谢谢你。

1 个答案:

答案 0 :(得分:0)

如果是流式传输,则从tomi_update表读取

bidCount值,但是在snappy-sql的情况下,它会从updateTable读取。这是故意的吗?可能你想在两种情况下都使用updateTable吗?