如何将json文件放入数据库

时间:2019-03-22 13:38:38

标签: java mysql json

我有一个程序,可以解析json文件并获取订单文件,然后使用解析的json可以确定哪个订单ID有效或无效。虽然我已经将这一部分用于一次运行和一个json文件,但我无法弄清楚如何能够多次运行并在数据库中保存json信息以及订单信息,然后能够根据保存在数据库中的这两个文件生成报告。以下是我拥有的代码以及试图与通用输入一起使用的数据库示例代码,但是我不确定如何将解析的json文件或订单文件转移到该数据库中。

SQLDemo.java

package csi311;


import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.BufferedReader;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.io.FileReader;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.sound.sampled.Line;
import org.apache.derby.jdbc.EmbeddedDriver;
import com.fasterxml.jackson.databind.ObjectMapper;

import csi311.MachineSpec.StateTransitions; 

public class SqlDemo {

    private static final String DB_URL = "jdbc:derby:csi311-testdb1;create=true";
    private Connection conn = null;
    private Statement stmt = null;

    public SqlDemo() {
    }

    public void loadStateMachine(String jsonFilePath) throws Exception{
        String json = processFile(jsonFilePath); 
        MachineSpec machineSpec = parseJson(json);
        dumpMachine(machineSpec); 
        createConnection();
        createTable();
        insertRestaurant("LaVals", "Berkeley");
        selectRestaurants();
        shutdown();
    }
    public void loadOrderFile(String orderFilePath) {

    }
    public void generateReport(String tenantId) {

    }
    public void run(String filename, String filename2) throws Exception {
        createConnection();
        createTable();
        insertRestaurant("LaVals", "Berkeley");
        selectRestaurants();
        shutdown();
        System.out.println("Tru State");
        HashMap<String, String> hmap = new HashMap<>();
        HashMap<String, BigDecimal> h2map = new HashMap<>();
        ArrayList<String> flaggedids = new ArrayList<String>();
        ArrayList<String> flaggedidz = new ArrayList<String>();
        HashMap<String, String> h3map = new HashMap<>();
        HashMap<String, String> h4map = new HashMap<>();
        String terminal = "(terminal)";

        if (filename != null && filename2 != null) {
            String json = processFile(filename); 
            MachineSpec machineSpec = parseJson(json);
            dumpMachine(machineSpec); 
            String line = null;
            String orderid = "\\d\\d\\d-[A-Za-z][A-Za-z][A-Za-z]-\\d\\d\\d\\d";
            String timestamp = "\\S[0-9]{12}";
            String customerid = "\\S[0-9]{8}";
            String price = "\\S[0-9]{0,4}.[0-9]{2}";
            String quantity ="\\d+$";
            String description = "\\w+";

            BufferedReader brrr = new BufferedReader(new FileReader(filename2));
            while((line = brrr.readLine()) != null) {
                if(line.trim().isEmpty()) {
                    continue;
                }
                String [] lineSplit = line.split(",");
                //System.out.println(lineSplit.length);
                String pkgtenantId = lineSplit[0].trim();
                String pkgtimestmp = lineSplit[1].trim();
                String pkgOrderId = lineSplit[2].trim();
                String pkgCustId = lineSplit[3].trim();
                String pkgState = lineSplit[4].trim();
                String pkgOrderDesc = lineSplit[5].trim();
                String pkgQuantity = lineSplit[6].trim();
                String pkgPrice = lineSplit[7].trim();
                BigDecimal pkgPrices = new BigDecimal( lineSplit[7].trim());
                // System.out.println(pkgtimestmp);
                // System.out.println(pkgOrderId);
                // System.out.println(pkgCustId);
                // System.out.println(pkgState);
                //  System.out.println(pkgOrderDesc);
                // System.out.println(pkgQuantity);
                // System.out.println(pkgPrice);
                if(lineSplit.length >= 3) {
                    //System.out.println(pkgOrderId);
                    if(pkgOrderId.matches(orderid)) {
                        //System.out.println(pkgOrderId);
                        // make it so that if same id placing order other order will be tracked too!
                        if(hmap.containsKey(pkgOrderId)) {

                            //System.out.println(pkgOrderId);
                            if(lineSplit.length == 8) {
                                //System.out.println(pkgOrderDesc);
                                if(pkgtimestmp.matches(timestamp)&& pkgCustId.matches(customerid)&& pkgQuantity.matches(quantity)
                                        && pkgPrice.matches(price)) {
                                    //System.out.println(pkgOrderId);
                                    //System.out.println(pkgOrderId + ":" + pkgState);
                                    String state1 = h3map.get(pkgOrderId);
                                    //System.out.println(state1);
                                    machineSpec.verifyTransition(state1.toLowerCase(), pkgState.toLowerCase());
                                    //System.out.println(machineSpec.verifyTransition(state1, pkgState));
                                    if(machineSpec.verifyTransition(state1, pkgState) == true) {
                                        h3map.put(pkgOrderId, pkgState);

                                        h2map.put(pkgOrderId, pkgPrices);
                                        machineSpec.verifyTerminal(state1.toLowerCase());
                                        if(machineSpec.verifyTerminal(state1) == true) {
                                            continue;
                                        }else if(machineSpec.verifyTerminal(state1) == false) {
                                            h4map.put(pkgOrderId, (pkgState + terminal));
                                            //System.out.println(h4map);
                                        }
                                    }
                                    else if(machineSpec.verifyTransition(state1, pkgState) == false) {
                                        h3map.remove(pkgOrderId);
                                        h4map.remove(pkgOrderId);
                                        flaggedids.add(pkgOrderId);
                                        if(!flaggedidz.contains(pkgOrderId)) {
                                            flaggedidz.add(pkgOrderId);
                                        }
                                    }


                                }
                                else if(!(pkgtimestmp.matches(timestamp)&& pkgCustId.matches(customerid)&& pkgQuantity.matches(quantity)
                                        && pkgPrice.matches(price))){
                                    flaggedids.add(pkgOrderId);
                                    if(!flaggedidz.contains(pkgOrderId)) {
                                        flaggedidz.add(pkgOrderId);
                                    }
                                }
                            }
                            else if(!(lineSplit.length == 8)) {
                                flaggedids.add(pkgOrderId);
                                if(!flaggedidz.contains(pkgOrderId)) {
                                    flaggedidz.add(pkgOrderId);
                                }
                            }

                        }
                        else if(!hmap.containsKey(pkgOrderId)) {
                            //System.out.println(pkgOrderId);
                            hmap.put(pkgOrderId, pkgCustId);

                            if(lineSplit.length == 8) {
                                //System.out.println(pkgOrderId);
                                if(pkgtimestmp.matches(timestamp)&& pkgCustId.matches(customerid)&& pkgQuantity.matches(quantity)
                                        && pkgPrice.matches(price)) {
                                    //System.out.println(pkgOrderId);
                                    //System.out.println(pkgState);
                                    for(StateTransitions gt : machineSpec.getMachineSpec()) {
                                        //System.out.println(pkgState);
                                        if(gt.getState().contains("start") || gt.getState().contains("START")) {
                                            //System.out.println(pkgState);
                                            if(gt.getTransitions().contains(pkgState.toLowerCase())) {
                                                h3map.put(pkgOrderId, pkgState);
                                                h2map.put(pkgOrderId, pkgPrices);
                                                //System.out.println(h3map);
                                            }
                                        }
                                    }

                                }
                                else if(!(pkgtimestmp.matches(timestamp)&& pkgCustId.matches(customerid)&& pkgQuantity.matches(quantity)
                                        && pkgPrice.matches(price))){
                                    flaggedids.add(pkgOrderId);
                                    if(!flaggedidz.contains(pkgOrderId)) {
                                        flaggedidz.add(pkgOrderId);
                                    }
                                }
                            }
                            else if(!(lineSplit.length == 8)) {
                                //System.out.println(pkgOrderId);
                                flaggedids.add(pkgOrderId);
                                if(!flaggedidz.contains(pkgOrderId)) {
                                    flaggedidz.add(pkgOrderId);
                                }
                            }
                        }
                    }
                    else if(!pkgOrderId.matches(orderid)){
                        if(!flaggedids.contains(pkgOrderId)) {
                            flaggedids.add(pkgOrderId);
                        }
                        if(!flaggedidz.contains(pkgOrderId)) {
                            flaggedidz.add(pkgOrderId);
                        }
                    }

                }
            }

            //System.out.println(h2map);
            //System.out.println(h3map);
            // h2map = float h3map = strings h3map = hmap h2map = h2map


            HashMap<String, BigDecimal> sum = new HashMap<>();


            for(String key : h2map.keySet()) {
                if(h3map.get(key) != null) {
                    String value = h3map.get(key);
                    if(sum.get(value) == null) {
                        sum.put(value, h2map.get(key));
                    }else {
                        sum.put(value, (sum.get(value).add(h2map.get(key))));
                    }
                }
            }

            for(int i = 0; i < flaggedids.size(); i++) {

                System.out.println("Flagging order: " + flaggedids.get(i));

                }
            for(String statekey : sum.keySet()) {
                if(machineSpec.verifyTerminal(statekey) == true) {


                System.out.println(statekey + " " + Collections.frequency(h3map.values(), statekey) + " "+ "$"+ sum.get(statekey) + " " + terminal);
                }
                else if(machineSpec.verifyTerminal(statekey) == false) {
                    System.out.println(statekey + " " + Collections.frequency(h3map.values(), statekey) + " "+ "$"+ sum.get(statekey));
                }

            }
            //System.out.println(sum);  
            HashMap<String, BigDecimal> countMap = new HashMap<>();
            for(Iterator<Map.Entry<String, BigDecimal>> itr = sum.entrySet().iterator(); itr.hasNext();) {
                Map.Entry<String, BigDecimal> entry = itr.next();
                String key = entry.getKey();
                int count = Collections.frequency(h3map.values(), key);
                countMap.put((key + " " + count), sum.get(key));
                itr.remove();
            }




            //System.out.println(h4map);



            //System.out.println(countMap);
            //countMap.forEach((k, v) -> System.out.println(k + " " + "$"+v+ " Terminal? "+machineSpec.verifyTerminal(k)));
            //System.out.println("this is" + hmap);
            System.out.println("flagged " + flaggedidz.size());
            //System.out.println("final mapping" + h3map);
            brrr.close();
        }
    }

