我在hdfs有Hive仓库hdfs://localhost:8020/用户/Hive/仓库。我在hdfs中有一个数据库mydb,比如hdfs://localhost:8020/user/hive/warehouse/mydb.db如何使用pyspark创建表并向其中插入数据请建议
gv8xihay1#
使用配置单元上下文,您将能够在配置单元中创建表,请参阅下面的代码来实现这一点。
import findsparkfindspark.init()import pysparkfrom pyspark.sql import HiveContext//hivecontextsqlCtx= HiveContext(sc)//Loading a csv file into dataframespark_df = sqlCtx.read.format('com.databricks.spark.csv').options(header='true', inferschema='true').load("./data/documents_topics.csv")//registering temp tablespark_df.registerTempTable("TABLE_Y")//Creating table out of an existing temp created from data frame tablesqlCtx.sql("CREATE TABLE TABLE_X AS SELECT * from TABLE_Y")//creating a brand new table in HivesqlCtx.sql("CREATE TABLE SomeSchema.TABLE_X (customername string, id string, ts timestamp) STORED AS DESIREDFORMAT")
import findspark
findspark.init()
import pyspark
from pyspark.sql import HiveContext
//hivecontext
sqlCtx= HiveContext(sc)
//Loading a csv file into dataframe
spark_df = sqlCtx.read.format('com.databricks.spark.csv').options(header='true', inferschema='true').load("./data/documents_topics.csv")
//registering temp table
spark_df.registerTempTable("TABLE_Y")
//Creating table out of an existing temp created from data frame table
sqlCtx.sql("CREATE TABLE TABLE_X AS SELECT * from TABLE_Y")
//creating a brand new table in Hive
sqlCtx.sql("CREATE TABLE SomeSchema.TABLE_X (customername string, id string, ts timestamp) STORED AS DESIREDFORMAT")
希望你能理解代码中的注解,如果遇到问题请告诉我。
1条答案
按热度按时间gv8xihay1#
使用配置单元上下文,您将能够在配置单元中创建表,请参阅下面的代码来实现这一点。
希望你能理解代码中的注解,如果遇到问题请告诉我。