12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import redis
- from odps import ODPS
- redis_host = 'r-bp1j1vsznx8h813ddk.redis.rds.aliyuncs.com'
- redis_port = 6379
- redis_password = 'Wqsd@2019'
- redis_db = 0
- access_id = 'LTAI9EBa0bd5PrDa'
- access_key = 'vAalxds7YxhfOA2yVv8GziCg3Y87v5'
- project_name = 'loghubods'
- endpoint = 'http://service.odps.aliyun.com/api'
- input_file = 'input.txt'
- output_file = 'output.txt'
- def process_data():
- """
- 从阿里云 DataWorks (MaxCompute) 读取数据,从 Redis 获取 value,并写入新文件。
- """
- try:
-
- o = ODPS(access_id, access_key, project=project_name, endpoint=endpoint)
-
- r = redis.Redis(host=redis_host, port=redis_port, password=redis_password, db=redis_db)
-
- sql = "SELECT * FROM loghubods.mid_generate_date_18;"
- with o.execute_sql(sql).open_reader(tunnel=True) as reader:
- with open(output_file, 'w') as outfile:
- for record in reader:
- key = record['mid']
- redis_key = "mid:generate:timestamp:" + str(key)
- value = r.get(redis_key)
- if value:
-
- outfile.write(f"{key} {value.decode('utf-8')}\n")
- else:
- outfile.write(f"{key} -\n")
- print(f"Key '{key}' not found in Redis.")
- except Exception as e:
- print(f"An error occurred: {e}")
- if __name__ == "__main__":
- process_data()
|