当前位置: 首页 > news >正文

好的模板网站推荐淘宝运营视频教程全集

好的模板网站推荐,淘宝运营视频教程全集,学校品牌建设,wordpress搬家 后台错乱将excel文件转成txt文件,再将txt文件上传hdfs,最后传入hive中 注意的点 (1)先判断写入的txt文件是否存在,如果不存在就需要创建路径 (2)如果txt文件已经存在,那么先将对应的文件进行…

将excel文件转成txt文件,再将txt文件上传hdfs,最后传入hive中

注意的点
(1)先判断写入的txt文件是否存在,如果不存在就需要创建路径
(2)如果txt文件已经存在,那么先将对应的文件进行删除后再写入txt数据
(3)excel文件中有可能第一行是字段名,需要跳过

(版本1 :本地版本)
1.利用python将excel转成txt文件

from datetime import datetime, timedelta
import os
import pytz
import pandas as pddef excel_to_txt(name, date):# Read Excel file into a DataFramedf = pd.read_excel(f'data/excel/{name}.xlsx', header=None, skiprows=1)# Define output directory and pathoutput_directory = os.path.join('data', 'txt', date)os.makedirs(output_directory, exist_ok=True)  # Create directory if it doesn't existoutput_path = os.path.join(output_directory, f'{name}.txt')# Check if the file already exists, if so, remove itif os.path.exists(output_path):os.remove(output_path)print(f'Existing file {output_path} removed.')# Write DataFrame to a new text fileprint('开始写入txt文件')df.to_csv(output_path, header=None, sep='\t', index=False)print('文件写入成功!')return output_pathif __name__ == '__main__':current_time = datetime.now(pytz.timezone('Asia/Shanghai'))one_day_ago = (current_time - timedelta(days=1)).strftime('%Y-%m-%d')local_file_path = excel_to_txt('IS_GS_Recruitment_Data_20231211', one_day_ago)print(local_file_path)

2.上传到hdfs
3.在hive中创建表

drop table if exists ticket.test_text;
create external table IF NOT EXISTS ticket.test_text
(name string,age int
) comment ''row format delimited fields terminated by '\t'lines terminated by '\n'NULL DEFINED AS ''stored as textfileLOCATION '/warehouse/ticket/ods/test_text';

4.将hdfs数据写入hive

load data inpath '/origin_data/test.txt' overwrite into table ticket.test_text;

(2)服务器版本
先把excel_to_txt脚本上传到服务器

excel_to_txt.py

from datetime import datetime, timedelta
import os
import pytz
import pandas as pddef excel_to_txt(name, date):# Read Excel file into a DataFramedf = pd.read_excel(f'/opt/module/data/excel/{name}.xlsx', header=None,skiprows=1)# df = pd.read_excel(f'hdfs://mycluster:8020/origin_data/hr_cn/db/is_gs_recruitment_data_full/excel/{name}.xlsx', header=None,skiprows=1)# df = pd.read_excel(f'data/excel/{name}.xlsx', header=None,skiprows=1)# Define output directory and pathoutput_directory = os.path.join('/opt/module/data', 'txt', date)os.makedirs(output_directory, exist_ok=True)  # Create directory if it doesn't existoutput_path = os.path.join(output_directory, f'{name}.txt')# Check if the file already exists, if so, remove itif os.path.exists(output_path):os.remove(output_path)print(f'Existing file {output_path} removed.')# Write DataFrame to a new text fileprint('开始写入txt文件')df.to_csv(output_path, header=None, sep='\t', index=False)print('文件写入成功!')return output_pathif __name__ == '__main__':current_time = datetime.now(pytz.timezone('Asia/Shanghai'))one_day_ago = (current_time - timedelta(days=1)).strftime('%Y-%m-%d')local_file_path = excel_to_txt('IS_GS_Recruitment_Data_20231211', one_day_ago)print(local_file_path)

