Can I call the Bluemix message hub service from Python?

后端 未结 4 844
梦如初夏
梦如初夏 2021-01-24 02:56

The kafka-python client supports Kafka 0.9 but doesn\'t obviously include the new authentication and encryption features so my guess is that it only works with open servers (as

相关标签:
4条回答
  • 2021-01-24 02:56

    The SASL support in the Kafka Python client has been requested : https://github.com/dpkp/kafka-python/issues/533 but until the username/password login method used by Message Hub is supported, it won't work

    0 讨论(0)
  • 2021-01-24 03:05

    We've added some text to our documentation on non-Java language support - see the "CONNECTING AND AUTHENTICATING IN A NON-JAVA APPLICATION" section: https://www.ng.bluemix.net/docs/services/MessageHub/index.html

    Our current authentication method is non-standard and not supported by the Apache project, but was a temporary solution. The Message Hub team is working with the Apache Kafka community to develop KIP-43. Once this is finalised, we'll change the Message Hub authentication implementation to match and it will be possible to implement clients to that spec in any language.

    0 讨论(0)
  • 2021-01-24 03:08

    Until this is natively supported by the Bluemix Apache Spark Service, you can follow the same approach as the Realtime Sentiment Analysis project. Helper code for this can be found on the cds labs spark samples github repo.

    0 讨论(0)
  • 2021-01-24 03:19

    I was able to connect using the kafka-python library:

    $ pip install --user kafka-python
    

    Then ...

    from kafka import KafkaProducer
    from kafka.errors import KafkaError
    import ssl
    
    ############################################
    # Service credentials from Bluemix UI:
    ############################################
    bootstrap_servers =   # kafka_brokers_sasl
    sasl_plain_username = # user
    sasl_plain_password = # password
    ############################################
    
    sasl_mechanism = 'PLAIN'
    security_protocol = 'SASL_SSL'
    
    # Create a new context using system defaults, disable all but TLS1.2
    context = ssl.create_default_context()
    context.options &= ssl.OP_NO_TLSv1
    context.options &= ssl.OP_NO_TLSv1_1
    
    producer = KafkaProducer(bootstrap_servers = bootstrap_servers,
                             sasl_plain_username = sasl_plain_username,
                             sasl_plain_password = sasl_plain_password,
                             security_protocol = security_protocol,
                             ssl_context = context,
                             sasl_mechanism = sasl_mechanism,
                             api_version=(0,10))
    
    # Asynchronous by default
    future = producer.send('my-topic', b'raw_bytes')
    
    # Block for 'synchronous' sends
    try:
        record_metadata = future.get(timeout=10)
    except KafkaError:
        # Decide what to do if produce request failed...
        log.exception()
        pass
    
    # Successful result returns assigned partition and offset
    print (record_metadata.topic)
    print (record_metadata.partition)
    print (record_metadata.offset)
    

    This worked for me from Bluemix spark as a service from a jupyter notebook, however, note that this approach is not using spark. The code is just running on the driver host.

    0 讨论(0)
提交回复
热议问题