Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.sql.*;
- import java.util.*;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- public class Populate {
- // Define the connection URL
- public static final String HOST = "localhost";
- public static final String DBNAME = "ORCL";
- public static final String PORT = "1521";
- public static final String DBURL = "jdbc:oracle:thin:@" + HOST + ":" + PORT + ":" + DBNAME;
- public static final String DBUSERNAME = "";
- public static final String DBPASSWORD = "";
- // @param args: the command line arguments
- public static void main(String[] args) throws FileNotFoundException {
- // Load the driver
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Class.forName("com.mysql.jdbc.Driver");
- }
- catch (Exception E) {
- System.out.println("Unable to load the driver.");
- E.printStackTrace();
- }
- // Remove previous data in tables
- removePrevData();
- // Get four JSON files put in command line
- if (args.length != 4) {
- System.out.println("Error: Four JSON files are required.")
- }
- else {
- parseBusinessJson(args[0])
- ParseUserJson(args[3])
- parseReviewJson(args[1])
- ParseCheckinJson(args[2])
- }
- }
- // Remove previous data in tables
- public static void removePrevData() {
- try {
- Connection connection = DriverManager.getConnection(DBURL, DBUSERNAME, DBPASSWORD);
- Statement deleteTableStatement = connection.createStatement();
- // TO DO: delete from all tables
- deleteTableStatement.executeUpdate("DELETE FROM Business");
- deleteTableStatement.executeUpdate("DELETE FROM User");
- deleteTableStatement.executeUpdate("DELETE FROM ")
- }
- catch(SQLException e) {
- System.out.println("Error in removing previous data.")
- e.printStackTrace();
- }
- }
- // Parse yelp_business.json file
- public static void parseBusinessJson(business_json) {
- System.out.println("Parsing yelp_business.json");
- try {
- File business_file = new File(business_json);
- FileReader business_fr = new FileReader(business_file);
- BufferReader business_bf = new BufferReader(business_fr);
- JSONObject jsonObject = null;
- JSONParser parser = new JSONParser();
- // preparedStatement
- Connection connection = DriverManager.getConnection(DBURL, DBUSERNAME, DBPASSWORD);
- PreparedStatement businessInsertPreparedStatement = connection.preparedStatement("INSERT INTO Business("
- + "business_id, full_address, open, city, state, latitude, longitude, "
- + "review_count, business_name, stars, type) "
- + "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
- PreparedStatement hoursInsertPreparedStatement = connection.preparedStatement("INSERT INTO Business_Hours("
- + "business_id, business_day, open_time, close_time) "
- + "VALUES (?,?,?,?)");
- PreparedStatement neighborhoodsPreparedStatement = connection.preparedStatement("INSERT INTO Business_Neighborhoods("
- + "business_id, neighborhoods) "
- + "VALUES (?,?)");
- PreparedStatement attributesPreparedStatement = connection.preparedStatement("INSERT INTO Business_Attributes("
- + "business_id, business_attributes) "
- + "VALUES (?,?)");
- PreparedStatement categoryPreparedStatement = connection.preparedStatement("INSERT INTO Business_Category("
- + "business_id, main_category, sub_category) "
- + "VALUES (?,?,?)");
- // Read each line
- while (business_line = business_json.readLine() != null) {
- try {
- jsonObject = (JSONObject) parser.parse(business_line);
- // Insert into Business Table
- String business_id = jsonObject.getString("business_id");
- String full_address = jsonObject.getString("full_address");
- String open = jsonObject.getBoolean("open").toString();
- String city = jsonObject.getString("city");
- String state = jsonObject.getString("state");
- Double latitude = jsonObject.getDouble("latitude");
- Double longitude = jsonObject.getDouble("longitude");
- Long review_count = jsonObject.getLong("review_count");
- String business_name = jsonObject.getString("review_count");
- Double stars = jsonObject.getDouble("stars");
- String type = jsonObject.getString("type");
- businessInsertPreparedStatement.setString(1, business_id);
- businessInsertPreparedStatement.setString(2, full_address);
- businessInsertPreparedStatement.setString(3, open);
- businessInsertPreparedStatement.setString(4, city);
- businessInsertPreparedStatement.setString(5, state);
- businessInsertPreparedStatement.setDouble(6, latitude);
- businessInsertPreparedStatement.setDouble(7, longitude);
- businessInsertPreparedStatement.setLong(8, review_count);
- businessInsertPreparedStatement.setString(9, business_name);
- businessInsertPreparedStatement.setDouble(10, stars);
- businessInsertPreparedStatement.setString(11, type);
- businessInsertPreparedStatement.executeUpdate();
- // Insert into Business_Hours Table (business_id, business_day, open_time, close_time)
- // {"hours": {"Monday": {"close": "22:00", "open": "11:00"}, "Tuesday": {}, ...}}
- // hours_jo: {"Monday": {"close": "22:00", "open": "11:00"}, "Tuesday": {}, ...}
- JSONObject hours_jo = jsonObject.getJSONObject("hours");
- // close_open_time_jo: {"close": "22:00", "open": "11:00"}
- JSONObject close_open_time_jo;
- // days: ["Sunday", "Monday" ...]
- ArrayList<String> days = new ArrayList<String>();
- days.addAll(hours_jo.keySet());
- for (int i = 0; i < hours_jo.size(); ++i) {
- // for a particular day
- day = days.get(i);
- close_open_time_jo = hours_jo.getJSONObject(day);
- String close_time = close_open_time_jo.getString("close");
- String open_time = close_open_time_jo.getString("open");
- DateFormat dateFormat = new SimpleDateFormat("hh:mm");
- Date close_date = dateFormat.parse(close_time);
- Timestamp close_timestamp = new Timestamp(close_date.getTime());
- Date open_date = dateFormat.parse(open_time);
- Timestamp open_timestamp = new Timestamp(open_date.getTime());
- hoursInsertPreparedStatement.setString(1, business_id);
- hoursInsertPreparedStatement.setString(2, day);
- hoursInsertPreparedStatement.setTimestamp(3, open_timestamp);
- hoursInsertPreparedStatement.setTimestamp(4, close_timestamp);
- hoursInsertPreparedStatement.executeUpdate();
- }
- // Insert into Business_Neighborhoods Table (business_id, neighborhoods)
- // {"neighborhoods": ["Mayfair Park"]}
- JSONArray neighborhoods_array = jsonObject.getJSONArray("neighborhoods");
- for (int i = 0; i < neighborhoods_array.size(); ++i) {
- String neighborhood = neighborhoods_array.get(i);
- neighborhoodsPreparedStatement.setString(1, business_id);
- neighborhoodsPreparedStatement.setString(2, neighborhood);
- neighborhoodsPreparedStatement.executeUpdate();
- }
- // Insert into Business_Attributes Table (business_id, attributes)
- // attributes is the attribute that is not false (including attributes and subattributes)
- // {"attributes": {"Take-out": true, "Good For": {"dessert": false, "breakfast": false}, ...}}
- JSONObject attributes_jo = jsonObject.getJSONObject("attributes");
- ArrayList<String> attribute_names = new ArrayList<String>();
- attribute_names.addAll(attributes_jo.keySet());
- for (int i = 0; i < attribute_names.size(); ++i) {
- String attribute_name = attribute_names.get(i);
- String value = attributes_jo.get(attribute_name).toString();
- // value can be "true" or "false" or "some string" or "{" ":} (an object)"
- if (value.equals("true")) {
- attributesPreparedStatement.setString(1, business_id);
- attributesPreparedStatement.setString(2, attribute_name);
- attributesPreparedStatement.executeUpdate();
- }
- else if (!(value.equals("true")) && !(value.equals("false")) && !(value[0].equals("{"))) {
- String combine_attr = attribute_name + "_" + value
- attributesPreparedStatement.setString(1, business_id);
- attributesPreparedStatement.setString(2, combine_attr);
- attributesPreparedStatement.executeUpdate();
- }
- else if(value[0].equals("{")) {
- JSONObject sub_attributes_jo = attributes_jo.getJSONObject(attribute_name);
- ArrayList<String> sub_attribute_names = new ArrayList<String>();
- sub_attribute_names.addAll(sub_attributes_jo.keySet());
- for (int i = 0; i < sub_attribute_names.size(); ++i) {
- String sub_attribute_name = sub_attribute_names.get(i);
- String sub_value = sub_attributes_jo.get(sub_attribute_name).toString();
- // sub_value can be "true" or "false" or "some string"
- if (sub_value.equals("true")) {
- String combine_attr = attribute_names + "_" + sub_attribute_name
- attributesPreparedStatement.setString(1, business_id);
- attributesPreparedStatement.setString(2, combine_attr);
- attributesPreparedStatement.executeUpdate();
- else if (!(sub_value.equals("false"))) {
- String combine_attr = attribute_names + "_" + sub_attribute_name + "_" + sub_value
- attributesPreparedStatement.setString(1, business_id);
- attributesPreparedStatement.setString(2, combine_attr);
- attributesPreparedStatement.executeUpdate();
- }
- }
- }
- }
- // Insert into Business_Category Table (business_id, main_category, sub_category)
- // {"categories": ["Mexican", "Restaurants"]} can have 0 or more than 1 main/sub categories
- String[] main_business_categories = {"Active Life", "Arts & Entertainment", "Automotive", "Car Rental", "Cafes", "Beauty & Spas", "Convenience Stores", "Dentists", "Doctors", "Drugstores", "Department Stores", "Education", "Event Planning & Services", "Flowers & Gifts", "Food", "Health & Medical", "Home Services", "Home & Garden", "Hospitals", "Hotels & Travel", "Hardware Stores", "Grocery", "Medical Centers", "Nurseries & Gardening", "Nightlife", "Restaurants", "Shopping", "Transportation"};
- ArrayList main_categories_list.addAll(main_business_categories);
- JSONArray category_array = jsonObject.getJSONArray("categories");
- // For the current line, list of main categories and sub categories
- ArrayList<String> main_categories = new ArrayList<String>();
- ArrayList<String> sub_categories = new ArrayList<String>();
- for (int i = 0; i < category_array.size(); ++i) {
- String category = category_array.get(i);
- if (main_categories_list.contains(category)) {
- main_categories.add(category);
- }
- else {
- sub_categories.add(category);
- }
- }
- // No sub categories, only need to insert main_category
- if (sub_categories.size() == 0) {
- for (int m = 0; m < main_categories.size(); ++m) {
- categoryPreparedStatement.setString(1, business_id);
- categoryPreparedStatement.setString(2, main_categories.get(m));
- categoryPreparedStatement.executeUpdate();
- }
- }
- // Have sub categories
- else {
- for (int m = 0; m < main_categories.size(); ++m) {
- for (int s = 0; s < sub_categories.size(); ++s) {
- categoryPreparedStatement.setString(1, business_id);
- categoryPreparedStatement.setString(2, main_categories.get(m));
- categoryPreparedStatement.setString(3, sub_categories.get(s));
- categoryPreparedStatement.executeUpdate();
- }
- }
- }
- main_categories.clear();
- sub_categories.clear();
- }
- catch (ParseException pe) {
- System.out.println("Error in parsing business.");
- pe.printStackTrace();
- }
- } //end of while
- business_bf.close();
- connection.close();
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- // Parse yelp_user.json file
- public static void parseUserJson(user_json) {
- System.out.println("Parsing yelp_user.json");
- try {
- File user_file = new File(user_json);
- FileReader user_fr = new FileReader(user_file);
- BufferReader user_bf = new BufferReader(user_fr);
- JSONObject jsonObject = null;
- JSONParser parser = new JSONParser();
- // preparedStatement
- Connection connection = DriverManager.getConnection(DBURL, DBUSERNAME, DBPASSWORD);
- PreparedStatement userInsertPreparedStatement = connection.preparedStatement("INSERT INTO User("
- + "yelping_since, useful_votes, funny_votes, cool_votes, review_count, user_name, user_id"
- + "fans, average_stars, type) "
- + "VALUES (?,?,?,?,?,?,?,?,?,?)");
- // Read each line
- while (user_line = user_json.readLine() != null) {
- try {
- jsonObject = (JSONObject) parser.parse(user_line);
- // Insert into Review Table
- // {"yelping_since": "2012-02"}
- String since_str = jsonObject.getString("yelping_since");
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
- Date since_date = dateFormat.parse(since_str);
- Timestamp yelping_since = new Timestamp(since_date.getTime());
- // {"votes": {"funny": 0, "useful": 2, "cool": 1}}
- JSONObject votes = jsonObject.getJSONObject("votes");
- Long useful_votes = votes.getLong("useful");
- Long funny_votes = votes.getLong("funny");
- Long cool_votes = votes.getLong("cool");
- Long review_count = jsonObject.getLong("review_count");
- String user_name = jsonObject.getString("name");
- String user_id = jsonObject.getString("user_id");
- int fans = jsonObject.getInt("fans");
- Double average_stars = jsonObject.getDouble("average_stars");
- String type = jsonObject.getString("type");
- reviewInsertPreparedStatement.setTimestamp(1, yelping_since);
- reviewInsertPreparedStatement.setLong(2, useful_votes);
- reviewInsertPreparedStatement.setLong(3, funny_votes);
- reviewInsertPreparedStatement.setLong(4, cool_votes);
- reviewInsertPreparedStatement.setLong(5, review_count);
- reviewInsertPreparedStatement.setString(6, user_name);
- reviewInsertPreparedStatement.setString(7, user_id);
- reviewInsertPreparedStatement.setInt(8, fans);
- reviewInsertPreparedStatement.setDouble(9, average_stars);
- reviewInsertPreparedStatement.setString(10, type);
- reviewInsertPreparedStatement.executeUpdate();
- }
- catch (ParseException pe) {
- System.out.println("Error in parsing user.")
- e.printStackTrace();
- }
- }
- user_bf.close();
- connection.close();
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- // Parse yelp_review.json file
- public static void parseReviewJson(review_json) {
- System.out.println("Parsing yelp_review.json");
- try {
- File review_file = new File(review_json);
- FileReader review_fr = new FileReader(review_file);
- BufferReader review_bf = new BufferReader(review_fr);
- JSONObject jsonObject = null;
- JSONParser parser = new JSONParser();
- // preparedStatement
- Connection connection = DriverManager.getConnection(DBURL, DBUSERNAME, DBPASSWORD);
- PreparedStatement reviewInsertPreparedStatement = connection.preparedStatement("INSERT INTO Review("
- + "useful_votes, funny_votes, cool_votes, user_id, review_id, stars, "
- + "review_date, review_text, type, business_id) "
- + "VALUES (?,?,?,?,?,?,?,?,?,?)");
- // Read each line
- while (review_line = review_json.readLine() != null) {
- try {
- jsonObject = (JSONObject) parser.parse(review_line);
- // Insert into Review Table
- // {"votes": {"funny": 0, "useful": 2, "cool": 1}}
- JSONObject votes = jsonObject.getJSONObject("votes");
- Long useful_votes = votes.getLong("useful");
- Long funny_votes = votes.getLong("funny");
- Long cool_votes = votes.getLong("cool");
- String user_id = jsonObject.getString("user_id");
- String review_id = jsonObject.getString("review_id");
- int stars = jsonObject.getInt("stars");
- // {"date": "2012-01-08"}
- String review_date_str = jsonObject.getString("date");
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date review_date_date = dateFormat.parse(review_date_str);
- Timestamp review_date = new Timestamp(review_date_date.getTime());
- String review_text = jsonObject.getString("text");
- String type = jsonObject.getString("type");
- String business_id = jsonObject.getString("business_id");
- reviewInsertPreparedStatement.setLong(1, useful_votes);
- reviewInsertPreparedStatement.setLong(2, funny_votes);
- reviewInsertPreparedStatement.setLong(3, cool_votes);
- reviewInsertPreparedStatement.setString(4, user_id);
- reviewInsertPreparedStatement.setString(5, review_id);
- reviewInsertPreparedStatement.setInt(6, stars);
- reviewInsertPreparedStatement.setTimestamp(7, review_date);
- reviewInsertPreparedStatement.setString(8, review_text);
- reviewInsertPreparedStatement.setString(9, type);
- reviewInsertPreparedStatement.setString(10, business_id);
- reviewInsertPreparedStatement.executeUpdate();
- }
- catch (ParseException pe) {
- System.out.println("Error in parsing review.")
- e.printStackTrace();
- }
- }
- review_bf.close();
- connection.close();
- }
- catch (JSONException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- // Parse yelp_checkin.json file
- public static void parseCheckinJson(checkin_json) {
- System.out.println("Parsing yelp_checkin.json");
- }
- }
Add Comment
Please, Sign In to add comment