【问题标题】:how to load csv file based on header data using sqlldr如何使用 sqlldr 基于标头数据加载 csv 文件
【发布时间】:2019-12-04 07:41:43
【问题描述】:

我有一个.csv 文件,其中第一行始终包含header 信息。但是标题列的位置不固定。例如,

file 1 has below mentioned data

Name,ID,Mailing Details

X,1,US

file 2 has below mentioned data

Mailing Details,Name,ID

UK,Y,10

如果我们看到列名被互换,我无法定义静态控制文件。 那么任何人都可以帮助我,例如如何编写我的控制文件来搜索标题列名称并将其放入表中。

my table structure is

ID,Name,Mailing_Details

非常感谢。

【问题讨论】:

  • 我们查看了多少这样的 .csv 文件?
  • 一天 50 多个文件
  • 有多少列组合?

标签: sql oracle csv sql-loader


【解决方案1】:

假设这 3 列和 6 个组合; shell 脚本中的 elsif 阶梯可用于在此处构建问题

希望这将有助于达到您想要的要求

for file in 1.txt

do

f1=`head "${file}" -n1 | tr A-Z a-z | sed 's/,//g'`


if [ $f1 == "idnamemailing_details" ] 
then
        echo "logic 1 -> ${file}"
        echo "
        load data 
        infile '${file}' 
        badfile 'badfile1.csv'
        discardfile 'discard1.csv' 
        append
        into table schema.table
        fields terminated by ',' 
        ( id, name, mailing_details ) " > loader.ctl
        sleep 1
        sqlldr user/pwd@schema  control=loader.ctl 
        break

elif [ $f1 == "idmailing_detailsname" ] 
then
        echo "logic 2 -> ${file}"
        echo "
        load data 
        infile '${file}' 
        badfile 'badfile1.csv'
        discardfile 'discard1.csv' 
        append
        into table schema.table
        fields terminated by ',' 
        ( id, mailing_details,name ) " > loader.ctl
        sleep 1
        sqlldr user/pwd@schema  control=loader.ctl 
        break

elif [ $f1 == "namemailing_detailsid" ] 
then
        echo "logic 3 -> ${file}"
        echo "
        load data 
        infile '${file}' 
        badfile 'badfile1.csv'
        discardfile 'discard1.csv' 
        append
        into table schema.table
        fields terminated by ',' 
        ( name, mailing_details ,id) " > loader.ctl
        sleep 1
        sqlldr user/pwd@schema  control=loader.ctl 
        break

elif [ $f1 == "nameidmailing_details" ] 
        then
        echo "logic 4 -> ${file}"
        echo "
        load data 
        infile '${file}' 
        badfile 'badfile1.csv'
        discardfile 'discard1.csv' 
        append
        into table schema.table
        fields terminated by ',' 
        ( name, id, mailing_details ) " > loader.ctl
        sleep 1
        sqlldr user/pwd@schema  control=loader.ctl 
        break

elif [ $f1 == "mailing_detailsidname" ] 
then
        echo "logic 5 -> ${file}"
        echo "
        load data 
        infile '${file}' 
        badfile 'badfile1.csv'
        discardfile 'discard1.csv' 
        append
        into table schema.table
        fields terminated by ',' 
        ( mailing_details,id,name ) " > loader.ctl
        sleep 1
        sqlldr user/pwd@schema  control=loader.ctl 
        break

elif [ $f1 == "mailing_detailsnameid" ] 
then
        echo "logic 6 -> ${file}"
        echo "
        load data 
        infile '${file}' 
        badfile 'badfile1.csv'
        discardfile 'discard1.csv' 
        append
        into table schema.table
        fields terminated by ',' 
        ( mailing_details, name,id  ) " > loader.ctl
        sleep 1
        sqlldr user/pwd@schema  control=loader.ctl 
        break

fi
done

【讨论】:

    【解决方案2】:

    SQLLDR 无法单独执行此操作...

    我认为你有几个选择:

    1. 如果格式组合太多;使用脚本,处理第一行并动态构建控制文件。
    2. 如果您的格式数量有限;使用脚本,查看第一行并将格式与控制文件相匹配。
    3. 寻找其他解决方案...放弃 SQLLDR 并使用外部表?

    【讨论】:

      【解决方案3】:

      您可以尝试使用BOUNDFILLER or FILLER,如下所示。

      BOUNDFILLER 或 FILLER 是一个数据文件映射字段,它不 对应于数据库列。填充字段被分配了值 从它们映射到的数据字段

      .

         load data
        infile 'file1.csv'           
        infile 'file2.csv'
          append
          into table SCHEMA.TABLE
          fields terminated by ';' optionally enclosed by '"'
          trailing nullcols
          (
            source_field1     BOUNDFILLER,
            source_field2     BOUNDFILLER,
            source_field3     BOUNDFILLER,
            destination_field1 ":source_field1",
            destination_field2 ":source_field2",
            destination_field3 ":source_field3");
      

      更多详情可以查看here

      【讨论】:

      • BOUNDFILLER 的工作方式也类似于静态控制文件格式,我必须明确提及列名。所以按照我的要求,如果我改变列顺序,那么它就不起作用了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2020-02-13
      • 2014-11-17
      相关资源
      最近更新 更多