如何在Kafka上使用flatMapValues

时间:2017-11-29 14:22:12

标签: scala apache-kafka-streams flatmap

在Scala中使用flatMapValues和Kafka库时出现错误。这是我的代码:

private void beginReliableWriteToGattServer(BluetoothDevice device, UUID serviceUUID,UUID charUUID, byte[] byte1){

        if(mGatt != null){
            BluetoothGattService service = mGatt.getService(serviceUUID);
            if(service != null){
                BluetoothGattCharacteristic gattCharacteristic = service.getCharacteristic(charUUID);
                if(gattCharacteristic != null){
                    Logger.d(TAG, "BeginReliable Write="+mGatt.beginReliableWrite());
                    gattCharacteristic.setValue(byte1);
                    mGatt.writeCharacteristic(gattCharacteristic);
                    Logger.d(TAG, "ExecuteReliable Write="+mGatt.executeReliableWrite());

                }
            }
        }
    }

Below are write Gatt characteristic logs 
BeginReliable Write=true
ExecuteReliable Write=false
D/Bluetooth_GATTCallBack: onCharacteristicWrite 17 

我在val builder: KStreamBuilder = new KStreamBuilder() val textLines: KStream[String, String] = builder.stream("streams-plaintext-input") import collection.JavaConverters.asJavaIterableConverter val wordCounts: KTable[String, JLong] = textLines .flatMapValues(textLine => textLine.toLowerCase.split("\\W+").toIterable.asJava) .groupBy((_, word) => word) .count("word-counts") 内收到了missing parameter type的错误textLine。如果我替换为flatMapValues,它仍然无效。

任何人都有一些想法? 谢谢,费利佩

1 个答案:

答案 0 :(得分:2)

使用Scala 2.12.4我这样解决了:

  val props = new Properties
  props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-wordcount")
  props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092")
  props.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String.getClass.getName)
  props.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String.getClass.getName)

  val stringSerde: Serde[String] = Serdes.String()
  val longSerde: Serde[Long] = Serdes.Long()

  val builder = new StreamsBuilder()
  val textLines: KStream[String, String] = builder.stream("streams-plaintext-input")

  val topology: Topology = builder.build()

  println(topology.describe())

  val wordCounts: KTable[String, Long] = textLines
    .flatMapValues { textLine =>
      println(textLine)
      println(topology.describe())
      textLine.toLowerCase.split("\\W+").toIterable.asJava
    }
    .groupBy((_, word) => word)
    // this is a stateful computation config to the topology
    .count("word-counts")

  wordCounts.to(stringSerde, longSerde, "streams-wordcount-output")

  val streams = new KafkaStreams(topology, props)
  streams.start()