如何在 Spark 表中创建索引? [英] How to create index in Spark Table?

查看:54
本文介绍了如何在 Spark 表中创建索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Spark Sql 和 Hive 几乎一样.

I know Spark Sql is almost same as Hive.

现在我已经创建了一个表,当我执行 Spark sql 查询来创建表索引时,它总是给我这个错误:

Now I have created a table and when I am doing Spark sql query to create the table index, it always gives me this error:

SQL 语句中的错误:AnalysisException:在创建索引语句中,不匹配的输入 '' 期望 AS 接近 ')'

Error in SQL statement: AnalysisException: mismatched input '' expecting AS near ')' in create index statement

我使用的 Spark sql 查询是:

The Spark sql query I am using is:

CREATE INDEX word_idx ON TABLE t (id)

id 的数据类型是 bigint.在此之前,我还尝试在该表的word"列上创建表索引,它给了我同样的错误.

The data type of id is bigint. Before this, I have also tried to create table index on "word" column of this table, it gave me the same error.

那么,有没有办法通过 Spark sql 查询创建索引?

So, is there anyway to create index through Spark sql query?

推荐答案

确实无法通过 Spark SQL 查询来做到这一点.但是有一个 RDD 函数叫做 zipWithIndex.您可以将 DataFrame 转换为 RDD,执行 zipWithIndex,并将生成的 RDD 转换回 DataFrame.

There's no way to do this through a Spark SQL query, really. But there's an RDD function called zipWithIndex. You can convert the DataFrame to an RDD, do zipWithIndex, and convert the resulting RDD back to a DataFrame.

请参阅此社区 Wiki 文章,了解完整的解决方案.

See this community Wiki article for a full-blown solution.

另一种方法是使用Spark MLLib 字符串索引器.

Another approach could be to use the Spark MLLib String Indexer.

这篇关于如何在 Spark 表中创建索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