    private void dumpMachine(MachineSpec machineSpec) {
        if (machineSpec == null) {
            return;
        }
        System.out.println(machineSpec.getTenantID());
        for (StateTransitions st : machineSpec.getMachineSpec()) {
            System.out.println(st.getState() + " : " + st.getTransitions());
        }
    }

    private String processFile(String filename) throws Exception {
        //System.out.println("Processing file: " + filename); 
        BufferedReader br = new BufferedReader(new FileReader(filename));  
        String json = "";
        String line; 
        while ((line = br.readLine()) != null) {
            json += " " + line; 
        } 
        br.close();
        // Get rid of special characters - newlines, tabs.  
        return json.replaceAll("\n", " ").replaceAll("\t", " ").replaceAll("\r", " "); 
    }


    private MachineSpec parseJson(String json) {
        ObjectMapper mapper = new ObjectMapper();
        try { 
            MachineSpec machineSpec = mapper.readValue(json, MachineSpec.class);
            return machineSpec; 
        }
        catch (Exception e) {
            e.printStackTrace(); 
        }
        return null;    
    }

    private void createConnection() {
        try {
            Driver derbyEmbeddedDriver = new EmbeddedDriver();
            DriverManager.registerDriver(derbyEmbeddedDriver);
            conn = DriverManager.getConnection(DB_URL);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void createTable() {
        try {
            stmt = conn.createStatement();
            stmt.execute("create table restaurants (" +
                    "id INT NOT NULL " + 
                    "CONSTRAINT id_PK PRIMARY KEY," + 
                    "name varchar(30) not null," + 
                    "city varchar(30) not null" + 
                    ")");
            stmt.close();
        }
        catch (SQLException sqlExcept) {
            if(!tableAlreadyExists(sqlExcept)) {
                sqlExcept.printStackTrace();
            }
        }
    }


    private boolean tableAlreadyExists(SQLException e) {
        boolean exists;
        if(e.getSQLState().equals("X0Y32")) {
            exists = true;
        } else {
            exists = false;
        }
        return exists;
    }


    private void insertRestaurant(String restName, String cityName) {
        try {
            stmt = conn.createStatement();
            //stmt.execute("insert into restaurants (name,city) values (" +
            //        "'" + restName + "','" + cityName +"')");
            stmt.close();
        }
        catch (SQLException sqlExcept) {
            sqlExcept.printStackTrace();
        }
    }


    private void selectRestaurants()
    {
        try {
            stmt = conn.createStatement();
            ResultSet results = stmt.executeQuery("select * from restaurants");
            ResultSetMetaData rsmd = results.getMetaData();
            int numberCols = rsmd.getColumnCount();
            for (int i=1; i<=numberCols; i++) {
                //print Column Names
                System.out.print(rsmd.getColumnLabel(i)+"\t\t");  
            }

            System.out.println("\n-------------------------------------------------");

            while(results.next()) {
                int id = results.getInt(1);
                String restName = results.getString(2);
                String cityName = results.getString(3);
                System.out.println(id + "\t\t" + restName + "\t\t" + cityName);
            }
            results.close();
            stmt.close();
        }
        catch (SQLException sqlExcept) {
            sqlExcept.printStackTrace();
        }
    }


    private void shutdown() {
        try {
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                DriverManager.getConnection(DB_URL + ";shutdown=true");
                conn.close();
            }           
        }
        catch (SQLException sqlExcept) {
        }
    }


    public static void main(String[] args) {
        SqlDemo theApp = new SqlDemo();
        String mode = null;
        if (args.length == 2) {
            mode = args[0];
            if(mode.equals("--state")) {
                String jsonFilePath = args[1];

                try {
                    theApp.loadStateMachine(jsonFilePath);
                }catch(Exception e) {
                    e.printStackTrace();
                }

            }else if(mode.equals("--order")) {
                String orderFilePath = args[1];

                try {
                    theApp.loadOrderFile(orderFilePath);
                }catch(Exception e) {
                    e.printStackTrace();
                }

            }else if(mode.equals("--report")) {
                String tenantId = args[1];
                if(tenantId.matches("[0-9]{5}")){
                try {
                    theApp.generateReport(tenantId);
                }catch(Exception e) {
                    e.printStackTrace();
                    }
                }else {
                    System.out.println("bad id");
                }

            }else {
                System.out.println("Invalid mode options");
            }

        try { 
            //theApp.run(filename, filename2);
        }
        catch (Exception e) {
            System.out.println("Something bad happened!");
            e.printStackTrace();
        }
       }else {
           System.out.println("Wrong Number of Arguments");
       }
    }   
}

MachineSpec.java

package csi311;

import java.io.Serializable;
import java.util.List;

@SuppressWarnings("serial")
public class MachineSpec implements Serializable {

