MySQL数据需要写入JSON文件 [英] Mysql data need to be written to a json file

查看:102
本文介绍了MySQL数据需要写入JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从mysql数据库读取数据并将其写入JSON文件.我是node.js的新手,任何帮助将不胜感激.

I am trying to read data from a mysql database and write it to a JSON file. I am pretty new to node.js and any help would be greatly appreciated.

我写的代码:

var mysql      = require('mysql');
var fs = require('fs');
var configFile = process.cwd() + "\\config.json";
var configFileContents = fs.readFileSync(configFile, 'utf8');
var config = JSON.parse(configFileContents);
//config = config[process.env.BLENV];
console.log(config.PROD.HTTP);

var connection = mysql.createConnection({
  host     : config.QA.SQLHostName,
  user     : config.QA.SQLUserName,
  password : config.QA.SQLPassword,
  database : config.QA.SQLDatabase,
  multipleStatements: true
});

connection.connect();

var timeInMs = Date.now();
console.log(timeInMs);

connection.query(` 
select 
c.name as "Name",
cl.u_geographic_region as "Region",
CASE
    WHEN cic.os like '%Windows%' THEN 'Windows'
    WHEN cic.os like '%aix%' THEN 'AIX'
    WHEN cic.os like '%esx%' THEN 'ESX'
    WHEN cic.os like '%linux%' THEN 'Linux'
    WHEN cic.os like '%solaris%' THEN 'Solaris'
        ELSE 'UNKNOWN'
    END as "Operating System",

ci.dns_domain as "Host domain",
ci.ip_address as "IP Address",
CASE
    WHEN c.sys_class_name = 'cmdb_ci_computer' THEN 'Computer'
    WHEN c.sys_class_name = 'cmdb_ci_win_server' THEN 'Windows Server'
    WHEN c.sys_class_name = 'cmdb_ci_esx_server' THEN 'ESX Server'
    WHEN c.sys_class_name = 'cmdb_ci_aix_server' THEN 'AIX Server'
    WHEN c.sys_class_name = 'cmdb_ci_lb' THEN 'Load Balancer'
    WHEN c.sys_class_name = 'cmdb_ci_lb_ace' THEN 'lb_ace'
    WHEN c.sys_class_name = 'cmdb_ci_aix_lb_bigip' THEN 'lb_bigip'
    WHEN c.sys_class_name = 'cmdb_ci_linux_server' THEN 'Linux Server'
    WHEN c.sys_class_name = 'cmdb_ci_mainframe' THEN 'IBM Mainframe'
    WHEN c.sys_class_name = 'cmdb_ci_solaris_server' THEN 'Solaris Server'
    WHEN c.sys_class_name = 'cmdb_u_ci_ibmi_server' THEN 'IBMi Server'
    WHEN c.sys_class_name = 'cmdb_ u_ci_ibmi_server' THEN 'IBMi Server'
    WHEN c.sys_class_name = 'cmdb_u_server_appliance' THEN 'ServerAppliance'
        ELSE 'Unknown'
    END as "Class",

CASE 
    WHEN c.install_status = '1' THEN 'Installed'
    WHEN c.install_status = '100' THEN 'Absent'
    WHEN c.install_status = '2' THEN 'On Order'
    WHEN c.install_status = '3' THEN 'In Maintenance'
    WHEN c.install_status = '4' THEN 'Pending Install'
    WHEN c.install_status = '5' THEN 'Pending Repair'
    WHEN c.install_status = '6' THEN 'In Stock'
    WHEN c.install_status = '7' THEN 'Retired'
    WHEN c.install_status = '8' THEN 'Stolen'
    WHEN c.install_status = '9' THEN 'In Use'
                         ELSE 'Unknown'
          END As "lifecycle Status"

from
cmdb as c
join cmdb_ci ci on c.sys_id = ci.sys_id
join cmdb_ci_hardware as cih on c.sys_id = cih.sys_id
join cmdb_ci_computer cic on c.sys_id = cic.sys_id
join cmn_location as cl on cl.sys_id = c.location
where c.name is not null
`, function(err, rows, fields) {
  if (err) throw err;

 for (var i in rows) {
  console.log('the rows are', rows[i]);
}
});

connection.end();

现在,当使用"node filename.js"运行代码时,可以将输出打印到我的控制台上.但我希望将其写入json文件,以便我可以处理数据并应用一些逻辑.

Now when am running the code using "node filename.js" am able to print the output to my console. But i want it to be written to a json file so that i can process the data and apply some logic.

推荐答案

如果您要做的只是将JSON中的MySQL数据输出写入文件,请替换...

If all you want to do is write the output of the MySQL data in JSON to a file, replace...

for (var i in rows) {
 console.log('the rows are', rows[i]);
}

...具有:

fs.writeFile("/path/to/file.json", JSON.stringify(rows), function(err) {
    if (err) return console.log(err);    
    console.log("The file was saved!");
});

有关JSON.stringify()的更多信息,位于 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

More about JSON.stringify() at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

这篇关于MySQL数据需要写入JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