python后端程序例子_Python MR程序示例

hadoop jar hadoop-streaming-2.6.4.jar \

-D mapreduce.job.name='test' \

-files /local/path/to/mapper.py,/local/path/to/reducer.py

-input /test/data/*

-output /test/output/

-mapper 'python /local/path/to/mapper.py'

-reducer 'python /local/path/to/reducer.py'

1. python文件需要分发到每个节点

2. -mapper和-reducer后面必须带python,否则会报错

Caused by: java.io.IOException: Cannot run program "mapper.py": error=2, No such file or director

mapper.py

#!/usr/bin/python3#-*- coding: utf-8 -*-

importosimportsysimportrefor line insys.stdin:

line=line.strip()

words= re.split('[,.?\s"]',line)for word inwords:

word= word.strip(',|.|?|\s')ifword:print("{0}\t{1}".format(word,1))

reducer.py

#!/usr/bin/env python#-*- coding: utf-8 -*-

importosimportsysfrom operator importitemgetter

current_word=None

current_count=0

word=Nonefor line insys.stdin:

word= line.split('\t',1)[0]

count= line.split('\t',1)[1]

count=int(count)if current_word ==word:

current_count+=countelse:ifcurrent_word:print("{0}\t{1}".format(current_word,current_count))

current_word=word

current_count=countifword:print("{0}\t{1}".format(current_word,current_count))

参考官方说明: https://hadoop.apache.org/docs/r2.7.7/hadoop-streaming/HadoopStreaming.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中使用MQTT协议进行通信可以使用paho-mqtt库,同时也支持TLS/SSL加密。下面是一个简单的示例程序: ```python import ssl import paho.mqtt.client as mqtt # MQTT连接信息 mqtt_broker = "broker.example.com" mqtt_port = 8883 mqtt_username = "username" mqtt_password = "password" # TLS/SSL证书信息 ca_cert = "/path/to/ca.crt" client_cert = "/path/to/client.crt" client_key = "/path/to/client.key" def on_connect(client, userdata, flags, rc): print("Connected with result code " + str(rc)) client.subscribe("test/topic") def on_message(client, userdata, msg): print(msg.topic + " " + str(msg.payload)) client = mqtt.Client() client.tls_set(ca_cert, certfile=client_cert, keyfile=client_key, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2) client.username_pw_set(mqtt_username, mqtt_password) client.on_connect = on_connect client.on_message = on_message client.connect(mqtt_broker, mqtt_port, 60) client.loop_forever() ``` 在此示例中,我们导入了`ssl`和`paho.mqtt.client`库,然后设置MQTT连接信息和TLS/SSL证书信息。在`on_connect`和`on_message`函数中,我们定义了当连接成功和收到消息时的行为。最后,我们创建了一个MQTT客户端对象,设置TLS/SSL证书,连接到MQTT代理,并开始循环以处理所有MQTT事件。 如果您没有TLS/SSL证书,可以使用以下代码禁用TLS/SSL加密: ```python import paho.mqtt.client as mqtt # MQTT连接信息 mqtt_broker = "broker.example.com" mqtt_port = 1883 mqtt_username = "username" mqtt_password = "password" def on_connect(client, userdata, flags, rc): print("Connected with result code " + str(rc)) client.subscribe("test/topic") def on_message(client, userdata, msg): print(msg.topic + " " + str(msg.payload)) client = mqtt.Client() client.username_pw_set(mqtt_username, mqtt_password) client.on_connect = on_connect client.on_message = on_message client.connect(mqtt_broker, mqtt_port, 60) client.loop_forever() ``` 在此示例中,我们将MQTT端口设置为1883,并禁用了TLS/SSL加密。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值