我正在使用Spark 2.1从Java中的Cassandra读取数据。
我尝试了在https://stackoverflow.com/a/39890996/1151472中发布的代码(使用SparkSession),它可以工作。但是,当我用spark.sql()一替换spark.read()方法时,抛出以下异常:

Exception in thread "main" org.apache.spark.sql.AnalysisException: Table or view not found: `wiki`.`treated_article`; line 1 pos 14;
'Project [*]
+- 'UnresolvedRelation `wiki`.`treated_article`

    at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)


我对read和sql方法使用相同的spark配置

read()代码:
数据集数据集=

spark.read().format("org.apache.spark.sql.cassandra")
                .options(new HashMap<String, String>() {
                    {
                        put("keyspace", "wiki");
                        put("table", "treated_article");
                    }
                }).load();


sql()代码:

spark.sql("SELECT * FROM WIKI.TREATED_ARTICLE");

最佳答案

Spark Sql使用Catalogue查找数据库和表引用。当您输入不在目录中的表标识符时,它将引发类似于您发布的表的错误。 read命令不需要目录,因为您需要在调用中指定所有相关信息。

您可以通过以下方式将条目添加到目录中

将数据集注册为视图

首先创建您的数据集

spark.read().format("org.apache.spark.sql.cassandra")
                .options(new HashMap<String, String>() {
                    {
                        put("keyspace", "wiki");
                        put("table", "treated_article");
                    }
                }).load();


然后使用目录注册表功能之一

void    createGlobalTempView(String viewName)
Creates a global temporary view using the given name.
void    createOrReplaceTempView(String viewName)
Creates a local temporary view using the given name.
void    createTempView(String viewName)
Creates a local temporary view using the given name


或使用SQL Create语句

   CREATE TEMPORARY VIEW words
     USING org.apache.spark.sql.cassandra
     OPTIONS (
       table "words",
       keyspace "test",
       cluster "Test Cluster",
       pushdown "true"
     )


通过以上两种方法之一添加到目录后,您都可以在该上下文发出的所有sql调用中引用该表。





CREATE TEMPORARY VIEW words
  USING org.apache.spark.sql.cassandra
  OPTIONS (
    table "words",
    keyspace "test"
  );

SELECT * FROM words;
// Hello    1
// World    2




Datastax(我的雇主)企业版软件通过将条目放置在Spark用作目录的Hive Metastore中来自动注册所有Cassandra表。这使得无需手动注册即可访问所有表。

此方法允许在不附带CREATE VIEW的情况下使用select语句

08-04 04:45
查看更多