    // Since defined as an inner class, must be declared static or Jackson can't deal.
    public static class StateTransitions implements Serializable {
        private String state; 
        private List<String> transitions;
        public StateTransitions() { }
        public String getState() { return state; }
        public void setState(String state) { this.state = state.toLowerCase(); } 
        public List<String> getTransitions() { return transitions; } 
        public void setTransitions(List<String> transitions) { 
            this.transitions = transitions;
            if (this.transitions != null) {
                for (int i = 0; i < transitions.size(); i++) {
                    transitions.set(i, transitions.get(i).toLowerCase()); 
                }
            }
        } 
    }
    private Integer tenantId;
    private List<StateTransitions> machineSpec;
    public MachineSpec() { }
    public Integer getTenantID() { return tenantId; }
    public void setTenantId(Integer tenantId) { this.tenantId = tenantId; }
    public List<StateTransitions> getMachineSpec() { return machineSpec; } 
    public void setMachineSpec(List<StateTransitions> machineSpec) { this.machineSpec = machineSpec; } 

    public boolean verifyTransition(String prev_state, String next_state) {
            for(int i = 0; i < getMachineSpec().size(); i++) {
                StateTransitions lt = getMachineSpec().get(i);
                if(lt.getState().contains(prev_state.toLowerCase())) {
                    //System.out.println(prev_state);
                    if(lt.getTransitions().contains(next_state.toLowerCase())) {
                        //System.out.println(next_state);
                        return true;
                    }
                }

            }


                //System.out.println(prev_state);
                //System.out.println(next_state);


        return false;
    }
    public boolean verifyTerminal(String prev_state) {
        for(int i = 0; i<getMachineSpec().size(); i++) {
            StateTransitions lt = getMachineSpec().get(i);
            if(lt.getState().contains(prev_state.toLowerCase())) {
                return false;
            }
        }
        return true;
    }
}

0 个答案:

没有答案