【问题标题】:Find a string and extract values from result of hive query using shell script?使用 shell 脚本从 hive 查询结果中查找字符串并提取值?
【发布时间】:2020-08-04 19:06:32
【问题描述】:

问题类似于: Find and Extract value after specific String from a file using bash shell script?

我正在从 shell 脚本执行 hive 查询,需要在变量中提取一些值,查询如下:

sql="show create table dev.emp"
partition_col= `beeline -u $Beeline_URL -e $sql` | grep 'PARTITIONED BY' | cut -d "'" -f2`

sql查询的输出如下:

+----------------------------------------------------+
|                   createtab_stmt                   |
+----------------------------------------------------+
| CREATE EXTERNAL TABLE `dv.par_kst`(                |
|   `col1` string,                                   |
|   `col2` string,                                  |
|   `col3` string)                                  |
| PARTITIONED BY (                                   |
|   `part_col1` int,                                 |
|   `part_col2` int)                                 |
| ROW FORMAT SERDE                                   |
|   'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'  |
| STORED AS INPUTFORMAT                              |
|   'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'  |
| OUTPUTFORMAT                                       |
|   'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat' |
| LOCATION                                           |
|   'hdfs://nameservicets1/dv/hdfsdata/par_kst' |
| TBLPROPERTIES (                                    |
|   'spark.sql.create.version'='2.2 or prior',       |
|   'spark.sql.sources.schema.numPartCols'='2',      |
|   'spark.sql.sources.schema.numParts'='1',         |
|   'spark.sql.sources.schema.part.0'='{"type":"struct","fields":[{"name":"col1","type":"string","nullable":true,"metadata":{}},{"name":"col2","type":"string","nullable":true,"metadata":{}},{"name":"col3","type":"integer","nullable":true,"metadata":{}},{"name":"part_col2","type":"integer","nullable":true,"metadata":{}}]}',  |
|   'spark.sql.sources.schema.partCol.0'='part_col1', |
|   'spark.sql.sources.schema.partCol.1'='part_col2', |
|   'transient_lastDdlTime'='1587487456')            |
+----------------------------------------------------+

从上面的 sql 中,我想提取 PARTITIONED BY details。

Desired output :

part_col1 , part_col2

尝试使用以下代码但没有得到正确的值:

partition_col=`beeline -u $Beeline_URL -e $sql` | grep 'PARTITIONED BY' | cut -d "'" -f2`

这些 PARTITIONED BY 不是固定的,意味着对于其他一些文件它可能包含 3 个或更多,所以我想提取所有 PARTITIONED BY。

PARTITIONED BY 和 ROW FORMAT SERDE 之间的所有值,删除空格“`”和数据类型!

【问题讨论】:

    标签: shell unix hive sh


    【解决方案1】:

    使用 sed

    sed -n  '/PARTITIONED BY/,/ROW FORMAT SERD/p' file.txt | sed  '1d; $d' |  sed  -E 's/.*(`.*`).*/\1/g' |  tr -d '`' | tr '\n' ','
    

    演示:

    $sed -n  '/PARTITIONED BY/,/ROW FORMAT SERD/p' file.txt | sed  '1d; $d' |  sed  -E 's/.*(`.*`).*/\1/g' |  tr -d '`'  | tr '\n' ','
    part_col1,part_col2,$
    $
    
    

    解释:

    sed -n '/PARTITIONED BY/,/ROW FORMAT SERD/p'

    sed '1d; $d'

    sed -E 's/.*(.*).*/\1/g' 之间打印字符串

    tr -d ''`

    tr '\n' ',' ,替换新行

    【讨论】:

    • 非常感谢您的详细解释,但我在 file.txt 中没有这些值,但我是从配置单元查询生成的,因此变量具有这些值而不是 file.txt ,所以这也适用于变量?
    • 是的。在你的代码中把它放在你使用过 grep 的地方
    • partition_col=$(beeline -u $Beeline_URL -e $sql) | sed -n '/PARTITIONED BY/,/ROW FORMAT SERD/p' | sed '1d; $d' | sed -E 's/.*(.*).*/\1/g' | tr -d '' | tr '\n' ','`
    【解决方案2】:

    你可以使用awk:

    /PARTITIONED BY \(/  {partitioned_by = 1; next}
    /ROW FORMAT SERDE/  {partitioned_by = 0; next}
    partitioned_by == 1 {a[n++] = substr($2, 2, length($2) - 2)}
    END { for (i in a) printf "%s, ", i}
    

    将上述内容存储在名为beeline.awk 的文件中并执行:

    partition_col=`beeline -u $Beeline_URL -e $sql` | awk -f beeline.awk
    

    【讨论】:

    • 你好,是的,我也是这样做的,但是查询结果是 0,1,
    • 我尝试将查询结果保存在一个文件中并尝试在下面运行:/PARTITIONED BY (/ {partitioned_by = 1; next} /ROW FORMAT SERDE/ {partitioned_by = 0; next}partitioned_by == 1 {a[n++] = substr($2, 2, length($2) - 2)} END { for (i in a) printf "%s, ", i} "file.txt"
    猜你喜欢
    • 2020-08-04
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多