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

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

注意的点

(1)先判断写入的txt文件是否存在,如果不存在就需要创建路径

(2)如果txt文件已经存在,那么先将对应的文件进行删除后再写入txt数据

(3)excel文件中有可能第一行是字段名,需要跳过

(版本1 :本地版本)

1.利用python将excel转成txt文件

bash 复制代码
from datetime import datetime, timedelta
import os
import pytz
import pandas as pd

def excel_to_txt(name, date):
    # Read Excel file into a DataFrame
    df = pd.read_excel(f'data/excel/{name}.xlsx', header=None, skiprows=1)

    # Define output directory and path
    output_directory = os.path.join('data', 'txt', date)
    os.makedirs(output_directory, exist_ok=True)  # Create directory if it doesn't exist
    output_path = os.path.join(output_directory, f'{name}.txt')

    # Check if the file already exists, if so, remove it
    if os.path.exists(output_path):
        os.remove(output_path)
        print(f'Existing file {output_path} removed.')

    # Write DataFrame to a new text file
    print('开始写入txt文件')
    df.to_csv(output_path, header=None, sep='\t', index=False)
    print('文件写入成功!')
    return output_path



if __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中创建表

bash 复制代码
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 textfile
    LOCATION '/warehouse/ticket/ods/test_text';

4.将hdfs数据写入hive

bash 复制代码
load data inpath '/origin_data/test.txt' overwrite into table ticket.test_text;

(2)服务器版本

先把excel_to_txt脚本上传到服务器

excel_to_txt.py

bash 复制代码
from datetime import datetime, timedelta
import os
import pytz
import pandas as pd

def excel_to_txt(name, date):
    # Read Excel file into a DataFrame
    df = 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 path
    output_directory = os.path.join('/opt/module/data', 'txt', date)
    os.makedirs(output_directory, exist_ok=True)  # Create directory if it doesn't exist
    output_path = os.path.join(output_directory, f'{name}.txt')

    # Check if the file already exists, if so, remove it
    if os.path.exists(output_path):
        os.remove(output_path)
        print(f'Existing file {output_path} removed.')

    # Write DataFrame to a new text file
    print('开始写入txt文件')
    df.to_csv(output_path, header=None, sep='\t', index=False)
    print('文件写入成功!')
    return output_path

if __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

bash 复制代码
#!/bin/bash
/opt/module/miniconda3/bin/python  /opt/module/data/excel/excel_to_txt.py

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

recruitment_hdfs_to_ods.sh

bash 复制代码
#!/bin/bash
DATAX_HOME=/opt/module/datax

# 如果传入日期则do_date等于传入的日期,否则等于前一天日期
if [ -n "$2" ] ;then
    datestr=$2
else
    datestr=$(date -d "-1 day" +%F)
fi

# 处理目标路径,检查目标路径是否存在且不为空,如果不为空,则清空目录内容
handle_target() {
  content_size=$(hadoop fs -count $1 | awk '{print $3}')
  if [[ $content_size -ne 0 ]]; then
    echo "路径$1不为空,正在清空......"
    hadoop fs -rm -r -f $1/*
  fi
}

# 整合处理目标路径和上传文件的逻辑
handle_target_and_put() {
  handle_target $2
  echo "上傳文件"
  hadoop fs -put $1 $2
}


function import_data(){
# $*: 获取所有参数,如果使用""包裹之后,$*当做整体
# $#: 获取参数个数
# $@: 获取所有参数,如果使用""包裹之后,把每个参数当做单独的个体
# $?: 获取上一个指令的结果
	tableNames=$*
	sql="use hr_cn;"
	#遍历所有表,拼接每个表的数据加载sql语句
	for table in $tableNames
	do
		sql="${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
相关推荐
Envyᥫᩣ23 分钟前
Python中的自然语言处理:从基础到高级
python·自然语言处理·easyui
哪 吒24 分钟前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od
我是陈泽27 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
hakesashou27 分钟前
python全栈开发是什么?
python
创作小达人1 小时前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
ZPC82101 小时前
Python使用matplotlib绘制图形大全(曲线图、条形图、饼图等)
开发语言·python·matplotlib
镜花照无眠1 小时前
Python爬虫使用实例-mdrama
开发语言·爬虫·python
aaasssdddd961 小时前
python和c
c语言·开发语言·python
爱写代码的小朋友1 小时前
Python的几个高级特性
python
Eric.Lee20212 小时前
数据集-目标检测系列- 螃蟹 检测数据集 crab >> DataBall
python·深度学习·算法·目标检测·计算机视觉·数据集·螃蟹检测