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

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

问题描述

我知道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

< img src =https://i.stack.imgur.com/OITq5.pngalt =test table>

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查询无法做到这一点。但是有一个名为 zipWithIndex RDD 函数。您可以将 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 String Indexer

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

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

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