如何使用SparkSQL将值插入Hive?

时间:2017-03-04 11:37:19

标签: java apache-spark hive apache-spark-sql

我正在尝试使用SparkSQL将值插入Hive表。我有值存储在列表中并尝试使用String插入。

 SparkSession spark = SparkSession
                .builder()
                .appName("Spark + Hive + sentiment Analyzer")
                .master("local[*]")
                .config("hive.metastore.uris","thrift://localhost:9083")
                .enableHiveSupport()
                .getOrCreate();
        spark.sql("create table if not exists output (srno int, values_string string)");
for(String str: nameAndCity){
            //System.out.println(str);
           str = str.replaceAll("\"","'");
             spark.sql("insert into output values(" + i +","+ "'"+ str+"'"+")");
            i++;
        }

我的字符串包含double quotessingle quotes和其他字符。我试图使double quote无效但现在使用。
我收到以下错误:

Exception in thread "main" java.lang.NullPointerException
    at spark.main(spark.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
17/03/04 16:55:50 INFO SparkContext: Invoking stop() from shutdown hook
17/03/04 16:55:50 INFO SparkUI: Stopped Spark web UI at http://10.0.0.15:4040
17/03/04 16:55:50 INFO MapOutputTrackerMasterEndpoint: MapOutputTrackerMasterEndpoint stopped!
17/03/04 16:55:50 INFO MemoryStore: MemoryStore cleared

请让我知道如何灵活运行我的代码?请建议我。
输入是这样的:

Mary let you win.
"Pleasant day to play football"
Don't worry go home now.
We ---- Will have conversation with you for sure. Relax.

还有很多其他人

0 个答案:

没有答案
相关问题