Guest User

Untitled

a guest
Oct 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. const { pipe, transform } = require('bluestream')
  2. const mysql = require('mysql')
  3. const { createWriteStream } = require('fs')
  4. const connection = mysql.createConnection({
  5. socketPath: '/var/run/mysqld/mysqld.sock',
  6. user: 'root',
  7. password: 'root',
  8. dateStrings: true,
  9. })
  10.  
  11.  
  12. const asyncQuery = (...args) => {
  13. return new Promise((resolve, reject) => {
  14. connection.query(...args, (error, data) => (error ? reject(error) : resolve(data)))
  15. })
  16. }
  17.  
  18. async function getTableIds() {
  19. const rows = await asyncQuery('SELECT table_schema, table_name FROM information_schema.tables')
  20. return rows
  21. .filter(({ table_schema: db }) => db !== 'test' && db !== 'vividcortex' && db !== 'information_schema')
  22. .map(({ table_schema: db, table_name: tableName }) => `${db}.${tableName}`)
  23. }
  24.  
  25. async function writeTableToFile(tableID) {
  26. console.log(`Loading ${tableID}`)
  27. const query = connection.query(`SELECT * from ${tableID}`).stream({ highWaterMark: 100 })
  28. const serialize = transform(row => JSON.stringify(row) + '\n')
  29. const file = createWriteStream(`./${tableID}.ndjson`)
  30. await pipe(
  31. query,
  32. serialize,
  33. file
  34. )
  35. console.log(`Done loading ${tableID}`)
  36. }
  37.  
  38. async function run() {
  39. connection.connect()
  40. const tableIds = await getTableIds()
  41. for (const tableId of tableIds) {
  42. await writeTableToFile(tableId)
  43. }
  44. connection.end()
  45. }
  46.  
  47. run()
Add Comment
Please, Sign In to add comment