unix—如何添加分号;在每次使用shell脚本创建ddl语句之后

pbwdgjma  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(394)

我正在尝试添加分号(;)在每个create-view-hive-ddl语句之后。我有一个文件,其中包含以下ddl语句:

CREATE VIEW `db1.table1` AS SELECT * FROM db2.table1
CREATE VIEW `db1.table2` AS SELECT * FROM db2.table2
CREATE VIEW `db1.table3` AS SELECT * FROM db3.table3
CREATE EXTERNAL TABLE `db1.table4`(
  `cus_id` int,
  `ren_mt` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
TBLPROPERTIES (
  'skip.header.line.count'='1', 
  'transient_lastDdlTime'='1558705259')
CREATE EXTERNAL TABLE `sndbx_cmcx.effective_month1`(
  `customeridentifier` bigint, 
  `renewalmonth` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
TBLPROPERTIES (
  'COLUMN_STATS_ACCURATE'='false', 
  'transient_lastDdlTime'='1558713596')

我希望它看起来像下面。每个create view语句后面都有一个;每个create表之后都有一个;。。

CREATE VIEW `db1.table1` AS SELECT * FROM db2.table1;
CREATE VIEW `db1.table2` AS SELECT * FROM db2.table2;
CREATE VIEW `db1.table3` AS SELECT * FROM db3.table3;
CREATE EXTERNAL TABLE `db1.table4`(
  `cus_id` int,
  `ren_mt` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
TBLPROPERTIES (
  'skip.header.line.count'='1', 
  'transient_lastDdlTime'='1558705259');
CREATE EXTERNAL TABLE `sndbx_cmcx.effective_month1`(
  `customeridentifier` bigint, 
  `renewalmonth` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
TBLPROPERTIES (
  'COLUMN_STATS_ACCURATE'='false', 
  'transient_lastDdlTime'='1558713596');

下面是我使用的shell脚本:


# Change database before you run the script

 hiveDBName=$1;

 showcreate="show create table "
 terminate=";"
 tables=`hive -e "use $hiveDBName;show tables;"`
 tab_list=`echo "${tables}"`

  for list in $tab_list
  do
         echo "Generating table script for " #${hiveDBName}.${list}
          showcreatetable=${showcreatetable}${showcreate}${hiveDBName}.${list}${terminate}
        done

        echo " ====== Create Tables ======= : "# $showcreatetable

 #Creates a filter ddls
 hive -e "use $hiveDBName; ${showcreatetable}"> a.sql
 #Removes the Warn: from the file
 grep -v "WARN" a.sql > /home/path/my_ddls/${hiveDBName}_extract_all_tables.sql

 echo "Removing Filter File"
 #Remove Filter file
 rm -f a.sql

# Puts a ; after each create view statement in the document

sed -i '/transient/s/$/;/' "/home/path/my_ddls/${hiveDBName}_extract_all_tables.sql"

这会生成DDL,但只会将;在create table语句之后,但它不会将其放在每个create view语句之后。
有什么想法或建议吗?

gstyhher

gstyhher1#

我会采取简单的方法,利用 ; 不必与语句(结尾)在同一行,并且可能有一个空语句。这将提供:

sed -i -e '/^CREATE/i;' -e '$a;' "/home/path/my_ddls/${hiveDBName}_extract_all_tables.sql"

相关问题