Github user mridulm commented on a diff in the pull request:

    https://github.com/apache/bahir/pull/13#discussion_r72379877
  
    --- Diff: 
sql-streaming-mqtt/src/main/scala/org/apache/bahir/sql/streaming/mqtt/MQTTStream.scala
 ---
    @@ -0,0 +1,141 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.bahir.sql.streaming.mqtt
    +
    +import java.sql.Timestamp
    +import java.text.SimpleDateFormat
    +import java.util.Calendar
    +import java.util.concurrent.atomic.AtomicLong
    +import javax.annotation.concurrent.GuardedBy
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.eclipse.paho.client.mqttv3._
    +import org.eclipse.paho.client.mqttv3.persist.{MemoryPersistence, 
MqttDefaultFilePersistence}
    +
    +import org.apache.spark.sql.{DataFrame, SQLContext}
    +import org.apache.spark.sql.execution.streaming.{LongOffset, Offset, 
Source}
    +import org.apache.spark.sql.sources.{DataSourceRegister, 
StreamSourceProvider}
    +import org.apache.spark.sql.types.{StringType, StructField, StructType, 
TimestampType}
    +
    +
    +object MQTTStream {
    +
    +  val DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    +
    +  val SCHEMA_DEFAULT = StructType(StructField("value", StringType)
    +    :: StructField("timestamp", TimestampType) :: Nil)
    +}
    +
    +class MQTTTextStream(brokerUrl: String, persistence: MqttClientPersistence,
    +    topic: String, messageParser: Array[Byte] => (String, Timestamp),
    +    sqlContext: SQLContext) extends Source with Logging {
    +
    +  override def schema: StructType = MQTTStream.SCHEMA_DEFAULT
    +
    +  @GuardedBy("this")
    +  private var messages = new ArrayBuffer[(String, Timestamp)]
    +  initialize()
    +  private def initialize(): Unit = {
    +
    +    val client = new MqttClient(brokerUrl, MqttClient.generateClientId(), 
persistence)
    +    val mqttConnectOptions: MqttConnectOptions = new MqttConnectOptions()
    +    mqttConnectOptions.setAutomaticReconnect(true)
    +    val callback = new MqttCallbackExtended() {
    +
    +      override def messageArrived(topic_ : String, message: MqttMessage): 
Unit = {
    +        messages += messageParser(message.getPayload)
    +        log.trace(s"Message arrived, $topic_ $message")
    +      }
    +
    +      override def deliveryComplete(token: IMqttDeliveryToken): Unit = {
    +      }
    +
    +      override def connectionLost(cause: Throwable): Unit = {
    +        log.warn("Connection to mqtt server lost.", cause)
    +      }
    +
    +      override def connectComplete(reconnect: Boolean, serverURI: String): 
Unit = {
    +        log.info(s"connect complete $serverURI. Is it a reconnect?: 
$reconnect")
    +      }
    +    }
    +    client.setCallback(callback)
    +    client.connect(mqttConnectOptions)
    +    client.subscribe(topic)
    +  }
    +
    +  /** Stop this source and free any resources it has allocated. */
    +  override def stop(): Unit = {
    +  }
    +
    +  /** Returns the maximum available offset for this source. */
    +  override def getOffset: Option[Offset] = {
    +    if (messages.isEmpty) {
    +      None
    +    } else {
    +      Some(LongOffset(messages.size))
    +    }
    --- End diff --
    
    GuardedBy here wont work, right ?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to