2.安装python3环境,安装链接:
https://editor.csdn.net/md/?articleId=129627849

3.执行python脚本

recruitment_excel_to_txt.sh

#!/bin/bash
/opt/module/miniconda3/bin/python  /opt/module/data/excel/excel_to_txt.py

4.上传到hdfs,并将数据导入hive

recruitment_hdfs_to_ods.sh

#!/bin/bash
DATAX_HOME=/opt/module/datax# 如果传入日期则do_date等于传入的日期,否则等于前一天日期
if [ -n "$2" ] ;thendatestr=$2
elsedatestr=$(date -d "-1 day" +%F)
fi# 处理目标路径,检查目标路径是否存在且不为空,如果不为空,则清空目录内容
handle_target() {content_size=$(hadoop fs -count $1 | awk '{print $3}')if [[ $content_size -ne 0 ]]; thenecho "路径$1不为空,正在清空......"hadoop fs -rm -r -f $1/*fi
}# 整合处理目标路径和上传文件的逻辑
handle_target_and_put() {handle_target $2echo "上傳文件"hadoop fs -put $1 $2
}function import_data(){
# $*: 获取所有参数,如果使用""包裹之后,$*当做整体
# $#: 获取参数个数
# $@: 获取所有参数,如果使用""包裹之后,把每个参数当做单独的个体
# $?: 获取上一个指令的结果tableNames=$*sql="use hr_cn;"#遍历所有表,拼接每个表的数据加载sql语句for table in $tableNamesdosql="${sql}load data inpath '/origin_data/hr_cn/db/${table:4}/${datestr}/*' overwrite into table ${table} partition (dt='$datestr');"done#执行sql/opt/module/hive/bin/hive -e "$sql"
}case $1 in
"all")handle_target_and_put /opt/module/data/txt/${datestr}/ /origin_data/hr_cn/db/recruitment_info_full/import_data "ods_recruitment_info_full";;
"recruitment_info")handle_target_and_put /opt/module/data/txt/${datestr}/ /origin_data/hr_cn/db/recruitment_info_full/import_data "ods_recruitment_info_full";;
esac
http://www.yayakq.cn/news/693139/

相关文章:

  • 郑州网站建设氵汉狮网络用wordpress搭建完整网站教程
  • 西青网站建设自媒体哪里培训
  • 中国搜索网站提交入口wordpress标签页模板
  • 传奇网站怎么制作教程包头建站
  • 怎么做网站服务器网络优化包括
  • 网站优化平台网络营销方案分析整理
  • 做微商建自己的网站有用吗產品定制网站开发
  • 网站建设费用多少钱百度高级搜索引擎入口
  • 公司网站建设需要的材料wordpress私密文章权限设置
  • 百度权重高的网站产品营销推广方式
  • 网站运营wordpress照片墙插件
  • php网站框架网站参数错误怎么解决方法
  • 江北网站建设价格南京制作网站公司哪家好
  • 在线股票交易网站开发怎么做代刷网站教程
  • 如何给网站建设提意见所有网站302跳转百度
  • 网站实现微信登录美食网站建设合同范例
  • 合肥网站建设需要多网站分析报告
  • 可以做微课ppt模板 网站有哪些内容网站建设公司十年乐云seo
  • 网站建设php论文青海省建设厅网站职称评审表
  • iis 网站301重定向公司手机app开发公司
  • 黑链 对网站的影响下载并安装app
  • 杭州网站建设慕枫宿迁建设局质安站网站
  • 移动应用开发公司网站模板鹤壁市网站建设
  • 在线做初中题网站手机百度网址大全首页
  • 制作图片网站网站要放备案号吗
  • 手机代码网站有哪些问题吗软文营销经典案例
  • 最新网站域名ip地址查询马上飞做的一些网站
  • 网站标题栏怎么做网站怎么续费
  • seo网站代码优化做网站是找什么人
  • 企业网站建设规划书的内容企业宣传册模板下载