在Datalab中查询Hive表时出现问题

时间:2019-05-02 01:59:10

标签: hive google-cloud-dataproc google-cloud-datalab

我已经创建了一个具有更新的init操作的dataproc集群,以安装datalab。

一切正常,除了当我从Datalab笔记本查询Hive表时,我遇到了

hc.sql(“””select * from invoices limit 10”””)

"java.lang.ClassNotFoundException: Class com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem not found" exception

创建集群

gcloud beta dataproc clusters create ds-cluster \
--project my-exercise-project \
--region us-west1 \
--zone us-west1-b \
--bucket dataproc-datalab \
--scopes cloud-platform  \
--num-workers 2  \
--enable-component-gateway  \
--initialization-actions gs://dataproc_mybucket/datalab-updated.sh,gs://dataproc-initialization-actions/connectors/connectors.sh  \
--metadata 'CONDA_PACKAGES="python==3.5"'  \
--metadata gcs-connector-version=1.9.11  

datalab-updated.sh

  -v "${DATALAB_DIR}:/content/datalab" ${VOLUME_FLAGS} datalab-pyspark; then
    mkdir -p ${HOME}/datalab
    gcloud source repos clone datalab-notebooks ${HOME}/datalab/notebooks

在datalab笔记本中

from pyspark.sql import HiveContext
hc=HiveContext(sc)
hc.sql("""show tables in default""").show()
hc.sql(“””CREATE EXTERNAL TABLE IF NOT EXISTS INVOICES
      (SubmissionDate DATE, TransactionAmount DOUBLE, TransactionType STRING)
      STORED AS PARQUET
      LOCATION 'gs://my-exercise-project-ds-team/datasets/invoices’”””)
hc.sql(“””select * from invoices limit 10”””)

更新

spark._jsc.hadoopConfiguration().set('fs.gs.impl', 'com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem')
spark._jsc.hadoopConfiguration().set('fs.gs.auth.service.account.enable', 'true')
spark._jsc.hadoopConfiguration().set('google.cloud.auth.service.account.json.keyfile', "~/Downloads/my-exercise-project-f47054fc6fd8.json")

更新2(datalab-updated.sh)

function run_datalab(){
  if docker run -d --restart always --net=host  \
      -v "${DATALAB_DIR}:/content/datalab" ${VOLUME_FLAGS} datalab-pyspark; then
    mkdir -p ${HOME}/datalab
    gcloud source repos clone datalab-notebooks ${HOME}/datalab/notebooks
    echo 'Cloud Datalab Jupyter server successfully deployed.'
  else
    err 'Failed to run Cloud Datalab'
  fi
}

2 个答案:

答案 0 :(得分:3)

您应使用Datalab initialization action在Dataproc集群上安装Datalab:

gcloud dataproc clusters create ${CLUSTER} \
    --image-version=1.3 \
    --scopes cloud-platform \
    --initialization-actions=gs://dataproc-initialization-actions/datalab/datalab.sh

此Hive与GCS一起在Datalab中开箱后使用:

from pyspark.sql import HiveContext
hc=HiveContext(sc)
hc.sql("""SHOW TABLES IN default""").show()

输出:

+--------+---------+-----------+
|database|tableName|isTemporary|
+--------+---------+-----------+
+--------+---------+-----------+

在Datalab中使用Hive在GCS上创建外部表:

hc.sql("""CREATE EXTERNAL TABLE IF NOT EXISTS INVOICES
      (SubmissionDate DATE, TransactionAmount DOUBLE, TransactionType STRING)
      STORED AS PARQUET
      LOCATION 'gs://<BUCKET>/datasets/invoices'""")

输出:

DataFrame[]

在Datalab中使用Hive查询GCS表:

hc.sql("""SELECT * FROM invoices LIMIT 10""")

输出:

DataFrame[SubmissionDate: date, TransactionAmount: double, TransactionType: string]

答案 1 :(得分:0)

如果要在数据实验室中使用Hive,则必须启用Hive Metastore

--properties hive:hive.metastore.warehouse.dir=gs://$PROJECT-warehouse/datasets \
--metadata "hive-metastore-instance=$PROJECT:$REGION:hive-metastore"

您的情况将是


--properties hive:hive.metastore.warehouse.dir=gs://$PROJECT-warehouse/datasets \
--metadata "hive-metastore-instance=$PROJECT:$REGION:hive-metastore"

hc.sql(“””CREATE EXTERNAL TABLE IF NOT EXISTS INVOICES
      (SubmissionDate DATE, TransactionAmount DOUBLE, TransactionType STRING)
      STORED AS PARQUET
      LOCATION 'gs://$PROJECT-warehouse/datasets/invoices’”””)

并确保添加以下设置以启用GCS

sc._jsc.hadoopConfiguration().set('fs.gs.impl', 'com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem')

# This is required if you are using service account and set true, 
sc._jsc.hadoopConfiguration().set('fs.gs.auth.service.account.enable', 'false')
sc._jsc.hadoopConfiguration().set('google.cloud.auth.service.account.json.keyfile', "/path/to/keyfile")

# Following are required if you are using oAuth
sc._jsc.hadoopConfiguration().set('fs.gs.auth.client.id', 'YOUR_OAUTH_CLIENT_ID')
sc._jsc.hadoopConfiguration().set('fs.gs.auth.client.secret', 'OAUTH_SECRET')