0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python & Spark でdashDBからデータを読み込んだメモ

Last updated at Posted at 2016-11-16

IBM Data Scientist Experience の Python2 with Spark1.6にて実行
(1)クレデンシャル(資格情報)を定義
あらかじめDSXに自分のdashDBを登録しておいたので、"insert to code"をクリックして設定。(ユーザー名とパスワードは伏字にして..ます)
Screen Shot 2016-11-16 at 17.46.16.png

credentials_2 = {
  'port':'50000',
  'db':'BLUDB',
  'username':'dashXXXXX',
  'ssljdbcurl':'jdbc:db2://dashdb-entry-yp-dal09-07.services.dal.bluemix.net:50001/BLUDB:sslConnection=true;',
  'host':'dashdb-entry-yp-dal09-07.services.dal.bluemix.net',
  'https_url':'https://dashdb-entry-yp-dal09-07.services.dal.bluemix.net:8443',
  'dsn':'DATABASE=BLUDB;HOSTNAME=dashdb-entry-yp-dal09-07.services.dal.bluemix.net;PORT=50000;PROTOCOL=TCPIP;UID=dashXXXX;PWD=XXXXXXXXXXX;',
  'hostname':'dashdb-entry-yp-dal09-07.services.dal.bluemix.net',
  'jdbcurl':'jdbc:db2://dashdb-entry-yp-dal09-07.services.dal.bluemix.net:50000/BLUDB',
  'ssldsn':'DATABASE=BLUDB;HOSTNAME=dashdb-entry-yp-dal09-07.services.dal.bluemix.net;PORT=50001;PROTOCOL=TCPIP;UID=dash7836;PWD=82f9d02e61da;Security=SSL;',
  'uri':'db2://dashXXXX:XXXXXXXXXXX@dashdb-entry-yp-dal09-07.services.dal.bluemix.net:50000/BLUDB',
  'password':"""XXXXXXXXXXXXX"""
}

(2) データ取得用の関数を定義
参考にしたサイト ---> http://stackoverflow.com/questions/37688993/how-to-use-pandas-on-spark-notebook-data-on-dashdb-in-python

def getDashData(credentials,schemaName , tableName):
    from pyspark.sql import SQLContext
    sqlContext = SQLContext(sc)
    props = {}
    props['user'] = credentials['username']
    props['password'] = credentials['password']
    table = schemaName + '.' + tableName     
    return sqlContext.read.jdbc(credentials['jdbcurl'],table,properties=props)

(3) dashDBからデータを取り出し& 先頭10レコードを確認

df_dash = getDashData(credentials_2 , 'DASH7836', 'TEST1' )
df_dash.toPandas().head(10)
Screen Shot 2016-11-16 at 17.56.05.png
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?