You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/04/29 02:01:43 UTC

[GitHub] sijie closed pull request #1680: Updated Python examples to work with py-2 and py-3

sijie closed pull request #1680: Updated Python examples to work with py-2 and py-3
URL: https://github.com/apache/incubator-pulsar/pull/1680
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-client-cpp/python/examples/rpc_client.py b/pulsar-client-cpp/python/examples/rpc_client.py
index 0df4df7c8f..048e50a468 100755
--- a/pulsar-client-cpp/python/examples/rpc_client.py
+++ b/pulsar-client-cpp/python/examples/rpc_client.py
@@ -24,8 +24,8 @@
 import uuid
 
 
-DEFAULT_CLIENT_TOPIC = 'persistent://sample/standalone/ns/rpc-client-topic'
-DEFAULT_SERVER_TOPIC = 'persistent://sample/standalone/ns/rpc-server-topic'
+DEFAULT_CLIENT_TOPIC = 'rpc-client-topic'
+DEFAULT_SERVER_TOPIC = 'rpc-server-topic'
 UUID = str(uuid.uuid4())
 NUM_CLIENT = 0
 LOCK = threading.Lock()
@@ -58,13 +58,14 @@ def __init__(self,
     def on_response(self, consumer, message):
         if message.partition_key() == self.partition_key \
            and consumer.topic() == self.client_topic:
-            print('Received: {0}'.format(message.data()))
-            self.response = message.data().decode()
+            msg = message.data().decode('utf-8')
+            print('Received: {0}'.format(msg))
+            self.response = msg
             consumer.acknowledge(message)
 
     def call(self, message):
         self.response = None
-        self.producer.send(message, partition_key=self.partition_key)
+        self.producer.send(message.encode('utf-8'), partition_key=self.partition_key)
 
         while self.response is None:
             pass
diff --git a/pulsar-client-cpp/python/examples/rpc_server.py b/pulsar-client-cpp/python/examples/rpc_server.py
index 6849f5686f..3e4e83ab9b 100755
--- a/pulsar-client-cpp/python/examples/rpc_server.py
+++ b/pulsar-client-cpp/python/examples/rpc_server.py
@@ -22,8 +22,8 @@
 import pulsar
 
 
-DEFAULT_CLIENT_TOPIC = 'persistent://sample/standalone/ns/rpc-client-topic'
-DEFAULT_SERVER_TOPIC = 'persistent://sample/standalone/ns/rpc-server-topic'
+DEFAULT_CLIENT_TOPIC = 'rpc-client-topic'
+DEFAULT_SERVER_TOPIC = 'rpc-server-topic'
 
 
 class RPCServer(object):
@@ -43,9 +43,9 @@ def __init__(self,
 
     def on_response(self, consumer, message):
         print('Received from {0}: {1}'.format(message.partition_key(),
-                                              message.data().decode()))
+                                              message.data().decode('utf-8')))
 
-        self.producer.send('{} bar'.format(message.data().decode()),
+        self.producer.send('{} bar'.format(message.data().decode('utf-8')),
                            partition_key=message.partition_key())
         consumer.acknowledge(message)
 
diff --git a/pulsar-client-cpp/python/pulsar.py b/pulsar-client-cpp/python/pulsar.py
index 538191641e..5b7380fe0b 100644
--- a/pulsar-client-cpp/python/pulsar.py
+++ b/pulsar-client-cpp/python/pulsar.py
@@ -51,11 +51,10 @@
 
     client = pulsar.Client('pulsar://localhost:6650')
 
-    producer = client.create_producer(
-                    'persistent://sample/standalone/ns/my-topic')
+    producer = client.create_producer('my-topic')
 
     for i in range(10):
-        producer.send('Hello-%d' % i)
+        producer.send(('Hello-%d' % i).encode('utf-8'))
 
     client.close()
 
@@ -65,13 +64,11 @@
     import pulsar
 
     client = pulsar.Client('pulsar://localhost:6650')
-    consumer = client.subscribe(
-            'persistent://sample/standalone/ns/my-topic',
-            'my-sub')
+    consumer = client.subscribe('my-topic', 'my-subscription')
 
     while True:
         msg = consumer.receive()
-        print("Received message '%s' id='%s'", msg.data(), msg.message_id())
+        print("Received message '%s' id='%s'", msg.data().decode('utf-8'), msg.message_id())
         consumer.acknowledge(msg)
 
     client.close()
@@ -84,7 +81,7 @@
     client = pulsar.Client('pulsar://localhost:6650')
 
     producer = client.create_producer(
-                    'persistent://sample/standalone/ns/my-topic',
+                    'my-topic',
                     block_if_queue_full=True,
                     batching_enabled=True,
                     batching_max_publish_delay_ms=10
@@ -94,7 +91,7 @@ def send_callback(res, msg):
         print('Message published res=%s', res)
 
     while True:
-        producer.send_async('Hello-%d' % i, send_callback)
+        producer.send_async(('Hello-%d' % i).encode('utf-8'), send_callback)
 
     client.close()
 """
@@ -554,7 +551,7 @@ def send(self, content,
         **Args**
 
         * `content`:
-          A string with the message payload
+          A `bytes` object with the message payload.
 
         **Options**
 
@@ -604,7 +601,7 @@ def callback(res, msg):
         **Args**
 
         * `content`:
-          A string with the message payload.
+          A `bytes` object with the message payload.
 
         **Options**
 
diff --git a/pulsar-client-cpp/python/test_consumer.py b/pulsar-client-cpp/python/test_consumer.py
index 3f99706991..dd0f937ab0 100755
--- a/pulsar-client-cpp/python/test_consumer.py
+++ b/pulsar-client-cpp/python/test_consumer.py
@@ -22,11 +22,11 @@
 import pulsar
 
 client = pulsar.Client('pulsar://localhost:6650')
-consumer = client.subscribe('persistent://sample/standalone/ns/my-topic', "my-sub")
+consumer = client.subscribe('my-topic', "my-subscription")
 
 while True:
     msg = consumer.receive()
-    print("Received message '{0}' id='{1}'".format(msg.data(), msg.message_id()))
+    print("Received message '{0}' id='{1}'".format(msg.data().decode('utf-8'), msg.message_id()))
     consumer.acknowledge(msg)
 
 client.close()
diff --git a/pulsar-client-cpp/python/test_producer.py b/pulsar-client-cpp/python/test_producer.py
index 8e3c655fbb..a3dd1f8285 100755
--- a/pulsar-client-cpp/python/test_producer.py
+++ b/pulsar-client-cpp/python/test_producer.py
@@ -24,7 +24,7 @@
 client = pulsar.Client('pulsar://localhost:6650')
 
 producer = client.create_producer(
-                    'persistent://sample/standalone/ns/my-topic',
+                    'my-topic',
                     block_if_queue_full=True,
                     batching_enabled=True,
                     batching_max_publish_delay_ms=10
@@ -32,7 +32,7 @@
 
 for i in range(10):
     try:
-        producer.send('hello', None)
+        producer.send('hello'.encode('utf-8'), None)
     except Exception as e:
         print("Failed to send message: %s", e)
 
diff --git a/site/docs/latest/clients/Python.md b/site/docs/latest/clients/Python.md
index 8cad7c432b..c67357dee1 100644
--- a/site/docs/latest/clients/Python.md
+++ b/site/docs/latest/clients/Python.md
@@ -69,32 +69,27 @@ Below you'll find a variety of Python code examples for the `pulsar-client` libr
 
 ### Producer example
 
-This creates a Python {% popover producer %} for the `persistent://sample/standalone/ns/my-topic` topic and send 10 messages on that topic:
+This creates a Python {% popover producer %} for the `my-topic` topic and send 10 messages on that topic:
 
 ```python
 import pulsar
 
-TOPIC = 'persistent://sample/standalone/ns/my-topic'
-PULSAR_SERVICE_URL = 'pulsar://localhost:6650'
+client = pulsar.Client('pulsar://localhost:6650')
 
-client = pulsar.Client(PULSAR_SERVICE_URL)
-
-producer = client.create_producer(TOPIC)
+producer = client.create_producer('my-topic')
 
 for i in range(10):
-    producer.send('Hello-%d' % i)
+    producer.send(('Hello-%d' % i).encode('utf-8'))
 
 client.close()
 ```
 
 ### Consumer example
 
-This creates a {% popover consumer %} with the `my-sub` {% popover subscription %} on the `persistent://sample/standalone/ns/my-topic` topic, listen for incoming messages, print the content and ID of messages that arrive, and {% popover acknowledge %} each message to the Pulsar {% popover broker %}:
+This creates a {% popover consumer %} with the `my-subscription` {% popover subscription %} on the `my-topic` topic, listen for incoming messages, print the content and ID of messages that arrive, and {% popover acknowledge %} each message to the Pulsar {% popover broker %}:
 
 ```python
-SUBSCRIPTION = 'my-sub'
-
-consumer = client.subscribe(TOPIC, SUBSCRIPTION)
+consumer = client.subscribe('my-topic', 'my-subscription')
 
 while True:
     msg = consumer.receive()
@@ -112,10 +107,10 @@ You can use the Pulsar Python API to use the Pulsar [reader interface](../../get
 # MessageId taken from a previously fetched message
 msg_id = msg.message_id()
 
-reader = client.create_reader(TOPIC, msg_id)
+reader = client.create_reader('my-topic', msg_id)
 
 while True:
     msg = reader.receive()
     print("Received message '%s' id='%s'", msg.data(), msg.message_id())
     # No acknowledgment
-```
\ No newline at end of file
+```
diff --git a/site/docs/latest/getting-started/docker.md b/site/docs/latest/getting-started/docker.md
index 6fbe405eff..79edee414e 100644
--- a/site/docs/latest/getting-started/docker.md
+++ b/site/docs/latest/getting-started/docker.md
@@ -43,7 +43,7 @@ $ docker run -it \
   -p 8080:8080 \
   -v $PWD/data:/pulsar/data \
   apachepulsar/pulsar:{{site.current_version}} \
-  bin/pulsar standalone --advertised-address 127.0.0.1
+  bin/pulsar standalone
 ```
 
 A few things to note about this command:
@@ -92,7 +92,7 @@ First create a consumer and subscribe to the topic:
 import pulsar
 
 client = pulsar.Client('pulsar://localhost:6650')
-consumer = client.subscribe('persistent://sample/standalone/ns1/my-topic',
+consumer = client.subscribe('my-topic',
                             subscription_name='my-sub')
 
 while True:
@@ -109,10 +109,10 @@ Now we can start a producer to send some test messages:
 import pulsar
 
 client = pulsar.Client('pulsar://localhost:6650')
-producer = client.create_producer('persistent://sample/standalone/ns1/my-topic')
+producer = client.create_producer('my-topic')
 
 for i in range(10):
-    producer.send('hello-pulsar-%d' % i)
+    producer.send(('hello-pulsar-%d' % i).encode('utf-8'))
 
 client.close()
 ```
@@ -126,7 +126,7 @@ You can find detailed documentation of all the APIs in the [Admin API Overview](
 In the simplest example, you can use curl to probe the stats for a particular topic:
 
 ```shell
-$ curl http://localhost:8080/admin/persistent/sample/standalone/ns1/my-topic/stats | python -m json.tool
+$ curl http://localhost:8080/admin/persistent/public/default/my-topic/stats | python -m json.tool
 ```
 
 The output will be something like this:


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services