Hadoop Python 调用自定义so动态库

使用import需要在所有宿主机安装相应模块,但使用ctypes库可以调用

  •  test.c
#include <stdio.h>

int add(int);
int out(); 
int buffer[100];
int count = 0;

int add(int num) {
    if (count < 100) {
        buffer[count] = num;
        count++;
    }
    return 0;
}

int out() {
    int i;
    for (i = 0; i < count; i++) {
        printf("%d\n", buffer[i]);
    }
    return 0;
}
  • test_map.py
#!/usr/bin/python
#coding=utf8

import sys 
from ctypes import *

so = './test.so'
cdll.LoadLibrary(so)
api = CDLL(so)
api.add.argtypes = [c_int]
api.add.restype = c_int
api.out.argtypes = []
api.out.restype = c_int

for line in sys.stdin:
    if line.find("100013221") != -1:
        v = line.strip().split('\t')
        api.add(int(v[1]))
    
api.out()
  • test_red.py
#!/usr/bin/python
#coding=utf8

import sys 

for line in sys.stdin:
    sys.stdout.write(line)
  • test.sh
hadoop jar $streaming \
-Dmapred.reduce.tasks=1 \
-Dmapred.output.compress=true \
-Dmapred.output.compression.codec=com.hadoop.compression.lzo.LzoCodec \
-Dmapred.output.compression.type=BLOCK \
-Dmapred.compress.map.output=true \
-Dmapred.map.output.compression.codec=com.hadoop.compression.lzo.LzoCodec \
-input $input \
-output $output \
-mapper "test_map.py" \
-file test_map.py \
-reducer "test_red.py" \
-file test_red.py \
-file test.so

 

 

参考文献:

https://blog.csdn.net/frankie110/article/details/8724509/

http://www.isnowfy.com/introduction-to-python-c-extension/

转载于:https://www.cnblogs.com/jhc888007/p/9018483.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值