Csv 使用文件中的列名在配置单元中创建表

Csv 使用文件中的列名在配置单元中创建表,csv,hive,Csv,Hive,我目前有一些配置单元代码,可以创建一个表,在其中声明所有列名和类型。然后,我使用LoadDataInPath将CSV加载到该表中,这样我就可以连接到我的数据库表中。问题在于有时CSV列的顺序可能不同。我无法控制它,因为它是从另一个来源发送给我的。我想知道是否有一种方法可以在不声明列名的情况下创建我每天使用的临时表,并允许它从CSV读取。这样我就不必每天早上手动查看文件来检查列的顺序是否正确了?因为列的顺序一直在变化,第一步可以使用shell脚本读取标题列并为临时表生成创建表脚本。然后,执行生成的

我目前有一些配置单元代码,可以创建一个表,在其中声明所有列名和类型。然后,我使用LoadDataInPath将CSV加载到该表中,这样我就可以连接到我的数据库表中。问题在于有时CSV列的顺序可能不同。我无法控制它,因为它是从另一个来源发送给我的。我想知道是否有一种方法可以在不声明列名的情况下创建我每天使用的临时表,并允许它从CSV读取。这样我就不必每天早上手动查看文件来检查列的顺序是否正确了?

因为列的顺序一直在变化,第一步可以使用shell脚本读取标题列并为临时表生成创建表脚本。然后,执行生成的CREATETABLE字符串并将文件加载到temp表中。然后,可以从临时表将其加载到目标表

这是一个bash脚本示例,可以为您提供一些想法

#!/bin/bash

#File check
if [ -s "$1" ]
then echo "File $1 exists and is non-empty"
else
echo "File $1 does not exist or is empty"
fi

create_tbl_cmd="create table tblname ("    
#Get fields from file header
fields=`head -1 $1`    
#add string as the datatype for each field
fields="${fields//,/ string,} string)"
create_table_cmd="$create_tbl_cmd$fields"
#echo $create_table_cmd

#Execute the $create_table_cmd using Beeline or Hive command line invoking the necessary command

#Execute the Load into temp table

#Execute the Insert from temp table to target table 
使用csv文件参数执行上面的bash脚本,如下所示

bash scriptname.sh filename.csv

由于列顺序不断变化,因此第一步可以使用shell脚本读取标题列并为临时表生成创建表脚本。然后,执行生成的CREATETABLE字符串并将文件加载到temp表中。然后,可以从临时表将其加载到目标表

这是一个bash脚本示例,可以为您提供一些想法

#!/bin/bash

#File check
if [ -s "$1" ]
then echo "File $1 exists and is non-empty"
else
echo "File $1 does not exist or is empty"
fi

create_tbl_cmd="create table tblname ("    
#Get fields from file header
fields=`head -1 $1`    
#add string as the datatype for each field
fields="${fields//,/ string,} string)"
create_table_cmd="$create_tbl_cmd$fields"
#echo $create_table_cmd

#Execute the $create_table_cmd using Beeline or Hive command line invoking the necessary command

#Execute the Load into temp table

#Execute the Insert from temp table to target table 
使用csv文件参数执行上面的bash脚本,如下所示

bash scriptname.sh filename.csv

目前,我们有一个用于访问和运行查询的终端,它类似于microsoft SQL或TD SQL。所以我不使用任何命令行或类似的东西来运行查询。在这些结构中,或者只在Putty中运行shell脚本,这样做是否可行我在需要时用于访问服务器的界面当前,我们有一个用于访问和运行查询的终端,它类似于microsoft SQL或TD SQL。所以我不使用任何命令行或类似的东西来运行查询。这样的工作是在这些构造中进行,还是只在Putty中运行一个shell脚本我在需要时用来访问服务器的接口