在Cloudera VM中读取教程CSV文件时的例外情况

时间:2016-05-01 12:19:17

标签: python csv hadoop pyspark cloudera-quickstart-vm

我正在尝试使用Cloudera虚拟机附带的Spark教程。但即使我使用正确的行结束编码,我也无法执行脚本,因为我遇到了大量的错误。 该教程是Coursera Introduction to Big Data Analytics课程的一部分。作业can be found here

所以这就是我的所作所为。安装IPython shell(如果尚未安装):

sudo easy_install ipython==1.2.1

打开/启动shell(使用1.2.0或1.4.0):

PYSPARK_DRIVER_PYTHON=ipython pyspark --packages com.databricks:spark-csv_2.10:1.2.0

将行尾设置为窗口样式。这是因为该文件采用windows编码,并且在课程中表示这样做。如果你不这样做,你将会遇到其他错误。

sc._jsc.hadoopConfiguration().set('textinputformat.record.delimiter','\r\n')

尝试加载CSV文件:

yelp_df = sqlCtx.load(source='com.databricks.spark.csv',header = 'true',inferSchema = 'true',path = 'file:///usr/lib/hue/apps/search/examples/collections/solr_configs_yelp_demo/index_data.csv')

但是得到一个很长的错误列表,就像这样开始:

Py4JJavaError: An error occurred while calling o23.load.: java.lang.RuntimeException: 
Unable to instantiate 
org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient at 
org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:472)

完整的错误消息can be seen here。这是/etc/hive/conf/hive-site.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>

  <!-- Hive Configuration can either be stored in this file or in the hadoop configuration files  -->
  <!-- that are implied by Hadoop setup variables.                                                -->
  <!-- Aside from Hadoop setup variables - this file is provided as a convenience so that Hive    -->
  <!-- users do not have to edit hadoop configuration files (that may be managed as a centralized -->
  <!-- resource).                                                                                 -->

  <!-- Hive Execution Parameters -->

  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://127.0.0.1/metastore?createDatabaseIfNotExist=true</value>
    <description>JDBC connect string for a JDBC metastore</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
    <description>Driver class name for a JDBC metastore</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>hive</value>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>cloudera</value>
  </property>

  <property>
    <name>hive.hwi.war.file</name>
    <value>/usr/lib/hive/lib/hive-hwi-0.8.1-cdh4.0.0.jar</value>
    <description>This is the WAR file with the jsp content for Hive Web Interface</description>
  </property>

  <property>
    <name>datanucleus.fixedDatastore</name>
    <value>true</value>
  </property>

  <property>
    <name>datanucleus.autoCreateSchema</name>
    <value>false</value>
  </property>

  <property>
    <name>hive.metastore.uris</name>
    <value>thrift://127.0.0.1:9083</value>
    <description>IP address (or fully-qualified domain name) and port of the metastore host</description>
  </property>
</configuration>

任何帮助或想法如何解决?我想这是一个非常常见的错误。但是,我找不到任何解决方案。

还有一件事:有没有办法将这么长的错误消息转储到一个单独的日志文件中?

2 个答案:

答案 0 :(得分:0)

似乎有两个问题。首先,hive-metastore在某些情况下处于脱机状态。其次,无法推断出架构。因此,我手动创建了一个模式,并在加载CSV文件时将其添加为参数。无论如何,我很想知道这是否适用于schemaInfer = true。

这是我的手动定义架构的版本。因此,请确保已启动配置单元:

sudo service hive-metastore restart

然后,查看CSV文件的第一部分以了解它的结构。我使用了这个命令行:

head /usr/lib/hue/apps/search/examples/collections/solr_configs_yelp_demo/index_data.csv

现在,打开python shell。有关如何执行此操作,请参阅原始帖子。然后定义架构:

from pyspark.sql.types import *
schema = StructType([
    StructField("business_id", StringType(), True),
    StructField("cool", IntegerType(), True),
    StructField("date", StringType(), True),
    StructField("funny", IntegerType(), True),
    StructField("id", StringType(), True),
    StructField("stars", IntegerType(), True),
    StructField("text", StringType(), True),
    StructField("type", StringType(), True),
    StructField("useful", IntegerType(), True),
    StructField("user_id", StringType(), True),
    StructField("name", StringType(), True),
    StructField("full_address", StringType(), True),
    StructField("latitude", DoubleType(), True),
    StructField("longitude", DoubleType(), True),
    StructField("neighborhood", StringType(), True),
    StructField("open", StringType(), True),
    StructField("review_count", IntegerType(), True),
    StructField("state", StringType(), True)])

然后通过指定架构加载CSV文件。请注意,无需设置窗口行结尾:

yelp_df = sqlCtx.load(source='com.databricks.spark.csv',
header = 'true',
schema = schema,
path = 'file:///usr/lib/hue/apps/search/examples/collections/solr_configs_yelp_demo/index_data.csv')

对数据集执行的任何方法的结果。我尝试了计数,这非常有效。

yelp_df.count()

感谢@yaron的帮助,我们可以弄清楚如何使用inferSchema加载CSV。首先,您必须正确设置hive-Metoreore:

sudo cp /etc/hive/conf.dist/hive-site.xml /usr/lib/spark/conf/

然后,启动Python shell并且不要将行结尾更改为Windows编码。请记住,更改是持久的(会话不变)。因此,如果您之前将其更改为Windows样式,则需要将其重置为&#39; \ n&#39;。然后加载CSV文件,并将inferSchema设置为true:

yelp_df = sqlCtx.load(source='com.databricks.spark.csv',
header = 'true',
inferSchema = 'true',
path = 'file:///usr/lib/hue/apps/search/examples/collections/solr_configs_yelp_demo/index_data.csv')

答案 1 :(得分:0)

讨论摘要: 执行以下命令解决了问题:

sudo cp /etc/hive/conf.dist/hive-site.xml /usr/lib/spark/conf/

请参阅https://www.coursera.org/learn/bigdata-analytics/supplement/tyH3p/setup-pyspark-for-dataframes了解详情。