首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Spark中高效地抓取多个表的列?

如何在Spark中高效地抓取多个表的列?
EN

Stack Overflow用户
提问于 2018-08-01 02:02:46
回答 2查看 35关注 0票数 1

我想要查找某些Hive表中满足特定条件的所有列。然而,我写的代码非常慢,因为Spark并不是一个特别喜欢循环的人:

matches = {}
for table in table_list:
    matching_cols = [c for c in spark.read.table(table).columns if substring in c]
    if matching_cols:
        matches[table] = matching_cols

我想要这样的东西:

matches = {'table1': ['column1', 'column2'], 'table2': ['column2']}

我怎样才能更有效地达到同样的效果?

EN

回答 2

Stack Overflow用户

发布于 2018-08-01 02:59:18

一位同事刚刚发现了这个问题。这是修改后的解决方案:

matches = {}
for table in table_list:
    matching_cols = spark.sql("describe {}".format(table)) \
                         .where(col('col_name').rlike(substring)) \
                         .collect()

    if matching_cols:
        matches[table] = [c.col_name for c in matching_cols]

这里的关键区别在于,在我之前的示例中,Spark似乎正在缓存分区信息,因此它在每个循环中变得越来越停滞。访问元数据来抓取列,而不是表本身,可以绕过这个问题。

票数 1
EN

Stack Overflow用户

发布于 2018-08-01 08:45:18

如果表字段有注释上面的代码将进入额外的信息(注释)的问题,也侧记HBase链接表也将被发布…

示例:

create TABLE deck_test (
COLOR string COMMENT 'COLOR Address',
SUIT string COMMENT '4 type Suits',
PIP string)
ROW FORMAT DELIMITED FIELDS TERMINATED by '|'
STORED AS TEXTFILE;

describe deck_test;
color                   string                  COLOR Address
suit                    string                  4 type Suits
pip                     string

处理评论问题的小改动可能会有所帮助……

matches = {}
for table in table_list:
    matching_cols = spark.sql("show columns in {}".format(table)).where(col('result').rlike(substring)).collect()
    if matching_cols:
        matches[table] = [c.col_name for c in matching_cols]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51619464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档