该方法使用场景为:在hadoop集群进行接口调用,并且获取接口返回值进行解析,解析完成数据写入hive表
其中存在的问题:测试环境和线上环境的一致性,还有接口调用不能一次性并发太高,自己把握这个量
分模块说一下每个模块需要做的:
1、调用get接口:
请求本地接口进行测试,参数需要encode
# -*- coding: utf-8 -*-
import urllib
import urllib2
# get接口调用
get_url ="http://localhost:7000/api/test"
get_params = {"startDate":20180729,"endDate":20180729}
get_params_urlencode = urllib.urlencode(get_params)
get_params_url = get_url+'?'+get_params_urlencode
get_requst = urllib2.Request(url=get_params_url)
get_res_data = urllib2.urlopen(get_requst)
get_res = get_res_data.read()
print get_res;
2、调用post接口:
这个操作复杂一点,加了从文件读取参数,参数是作为list入参
每次读取20个id,循环调用post接口,获取 json 结果进行解析
# -*- coding: utf-8 -*-
import json
import urllib2
import time
file_dir = "/user/tmp/ids"
post_url = "http://localhost:7000/api/users"

本文介绍了一种使用Python调用接口获取数据并写入Hive表的方法。涉及步骤包括调用GET和POST接口,解析JSON结果,将数据写入文件,以及通过Python连接Hive执行SQL加载数据。同时,文中提到了并发控制和环境一致性的问题。
最低0.47元/天 解锁文章
978

被折叠的 条评论
为什么被折叠?



