Advertisement
Guest User

Untitled

a guest
May 16th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. def write_to_mysql(df):
  2. """ append all records from a dataframe
  3. into MySQL table
  4. """
  5. df.write.jdbc(
  6. url='jdbc:mysql://host/db',
  7. table='mysql_tbl',
  8. mode='append',
  9. properties={
  10. 'user': 'mysql_user',
  11. 'password': 'mysql_pw',
  12. 'driver': 'com.mysql.jdbc.Driver'
  13. }
  14. )
  15.  
  16. def read_from_mysql():
  17. """ read all records from a MySQL table
  18. into a dataframe and return
  19. """
  20. df = hiveContext.read.format('jdbc') \
  21. .option('driver', 'com.mysql.jdbc.Driver') \
  22. .option('url', 'jdbc:mysql://host/db') \
  23. .option('dbtable', '({query}) as T'.format(query='SELECT * FROM table')) \
  24. .option('user', 'mysql_user') \
  25. .option('password', 'mysql_pw') \
  26. .load()
  27. return df
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement