2

Spark を介していくつかのログ ファイルを SQL テーブルに配置すると、スキーマは次のようになります。

|-- timestamp: timestamp (nullable = true) 
|-- c_ip: string (nullable = true) 
|-- cs_username: string (nullable = true) 
|-- s_ip: string (nullable = true) 
|-- s_port: string (nullable = true) 
|-- cs_method: string (nullable = true) 
|-- cs_uri_stem: string (nullable = true) 
|-- cs_query: string (nullable = true) 
|-- sc_status: integer (nullable = false) 
|-- sc_bytes: integer (nullable = false) 
|-- cs_bytes: integer (nullable = false) 
|-- time_taken: integer (nullable = false) 
|-- User_Agent: string (nullable = true) 
|-- Referrer: string (nullable = true) 

お気づきのように、私が読んだタイムスタンプ フィールドは Spark でサポートされています (私が理解している限り、日付は機能しません)。「where timestamp>(2012-10-08 16:10:36.0)」のようなクエリに使用したいのですが、実行するとエラーが発生し続けます。次の 2 つのシンタックス フォームを試しました。parsedate2timestampの2 つの関数を使用します。

タイムスタンプ値の処理方法に関するヒントはありますか?

ありがとう!

1) scala> sqlContext.sql("SELECT * FROM Logs as l where l.timestamp=(2012-10-08 16:10:36.0)").collect

java.lang.RuntimeException: [1.55] failure: ``)'' expected but 16 found 

SELECT * FROM Logs as l where l.timestamp=(2012-10-08 16:10:36.0) 
                                                  ^ 

2) sqlContext.sql("SELECT * FROM Logs as l where l.timestamp="+date2timestamp(formatTime3.parse("2012-10-08 16:10:36.0"))).collect

java.lang.RuntimeException: [1.54] failure: ``UNION'' expected but 16 found 

SELECT * FROM Logs as l where l.timestamp=2012-10-08 16:10:36.0 
                                                 ^ 
4

4 に答える 4

6

問題はまずタイムスタンプの精度であり、タイムスタンプを表す文字列を渡すには文字列としてキャストする必要があると考えまし

したがって、このクエリは機能するようになりました。

sqlContext.sql("SELECT * FROM Logs as l where cast(l.timestampLog as String) <= '2012-10-08 16:10:36'")
于 2014-11-24T15:48:10.360 に答える
5

引用符を忘れました。

次の構文で何かを試してください。

L.timestamp = '2012-07-16 00:00:00'

または、試してください

L.timestamp = CAST('2012-07-16 00:00:00' AS TIMESTAMP)
于 2014-11-22T14:21:30.637 に答える
2

タイムスタンプの文字列表現をタイムスタンプにキャストします。cast('2012-10-10 12:00:00' as timestamp) 次に、文字列ではなくタイムスタンプとして比較を行うことができます。それ以外の:

sqlContext.sql("SELECT * FROM Logs as l where cast(l.timestamp as String) <= '2012-10-08 16:10:36'")

試す

sqlContext.sql("SELECT * FROM Logs as l where l.timestamp <= cast('2012-10-08 16:10:36' as timestamp)")
于 2016-11-04T15:21:17.227 に答える