hadoop apache-spark hive apache-spark-sql

hadoop - ¿Cómo conectarse a un Hive metastore programáticamente en SparkSQL?



apache-spark apache-spark-sql (5)

Estoy usando HiveContext con SparkSQL y estoy tratando de conectarme a un Hive metastore remoto, la única forma de configurar el hive metastore es incluir hive-site.xml en el classpath (o copiarlo a / etc / spark / conf /).

¿Hay alguna manera de establecer este parámetro mediante programación en un código java sin incluir hive-site.xml? Si es así, ¿cuál es la configuración de Spark para usar?


El siguiente código funcionó para mí. Podemos ignorar la configuración de hive.metastore.uris para hive.metastore.uris local, spark creará objetos de colmena en el directorio de almacén de repuesto localmente.

import org.apache.spark.sql.SparkSession; object spark_hive_support1 { def main (args: Array[String]) { val spark = SparkSession .builder() .master("yarn") .appName("Test Hive Support") //.config("hive.metastore.uris", "jdbc:mysql://localhost/metastore") .enableHiveSupport .getOrCreate(); import spark.implicits._ val testdf = Seq(("Word1", 1), ("Word4", 4), ("Word8", 8)).toDF; testdf.show; testdf.write.mode("overwrite").saveAsTable("WordCount"); } }


En spark 2.0. + Debería verse así:

No olvide reemplazar el "hive.metastore.uris" por el suyo. Esto supone que ya tiene un servicio de metastore de colmena iniciado (no un servidor de colmena).

val spark = SparkSession .builder() .appName("interfacing spark sql to hive metastore without configuration file") .config("hive.metastore.uris", "thrift://localhost:9083") // replace with your hivemetastore service''s thrift url .enableHiveSupport() // don''t forget to enable hive support .getOrCreate() import spark.implicits._ import spark.sql // create an arbitrary frame val frame = Seq(("one", 1), ("two", 2), ("three", 3)).toDF("word", "count") // see the frame created frame.show() /** * +-----+-----+ * | word|count| * +-----+-----+ * | one| 1| * | two| 2| * |three| 3| * +-----+-----+ */ // write the frame frame.write.mode("overwrite").saveAsTable("t4")


Para Spark 1.x, puede configurar con:

System.setProperty("hive.metastore.uris", "thrift://METASTORE:9083"); final SparkConf conf = new SparkConf(); SparkContext sc = new SparkContext(conf); HiveContext hiveContext = new HiveContext(sc);

O

final SparkConf conf = new SparkConf(); SparkContext sc = new SparkContext(conf); HiveContext hiveContext = new HiveContext(sc); hiveContext.setConf("hive.metastore.uris", "thrift://METASTORE:9083");

Actualice si su colmena está kerberizada :

Intente configurarlos antes de crear HiveContext:

System.setProperty("hive.metastore.sasl.enabled", "true"); System.setProperty("hive.security.authorization.enabled", "false"); System.setProperty("hive.metastore.kerberos.principal", hivePrincipal); System.setProperty("hive.metastore.execute.setugi", "true");


Versión Spark: 2.0.2

Versión de la colmena: 1.2.1

A continuación, el código Java funcionó para mí para conectarme a Hive metastore desde Spark:

import org.apache.spark.sql.SparkSession; public class SparkHiveTest { public static void main(String[] args) { SparkSession spark = SparkSession .builder() .appName("Java Spark Hive Example") .config("spark.master", "local") .config("hive.metastore.uris", "thrift://maxiqtesting123.com:9083") .config("spark.sql.warehouse.dir", "/apps/hive/warehouse") .enableHiveSupport() .getOrCreate(); spark.sql("SELECT * FROM default.survey_data limit 5").show(); } }


Yo también enfrenté el mismo problema, pero lo resolví. Simplemente siga estos pasos en la versión Spark 2.0

Paso 1: Copie el archivo hive-site.xml de la carpeta conf de Hive a spark conf.

Paso 2: edite el archivo spark-env.sh y configure su controlador mysql. (Si está utilizando Mysql como una colmena metastore).

O agregue controladores MySQL a Maven / SBT (si los usa)

Paso 3: Cuando esté creando sesión de chispa, agregue enableHiveSupport ()

val spark = SparkSession.builder.master ("local"). appName ("testing") .enableHiveSupport () .getOrCreate ()

Código de muestra:

package sparkSQL /** * Created by venuk on 7/12/16. */ import org.apache.spark.sql.SparkSession object hivetable { def main(args: Array[String]): Unit = { val spark = SparkSession.builder.master("local[*]").appName("hivetable").enableHiveSupport().getOrCreate() spark.sql("create table hivetab (name string, age int, location string) row format delimited fields terminated by '','' stored as textfile") spark.sql("load data local inpath ''/home/hadoop/Desktop/asl'' into table hivetab").show() val x = spark.sql("select * from hivetab") x.write.saveAsTable("hivetab") } }

Salida: