Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

lkexport.js 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  1. const fs = require('fs');
  2. const zlib = require('zlib');
  3. const pg = require('pg');
  4. const QueryStream = require('pg-query-stream');
  5. const Q = require('q');
  6. const csv = require('fast-csv');
  7. const queries = {
  8. amazon: 'SELECT user_id, book_id, rating FROM az_export_ratings',
  9. 'bx-explicit': 'SELECT user_id, book_id, rating FROM bx_explicit_ratings',
  10. 'bx-all': 'SELECT user_id, book_id, rating FROM bx_all_ratings'
  11. };
  12. function exportRatingTriples(type, outFile) {
  13. let rq = queries[type];
  14. if (!rq) {
  15. return Q.reject(`invalid data type ${type}`);
  16. }
  17. var out = fs.createWriteStream(outFile, 'utf8');
  18. var query = new QueryStream(rq);
  19. pg.connect(null, (err, client, done) => {
  20. if (err) {
  21. out.emit('error', err);
  22. out.close();
  23. throw err;
  24. }
  25. var qstr = client.query(query);
  26. qstr.pipe(csv.createWriteStream({headers: true})
  27. .transform((row) => {
  28. return {
  29. userID: row.user_id,
  30. bookID: row.book_id,
  31. rating: row.rating
  32. };
  33. }))
  34. .pipe(out)
  35. .on('error', done)
  36. .on('finish', () => done());
  37. });
  38. return out;
  39. }
  40. module.exports = {
  41. amazon: exportRatingTriples.bind(null, 'amazon'),
  42. bxExplicit: exportRatingTriples.bind(null, 'bx-explicit'),
  43. bxAll: exportRatingTriples.bind(null, 'bx-all')
  44. };
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...