List of usage examples for java.util Scanner next
public String next()
From source file:fitmon.Client.java
public static void main(String[] args) throws IOException, ClientProtocolException, NoSuchAlgorithmException, InvalidKeyException, SAXException, ParserConfigurationException { Food food = null;/*from w w w . j a va 2 s.co m*/ DietAPI dApi = new DietAPI(); JDBCConnection jdbcCon = new JDBCConnection(); ArrayList<Food> foodItems = dApi.getFood(); Scanner scan = new Scanner(System.in); Calendar currentDate = Calendar.getInstance(); //Get the current date SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); //format it as per your requirement String dateNow = formatter.format(currentDate.getTime()); System.out.println("Now the date is :=> " + dateNow); for (int i = 0; i < foodItems.size(); i++) { food = foodItems.get(i); System.out.println("ID : " + food.getFoodID()); System.out.println("servingID : " + food.getServingID()); System.out.println("Name : " + food.getItemName()); System.out.println("category : " + food.getCategory()); System.out.println("Quantity : " + food.getQuantity()); System.out.println("calories : " + food.getCalories()); System.out.println("fat : " + food.getFat()); System.out.println("carbs : " + food.getCarbs()); System.out.println("protein : " + food.getProtein()); System.out.println("fiber : " + food.getFiber()); System.out.println("sodium : " + food.getSodium()); System.out.println("sugar : " + food.getSugar()); System.out.println("cholesterol : " + food.getCholesterol()); System.out.println( "------------------------------------------------------------------------------------------------"); } System.out.println("Choose a meal......"); String mealType = scan.next(); System.out.println("Choose an item......"); String servingID = scan.next(); for (int j = 0; j < foodItems.size(); j++) { if (foodItems.get(j).getServingID() == null ? servingID == null : foodItems.get(j).getServingID().equals(servingID)) { food = foodItems.get(j); break; } } Diet diet = new CustomizedDiet(); diet.createDiet(food, mealType, dateNow); }
From source file:TwitterClustering.java
public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here File outFile = new File(args[3]); Scanner s = new Scanner(new File(args[1])).useDelimiter(","); JSONParser parser = new JSONParser(); Set<Cluster> clusterSet = new HashSet<Cluster>(); HashMap<String, Tweet> tweets = new HashMap(); FileWriter fw = new FileWriter(outFile.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); // init/*from www . j av a 2 s .c o m*/ try { Object obj = parser.parse(new FileReader(args[2])); JSONArray jsonArray = (JSONArray) obj; for (int i = 0; i < jsonArray.size(); i++) { Tweet twt = new Tweet(); JSONObject jObj = (JSONObject) jsonArray.get(i); String text = jObj.get("text").toString(); long sum = 0; for (int y = 0; y < text.toCharArray().length; y++) { sum += (int) text.toCharArray()[y]; } String[] token = text.split(" "); String tID = jObj.get("id").toString(); Set<String> mySet = new HashSet<String>(Arrays.asList(token)); twt.setAttributeValue(sum); twt.setText(mySet); twt.setTweetID(tID); tweets.put(tID, twt); } // preparing initial clusters int i = 0; while (s.hasNext()) { String id = s.next();// id Tweet t = tweets.get(id.trim()); clusterSet.add(new Cluster(i + 1, t, new LinkedList())); i++; } Iterator it = tweets.entrySet().iterator(); for (int l = 0; l < 2; l++) { // limit to 25 iterations while (it.hasNext()) { Map.Entry me = (Map.Entry) it.next(); // calculate distance to each centroid Tweet p = (Tweet) me.getValue(); HashMap<Cluster, Float> distMap = new HashMap(); for (Cluster clust : clusterSet) { distMap.put(clust, jaccardDistance(p.getText(), clust.getCentroid().getText())); } HashMap<Cluster, Float> sorted = (HashMap<Cluster, Float>) sortByValue(distMap); sorted.keySet().iterator().next().getMembers().add(p); } // calculate new centroid and update Clusterset for (Cluster clust : clusterSet) { TreeMap<String, Long> tDistMap = new TreeMap(); Tweet newCentroid = null; Long avgSumDist = new Long(0); for (int j = 0; j < clust.getMembers().size(); j++) { avgSumDist += clust.getMembers().get(j).getAttributeValue(); tDistMap.put(clust.getMembers().get(j).getTweetID(), clust.getMembers().get(j).getAttributeValue()); } if (clust.getMembers().size() != 0) { avgSumDist /= (clust.getMembers().size()); } ArrayList<Long> listValues = new ArrayList<Long>(tDistMap.values()); if (tDistMap.containsValue(findClosestNumber(listValues, avgSumDist))) { // found closest newCentroid = tweets .get(getKeyByValue(tDistMap, findClosestNumber(listValues, avgSumDist))); clust.setCentroid(newCentroid); } } } // create an iterator Iterator iterator = clusterSet.iterator(); // check values while (iterator.hasNext()) { Cluster c = (Cluster) iterator.next(); bw.write(c.getId() + "\t"); System.out.print(c.getId() + "\t"); for (Tweet t : c.getMembers()) { bw.write(t.getTweetID() + ", "); System.out.print(t.getTweetID() + ","); } bw.write("\n"); System.out.println(""); } System.out.println(""); System.out.println("SSE " + sumSquaredErrror(clusterSet)); } catch (Exception e) { e.printStackTrace(); } finally { bw.close(); fw.close(); } }
From source file:com.github.horrorho.inflatabledonkey.Main.java
/** * @param args the command line arguments * @throws IOException/*from w ww.j a v a2s. c om*/ */ public static void main(String[] args) throws IOException { try { if (!PropertyLoader.instance().test(args)) { return; } } catch (IllegalArgumentException ex) { System.out.println("Argument error: " + ex.getMessage()); System.out.println("Try '" + Property.APP_NAME.value() + " --help' for more information."); System.exit(-1); } // SystemDefault HttpClient. // TODO concurrent CloseableHttpClient httpClient = HttpClients.custom().setUserAgent("CloudKit/479 (13A404)") .useSystemProperties().build(); // Auth // TODO rework when we have UncheckedIOException for Authenticator Auth auth = Property.AUTHENTICATION_TOKEN.value().map(Auth::new).orElse(null); if (auth == null) { auth = Authenticator.authenticate(httpClient, Property.AUTHENTICATION_APPLEID.value().get(), Property.AUTHENTICATION_PASSWORD.value().get()); } logger.debug("-- main() - auth: {}", auth); logger.info("-- main() - dsPrsID:mmeAuthToken: {}:{}", auth.dsPrsID(), auth.mmeAuthToken()); if (Property.ARGS_TOKEN.booleanValue().orElse(false)) { System.out.println("DsPrsID:mmeAuthToken " + auth.dsPrsID() + ":" + auth.mmeAuthToken()); return; } logger.info("-- main() - Apple ID: {}", Property.AUTHENTICATION_APPLEID.value()); logger.info("-- main() - password: {}", Property.AUTHENTICATION_PASSWORD.value()); logger.info("-- main() - token: {}", Property.AUTHENTICATION_TOKEN.value()); // Account Account account = Accounts.account(httpClient, auth); // Backup Backup backup = Backup.create(httpClient, account); // BackupAccount BackupAccount backupAccount = backup.backupAccount(httpClient); logger.debug("-- main() - backup account: {}", backupAccount); // Devices List<Device> devices = backup.devices(httpClient, backupAccount.devices()); logger.debug("-- main() - device count: {}", devices.size()); // Snapshots List<SnapshotID> snapshotIDs = devices.stream().map(Device::snapshots).flatMap(Collection::stream) .collect(Collectors.toList()); logger.info("-- main() - total snapshot count: {}", snapshotIDs.size()); Map<String, Snapshot> snapshots = backup.snapshot(httpClient, snapshotIDs).stream().collect( Collectors.toMap(s -> s.record().getRecordIdentifier().getValue().getName(), Function.identity())); boolean repeat = false; do { for (int i = 0; i < devices.size(); i++) { Device device = devices.get(i); List<SnapshotID> deviceSnapshotIDs = device.snapshots(); System.out.println(i + " " + device.info()); for (int j = 0; j < deviceSnapshotIDs.size(); j++) { SnapshotID sid = deviceSnapshotIDs.get(j); System.out.println("\t" + j + snapshots.get(sid.id()).info() + " " + sid.timestamp()); } } if (Property.PRINT_SNAPSHOTS.booleanValue().orElse(false)) { return; } // Selection Scanner input = new Scanner(System.in); int deviceIndex; int snapshotIndex = Property.SELECT_SNAPSHOT_INDEX.intValue().get(); if (devices.size() > 1) { System.out.printf("Select a device [0 - %d]: ", devices.size() - 1); deviceIndex = input.nextInt(); } else deviceIndex = Property.SELECT_DEVICE_INDEX.intValue().get(); if (deviceIndex >= devices.size() || deviceIndex < 0) { System.out.println("No such device: " + deviceIndex); System.exit(-1); } Device device = devices.get(deviceIndex); System.out.println("Selected device: " + deviceIndex + ", " + device.info()); if (device.snapshots().size() > 1) { System.out.printf("Select a snapshot [0 - %d]: ", device.snapshots().size() - 1); snapshotIndex = input.nextInt(); } else snapshotIndex = Property.SELECT_SNAPSHOT_INDEX.intValue().get(); if (snapshotIndex >= devices.get(deviceIndex).snapshots().size() || snapshotIndex < 0) { System.out.println("No such snapshot for selected device: " + snapshotIndex); System.exit(-1); } logger.info("-- main() - arg device index: {}", deviceIndex); logger.info("-- main() - arg snapshot index: {}", snapshotIndex); String selected = devices.get(deviceIndex).snapshots().get(snapshotIndex).id(); Snapshot snapshot = snapshots.get(selected); System.out.println("Selected snapshot: " + snapshotIndex + ", " + snapshot.info()); // Asset list. List<Assets> assetsList = backup.assetsList(httpClient, snapshot); logger.info("-- main() - assets count: {}", assetsList.size()); // Domains filter --domain option String chosenDomain = Property.FILTER_DOMAIN.value().orElse("").toLowerCase(Locale.US); logger.info("-- main() - arg domain substring filter: {}", Property.FILTER_DOMAIN.value()); // Output domains --domains option if (Property.PRINT_DOMAIN_LIST.booleanValue().orElse(false)) { System.out.println("Domains / file count:"); assetsList.stream().filter(a -> a.domain().isPresent()) .map(a -> a.domain().get() + " / " + a.files().size()).sorted() .forEach(System.out::println); System.out.print("Type a domain ('null' to exit): "); chosenDomain = input.next().toLowerCase(Locale.US); if (chosenDomain.equals("null")) return; // TODO check Assets without domain information. } String domainSubstring = chosenDomain; Predicate<Optional<String>> domainFilter = domain -> domain.map(d -> d.toLowerCase(Locale.US)) .map(d -> d.contains(domainSubstring)).orElse(false); List<String> files = Assets.files(assetsList, domainFilter); logger.info("-- main() - domain filtered file count: {}", files.size()); // Output folders. Path outputFolder = Paths.get(Property.OUTPUT_FOLDER.value().orElse("output")); Path assetOutputFolder = outputFolder.resolve("assets"); // TODO assets value injection Path chunkOutputFolder = outputFolder.resolve("chunks"); // TODO chunks value injection logger.info("-- main() - output folder chunks: {}", chunkOutputFolder); logger.info("-- main() - output folder assets: {}", assetOutputFolder); // Download tools. AuthorizeAssets authorizeAssets = AuthorizeAssets.backupd(); DiskChunkStore chunkStore = new DiskChunkStore(chunkOutputFolder); StandardChunkEngine chunkEngine = new StandardChunkEngine(chunkStore); AssetDownloader assetDownloader = new AssetDownloader(chunkEngine); KeyBagManager keyBagManager = backup.newKeyBagManager(); // Mystery Moo. Moo moo = new Moo(authorizeAssets, assetDownloader, keyBagManager); // Filename extension filter. String filenameExtension = Property.FILTER_EXTENSION.value().orElse("").toLowerCase(Locale.US); logger.info("-- main() - arg filename extension filter: {}", Property.FILTER_EXTENSION.value()); Predicate<Asset> assetFilter = asset -> asset.relativePath().map(d -> d.toLowerCase(Locale.US)) .map(d -> d.endsWith(filenameExtension)).orElse(false); // Batch process files in groups of 100. // TODO group files into batches based on file size. List<List<String>> batches = ListUtils.partition(files, 100); for (List<String> batch : batches) { List<Asset> assets = backup.assets(httpClient, batch).stream().filter(assetFilter::test) .collect(Collectors.toList()); logger.info("-- main() - filtered asset count: {}", assets.size()); moo.download(httpClient, assets, assetOutputFolder); } System.out.print("Download other snapshot (Y/N)? "); repeat = input.next().toLowerCase(Locale.US).charAt(0) == 'y'; } while (repeat == true); }
From source file:com.thinkbiganalytics.kerberos.TestKerberosKinit.java
public static void main(String[] args) throws Exception { final TestKerberosKinit testKerberosKinit = new TestKerberosKinit(); Scanner scanner = new Scanner(System.in); System.out.println(" "); System.out.print("Which environment are you in? Enter 1 for HDP or 2 for Cloudera: "); String environmentCode = scanner.nextLine(); if (StringUtils.isEmpty(environmentCode)) { environmentCode = "1"; }/* w ww . j av a 2 s . co m*/ String environment; switch (environmentCode) { case "1": environment = ENVIRONMENT_HDP; break; case "2": environment = ENVIRONMENT_CLOUDERA; break; default: throw new Exception("Invalid environment code"); } System.out.println(" "); System.out.println( "Hit enter to default to: /etc/hadoop/conf/core-site.xml,/etc/hadoop/conf/hdfs-site.xml,/usr/hdp/current/hive-client/conf/hive-site.xml"); System.out.print("Please enter the list of configuration resources: "); String configResources = scanner.nextLine(); if (StringUtils.isEmpty(configResources)) { configResources = "/etc/hadoop/conf/core-site.xml,/etc/hadoop/conf/hdfs-site.xml,/usr/hdp/current/hive-client/conf/hive-site.xml"; } System.out.println(" "); System.out.println("Hit enter to default to: /etc/security/keytabs/hive-thinkbig.headless.keytab"); System.out.print("Please enter the keytab file location: "); String keytab = scanner.nextLine(); if (StringUtils.isEmpty(keytab)) { keytab = "/etc/security/keytabs/hive-thinkbig.headless.keytab"; } System.out.println(" "); System.out.println("Hit enter to default to: hive/sandbox.hortonworks.com@sandbox.hortonworks.com"); System.out.print("Please enter the real user principal name: "); String realUserPrincipal = scanner.nextLine(); if (StringUtils.isEmpty(realUserPrincipal)) { realUserPrincipal = "hive/sandbox.hortonworks.com@sandbox.hortonworks.com"; } System.out.println(" "); System.out.println("Please enter Y/N (default is N)"); System.out.print("Do you want to test with a proxy user: "); String proxyUser = scanner.nextLine(); if (StringUtils.isEmpty(realUserPrincipal)) { proxyUser = "N"; } System.out.println(" "); System.out.println("Hit enter to default to: hdfs://sandbox.hortonworks.com:8020"); System.out.print("Please enter the HDFS URL: "); String hdfsUrl = scanner.nextLine(); if (StringUtils.isEmpty(hdfsUrl)) { hdfsUrl = "hdfs://sandbox.hortonworks.com:8020"; } System.out.println(" "); System.out.println("Hit enter to default to: jdbc:hive2://localhost:10000/default"); System.out.print("Please enter the Hive base connection string: "); String hiveHost = scanner.nextLine(); if (StringUtils.isEmpty(hiveHost)) { hiveHost = "jdbc:hive2://localhost:10000/default"; } String proxyUserName = null; if ("Y".equalsIgnoreCase(proxyUser)) { System.out.println(" "); System.out.print("Please enter the proxy user: "); proxyUserName = scanner.next(); } System.out.println(" "); System.out.println("Executing Kinit to generate a kerberos ticket"); if ("Y".equalsIgnoreCase(proxyUser)) { System.out.println("Testing with the proxy user: " + proxyUserName); testKerberosKinit.testHdfsWithUserImpersonation(configResources, keytab, realUserPrincipal, proxyUserName, environment, hdfsUrl); //testKerberosKinit.testHiveJdbcConnectionWithUserImpersonation(configResources, keytab, realUserPrincipal, proxyUserName); testKerberosKinit.testHiveJdbcConnection(configResources, keytab, realUserPrincipal, proxyUserName, hiveHost); } else { System.out.println("No Proxy User"); testKerberosKinit.testHdfsAsKerberosUser(configResources, keytab, realUserPrincipal, environment, hdfsUrl); testKerberosKinit.testHiveJdbcConnection(configResources, keytab, realUserPrincipal, null, hiveHost); } }
From source file:sql.SQL.java
public static void main(String[] args) { COutput c = new COutput(); String command = "sql>"; String s[] = new String[1000]; Scanner sc = new Scanner(System.in); //import java.util.* String last_w;/*from w w w. j a va 2 s .com*/ String str = null; String pr = null; CreateTable CT = new CreateTable(); boolean InsertWithKey = false; boolean Index = false; ListMultimap<String, String> index_attri = ArrayListMultimap.create(); //key:table, values:atrribute for indexing Map<String, ListMultimap<String, String>> map = new HashMap<>(); Map<String, Hashtable<String, List<String>>> Hashing = new HashMap<>();// key:table name value:Hashing String regEx = "[`~!@#$%^&()+=|{}':',\\[\\]/?~?@#%&+|{}????]"; // delete * Pattern p = Pattern.compile(regEx); //words put into array while (true) { int i = 0, j = 0, k = 0, l = 0; int temp = 0, index = 0; int cal = 0; boolean flag = false; InsertWithKey = false; boolean samekey = false; boolean error = false; Arrays.fill(s, null); System.out.print(command); //preprocessing: put words in array, ; deletion while (true) { s[i] = sc.next(); last_w = String.valueOf(s[i].charAt(s[i].length() - 1)); // ;-->" " if (last_w.equals(";")) { if (s[i].length() > 1) { s[i] = s[i].substring(0, s[i].length() - 1); } else { s[i] = null; } break; } i++; } while (true) { //preprocessing: parenthesis if ((s[2].charAt(s[2].length() - 1) + "").equals("(")) { // x( y ---> x (y if ((s[2].charAt(s[2].length() - 1) + "").equals("(")) { s[2] = s[2].substring(0, s[2].length() - 1); s[3] = "(" + s[3]; } // x(y ---> x (y else if (!s[2].contains("(") && (s[2].charAt(s[2].length() - 1) + "").equals("(")) { while (true) { if ((s[2].charAt(cal) + "").equals("(")) { break; } cal++; } s[3] = s[3].substring(cal, s[2].length()) + s[3]; s[2] = s[2].substring(0, cal); } } // handle x ( y for (k = 0; s[k] != null; k++) { if (s[k].equals("(")) { for (j = k; s[j + 1] != null; j++) { if (j == k) { s[j] = s[j] + s[j + 1]; } else { s[j] = s[j + 1]; } } s[j] = null; } } /* if(s[0].equalsIgnoreCase("CREATE") &&!s[3].contains("(")){ System.out.println("SQL syntax error! Losing left parenthesis..."); break; } if(s[0].equalsIgnoreCase("INSERT") &&!s[4].contains("(")){ System.out.println("SQL syntax error! Losing left parenthesis..."); break; }*/ for (j = 0; s[j] != null; j++) { if (s[j].equalsIgnoreCase("VALUES")) { if (!s[j + 1].contains("(")) { System.out.println("SQL syntax error! Losing left parenthesis..."); break; } } } if (s[0].equalsIgnoreCase("CREATE") && s[k - 2].contains(",") || s[k - 1].contains(",")) { System.out.println("SQL syntax error! Found unnecessary semicolon..."); break; } // remove "," "(" ")" for (j = 0; s[j] != null; j++) { if (s[0].equalsIgnoreCase("SELECT")) { if (s[j].contains(",")) { s[j] = s[j].substring(0, s[j].length() - 1); } } else { if (String.valueOf(s[j].charAt(0)).equals("(")) { s[j] = s[j].substring(1, s[j].length()); } if (s[j].equals(")")) { s[j] = null; break; } if (String.valueOf(s[j].charAt(s[j].length() - 1)).equals(",") || (String.valueOf(s[j].charAt(s[j].length() - 1)).equals(")") && !(Character.isDigit(s[j].charAt(s[j].length() - 2))) && s[0].equalsIgnoreCase("CREATE")) || (String.valueOf(s[j].charAt(s[j].length() - 1)).equals(")") && s[0].equalsIgnoreCase("INSERT"))) { s[j] = s[j].substring(0, s[j].length() - 1); } } } // handle case like: ' John Snow ' int countdot = 0; for (j = 0; s[j] != null; j++) { if (s[j].contains("'")) { countdot++; } if (String.valueOf(s[j].charAt(0)).equals("'") && s[j].length() == 1 && countdot == 1) { for (k = j; s[k + 1] != null; k++) { if (k == j) { s[k] = s[k] + s[k + 1]; } else { s[k] = s[k + 1]; } } s[k] = null; } if (String.valueOf(s[j].charAt(0)).equals("'") && s[j].length() == 1 && countdot == 2) { for (k = j; s[k - 1] != null; k++) { if (k == j) { s[k - 1] = s[k - 1] + s[k]; } else { s[k - 1] = s[k]; } } s[k] = null; } } // handle case like: 'John Snow' String cache; int move; for (j = 0; s[j] != null; j++) { if (s[j].contains("'")) { cache = s[j]; int m; if (String.valueOf(s[j].charAt(0)).equals("'") && String.valueOf(s[j].charAt(s[j].length() - 1)).equals("'")) { if (!s[0].equalsIgnoreCase("SELECT")) { s[j] = s[j].substring(1, s[j].length() - 1); } } else { for (k = j + 1; s[k] != null; k++) { if (s[k].contains("'")) { break; } } for (l = j; l < k; l++) { cache = cache + " " + s[l + 1]; } s[j] = cache; move = k - j; for (m = j + 1; s[m + move] != null; m++) { s[m] = s[m + move]; } for (; s[m] != null; m++) { s[m] = null; } if (!s[0].equalsIgnoreCase("SELECT")) { //System.out.println("**********"); s[j] = s[j].substring(1, s[j].length() - 1); } } } } for (j = 0; s[j] != null; j++) { if (s[j].equalsIgnoreCase("PRIMARY") && s[j + 1].equalsIgnoreCase("KEY")) { for (k = j; s[k + 2] != null; k++) { if (k == j) { s[k - 1] = s[k - 1] + "*"; s[k] = s[k + 2]; } else { s[k] = s[k + 2]; } } s[k] = null; s[k + 1] = null; } } //check for valid Matcher m = p.matcher(s[2]); if (m.find()) { System.out.println("??\n"); break; } else { /*for (j = 0; s[j] != null; j++) { System.out.println(s[j]); }*/ //Syntax c.correct, action. try { ListMultimap<String, String> newmap = ArrayListMultimap.create(); MyLinkedMap<String, String> tables = new MyLinkedMap<>(); // name and alias int aggrenum = 0; ListMultimap<String, String> SelAtr = ArrayListMultimap.create(); // index, <selected attributes, table> List<String> coltitle = new ArrayList<String>(); MyLinkedMap<String, Integer> syntax = new MyLinkedMap<>(); Map<String, BTree<String, List<String>>> BT = new HashMap<>(); // key:index name value:Btree //Hashtable<String, List<String>> ht = new Hashtable<>(); //indexes of syntax int from = 0; //index of FROM int where = 0; int[] as = new int[10]; //The system must allow SQL SELECT from up to 10 attributes in a table. int as_n = 0; boolean innerJoin = false; String key = null; boolean ambuguous = false; c.aggre_b = false; if (s[0].equalsIgnoreCase("CREATE") && s[1].equalsIgnoreCase("INDEX")) { //s[2]: index name //s[4]: table name //s[5]: attribute Index = true; for (i = 0; s[i] != null; i++) { if (s[i + 1] == null) { break; } } if (s[i].equalsIgnoreCase("BTREE")) { BTree<String, List<String>> tree = new BTree<>(); List<String> tuple = new ArrayList<>(); String attriName = null; String temp_tupleData = null; for (i = 0; i < map.get(s[4]).get("attribute").size(); i++) { if (map.get(s[4]).get("attribute").get(i).contains(s[5])) { attriName = map.get(s[4]).get("attribute").get(i); break; } } index_attri.put(s[4], attriName + "-btree"); //System.out.println(tree.height()); if (attriName != null) { for (i = 0; i < map.get(s[4]).get(attriName).size(); i++) { List<String> tempList = new ArrayList<>(); for (j = 0; j < map.get(s[4]).get("attribute").size(); j++) { temp_tupleData = map.get(s[4]) .get(map.get(s[4]).get("attribute").get(j)).get(i); tempList.add(temp_tupleData); } tree.put(String.valueOf(map.get(s[4]).get(attriName).get(i)), tempList); tuple.add(String.valueOf(map.get(s[4]).get(attriName).get(i))); } Collections.sort(tuple); BT.put(s[2], tree); System.out.println("tuple: " + tuple); /*for(i=0; i<map.get(s[4]).get(attriName).size(); i++){ System.out.println("tree: "+ tree.get(tuple.get(i))); }*/ } else { System.out.println("Wrong attribute"); } } else if (s[i].equalsIgnoreCase("HASHING")) { Hashtable<String, List<String>> ht = new Hashtable<>(); //key:index value: data String attriName = null; String temp_tupleData = null; for (i = 0; i < map.get(s[4]).get("attribute").size(); i++) { if (map.get(s[4]).get("attribute").get(i).contains(s[5])) { attriName = map.get(s[4]).get("attribute").get(i); break; } } index_attri.put(s[4], attriName); if (attriName != null) { for (i = 0; i < map.get(s[4]).get(attriName).size(); i++) { List<String> tempList = new ArrayList<>(); for (j = 0; j < map.get(s[4]).get("attribute").size(); j++) { temp_tupleData = map.get(s[4]) .get(map.get(s[4]).get("attribute").get(j)).get(i); tempList.add(temp_tupleData); } //System.out.println("key is: "+map.get(s[4]).get(attriName).get(i)); ht.put(map.get(s[4]).get(attriName).get(i), tempList); } //System.out.println("table is: " + s[4]); Hashing.put(s[4], ht); for (i = 0; i < map.get(s[4]).get(attriName).size(); i++) { System.out .println("hashing: " + ht.get(map.get(s[4]).get(attriName).get(i))); } } else { System.out.println("Wrong attribute"); } } //System.out.println("Hashing: " + Hashing); } else if (s[0].equalsIgnoreCase("SELECT") && Index) { long start = System.currentTimeMillis(); //map:syntax (FROM, index in s[]) (Select From As Where) String table[] = new String[2]; String target = null; //target attibute String targetTable = null; //target's table int targetNum = -1; for (i = 0; s[i] != null; i++) { if (s[i].equalsIgnoreCase("FROM")) { from = i; table[0] = s[i + 1]; table[1] = s[i + 2]; //syntax.put("FROM", from); } else if (s[i].equalsIgnoreCase("WHERE")) { where = i; //syntax.put("WHERE", where); } } if (s[1].contains("SUM") || s[1].contains("COUNT")) { if (s[1].contains(".")) { target = s[1].substring(s[1].indexOf(".") + 1, s[1].length() - 1); targetTable = s[1].substring(s[1].indexOf("(") + 1, s[1].indexOf(".")); //System.out.println("targetTable:" + targetTable); } else { target = s[1].substring(s[1].indexOf("(") + 1, s[1].length() - 1); for (j = from + 1; j < where; j++) { //System.out.println("s----:" + s[j]); //System.out.println(map.get(s[j]).get("attribute").size()); for (k = 0; k < map.get(s[j]).get("attribute").size(); k++) { //System.out.println(map.get(s[j]).get("attribute").get(k)); if (map.get(s[j]).get("attribute").get(k).contains(target)) { targetTable = s[j]; //System.out.println("targetTable:" + targetTable); break; } } } } } for (i = 0; i < map.get(targetTable).get("attribute").size(); i++) { if (map.get(targetTable).get("attribute").get(i).contains(target)) { target = map.get(targetTable).get("attribute").get(i); targetNum = i; break; } } //System.out.println("targetNum:" + targetNum); //System.out.println("targetTable:" + targetTable); //System.out.println("target:" + target); String tempTable = null; String tempAtrri = null; List<String> pos = new ArrayList<>(); int numOfAttri = -1; //order of temp attribute i = where + 1; //System.out.println("i:" + i); if (s[i].contains(".")) { tempTable = s[i].substring(0, s[i].indexOf(".")); for (j = 0; j < map.get(tempTable).get("attribute").size(); j++) { if (map.get(tempTable).get("attribute").get(j) .contains(s[i].substring(s[i].indexOf(".") + 1, s[i].length()))) { numOfAttri = map.get(tempTable).get("attribute") .indexOf(map.get(tempTable).get("attribute").get(j)); tempAtrri = map.get(tempTable).get("attribute").get(j); //System.out.println("tempTable:" + tempTable); //System.out.println("tempAtrri:" + tempAtrri); //System.out.println("numOfAttri:" + numOfAttri); break; } } } else { for (j = from + 1; j < where; j++) { //table for (k = 0; k < map.get(s[j]).get("attribute").size(); k++) { //atrribute if (map.get(s[j]).get("attribute").get(k).contains(s[i])) { numOfAttri = map.get(s[j]).get("attribute") .indexOf(map.get(s[j]).get("attribute").get(k)); tempAtrri = map.get(s[j]).get("attribute").get(k); tempTable = s[j]; //System.out.println("tempTable:" + tempTable); //System.out.println("tempAtrri:" + tempAtrri); //System.out.println("numOfAttri:" + numOfAttri); break; } } } } switch (s[i + 1]) { case ">": for (j = 0; j < map.get(tempTable).get(tempAtrri).size(); j++) { if (Integer.parseInt(map.get(tempTable).get(tempAtrri).get(j)) > Integer .parseInt(s[i + 2])) { //System.out.println("record:" + map.get(tempTable).get(tempAtrri).get(j)); pos.add(map.get(tempTable).get(index_attri.get(tempTable).get(0)).get(j)); //index list } } break; case "<": for (j = 0; j < map.get(tempTable).get(tempAtrri).size(); j++) { if (Integer.parseInt(map.get(tempTable).get(tempAtrri).get(j)) < Integer .parseInt(s[i + 2])) { //System.out.println("record:" + map.get(tempTable).get(tempAtrri).get(j)); pos.add(map.get(tempTable).get(index_attri.get(tempTable).get(0)).get(j)); } } break; case "=": for (j = 0; j < map.get(tempTable).get(tempAtrri).size(); j++) { if (Integer.parseInt(map.get(tempTable).get(tempAtrri).get(j)) == Integer .parseInt(s[i + 2])) { //System.out.println("record:" + map.get(tempTable).get(tempAtrri).get(j)); pos.add(map.get(tempTable).get(index_attri.get(tempTable).get(0)).get(j)); } } break; case "!=": for (j = 0; j < map.get(tempTable).get(tempAtrri).size(); j++) { if (Integer.parseInt(map.get(tempTable).get(tempAtrri).get(j)) != Integer .parseInt(s[i + 2])) { //.out.println("record:" + map.get(tempTable).get(tempAtrri).get(j)); pos.add(map.get(tempTable).get(index_attri.get(tempTable).get(0)).get(j)); } } break; } //System.out.println("pos:" + pos); //todo if (s[1].contains("SUM")) { int sum = 0; if (tempTable.equals(table[0])) { for (i = 0; i < pos.size(); i++) { String temp1 = Hashing.get(tempTable).get(pos.get(i)) .get(Hashing.get(tempTable).get(pos.get(i)).size() - 1); //System.out.println("foreign key:" + temp1); if (Hashing.get(targetTable).get(temp1) != null) { sum += Integer .parseInt(Hashing.get(targetTable).get(temp1).get(targetNum)); } } } else if (tempTable.equals(table[1])) { Set<String> keys = Hashing.get(table[0]).keySet(); Iterator<String> itr = keys.iterator(); while (itr.hasNext()) { str = itr.next(); //System.out.println("str:" + str); //System.out.println("---" + Hashing.get(table[0]).get(str).get(Hashing.get(table[0]).get(str).size() - 1)); if (pos.contains(Hashing.get(table[0]).get(str) .get(Hashing.get(table[0]).get(str).size() - 1))) { //System.out.println("num: " + Hashing.get(targetTable).get(str).get(targetNum)); sum += Integer .parseInt(Hashing.get(targetTable).get(str).get(targetNum)); } } } System.out.println("------------------------------------------------------"); System.out.println("sum:" + sum); System.out .println("Time cost with select: " + (System.currentTimeMillis() - start)); } else if (s[1].contains("COUNT")) { int count = 0; if (tempTable.equals(table[0])) { for (i = 0; i < pos.size(); i++) { String temp1 = Hashing.get(tempTable).get(pos.get(i)) .get(Hashing.get(tempTable).get(pos.get(i)).size() - 1); //System.out.println("foreign key:" + temp1); if (Hashing.get(targetTable).get(temp1) != null) { count++; } } } else if (tempTable.equals(table[1])) { Set<String> keys = Hashing.get(table[0]).keySet(); Iterator<String> itr = keys.iterator(); while (itr.hasNext()) { str = itr.next(); //System.out.println("str:" + str); //System.out.println("---" + Hashing.get(table[0]).get(str).get(Hashing.get(table[0]).get(str).size() - 1)); if (pos.contains(Hashing.get(table[0]).get(str) .get(Hashing.get(table[0]).get(str).size() - 1))) { //System.out.println("num: " + Hashing.get(targetTable).get(str).get(targetNum)); count++; } } } System.out.println("------------------------------------------------------"); System.out.println("count:" + count); System.out .println("Time cost with select: " + (System.currentTimeMillis() - start)); } } else if (s[0].equalsIgnoreCase("SELECT") && !Index) { //map:syntax (FROM, index in s[]) (Se Fr As Wh) for (i = 0; s[i] != null; i++) { if (s[i].equalsIgnoreCase("FROM")) { from = i; syntax.put("FROM", from); } else if (s[i].equalsIgnoreCase("WHERE")) { where = i; syntax.put("WHERE", where); } else if (s[i].equalsIgnoreCase("AS")) { as[as_n] = i; syntax.put("AS", as[as_n]); as_n++; } } for (i = 1; i < from; i++) { //select if (!s[i].contains(".") && (where - from) > 1) { System.out.println(map.get(s[from + 1]).get("attribute").size()); for (j = 0; j < map.get(s[from + 1]).get("attribute").size(); j++) { if (map.get(s[from + 1]).get("attribute").get(j).contains(s[i])) { for (k = 0; k < map.get(s[from + 2]).get("attribute").size(); k++) { if (map.get(s[from + 2]).get("attribute").get(k).contains(s[i])) { System.out.println("Ambuguous Input!!"); ambuguous = true; break; } } } } } } if (ambuguous) { break; } //map:tables (realname, alias) (Se Fr '*' As '*' Wh) if (where != 0) { for (i = from + 1; i < syntax.getValue(1); i++) { if (s[i + 1].equalsIgnoreCase("AS")) { tables.put(s[i], s[i + 2]); i = i + 2; } else if (!s[i - 1].equalsIgnoreCase("AS") && !s[i + 1].equalsIgnoreCase("AS")) { tables.put(s[i], null); } } } else { if (s[from + 2] != null) { for (i = from + 1; s[i] != null; i++) { //System.out.println(i); if (s[i + 1].equalsIgnoreCase("AS")) { tables.put(s[i], s[i + 2]); i = i + 2; } else { tables.put(s[i], null); } } } else { tables.put(s[from + 1], null); } } System.out.println(tables); //SELECT aggregation function ?table table-->ex. SELECT a.friendid, SUM(b.number) //SELECT COUNT(*) FROM Book; HERE!!! ??without"a." //SELECT COUNT(*)FROM Author WHERE nationality = 'Taiwan'; //SELECT COUNT(editorial) FROM Book; //SELECT SUM(pages) FROM Book WHERE authorId = 2; // //multimap:SelAtr (index, attribute, table-real-name) (Se '*' Fr * As * Wh) for (i = 1; i < from; i++) {//@check one attribute: [stuID] or [a.stuId] or [COUNT(stuID)] or [COUNT(a.stuID)] String table1; String attribute1; //String[] atri = new String[2]; //store A.name as name and A //aggregation bool if (s[i].toUpperCase().contains("COUNT") || s[i].toUpperCase().contains("SUM")) { c.aggre = s[i].substring(0, s[i].indexOf("(")).toUpperCase(); c.aggre_b = true; } System.out.println(c.aggre_b); System.out.println("Im here1"); c.atri(s, i, tables, map); System.out.println("Im here2"); table1 = c.table; System.out.println("table1:" + table1); attribute1 = c.attribute; System.out.println("attribute1:" + attribute1); if (attribute1.equals("*")) { for (k = 0; k < map.get(c.table).get("attribute").size(); k++) { SelAtr.put(c.table, map.get(c.table).get("attribute").get(k)); String coll; if (c.pt_attribute.contains(".")) { int len_todot = c.pt_attribute.indexOf("."); coll = c.pt_attribute.substring(0, len_todot) + map.get(c.table).get("attribute").get(k); } else { coll = map.get(c.table).get("attribute").get(k); } coltitle.add(coll); } } else { for (j = 0; j < map.get(table1).get("attribute").size(); j++) { //System.out.println("000000000000" + map.get(table1).get("attribute").get(j)); if (map.get(table1).get("attribute").get(j).contains(attribute1)) { SelAtr.put(table1, map.get(table1).get("attribute").get(j)); coltitle.add(attribute1); } } } } //end of [attributes --> SelAtr] for loop System.out.println(SelAtr); System.out.println(coltitle); System.out.println("Im here1"); boolean compare = false; // if there is no WHERE clause, set the c.correct if (where == 0) { System.out.println("im here in where"); String atriTemp; String table_0 = tables.getKey(0); atriTemp = map.get(table_0).get("attribute").get(0); for (i = 0; i < map.get(table_0).get(atriTemp).size(); i++) { c.correct.add(i); } if (tables.getKey(1) != null) { String table_1 = tables.getKey(1); atriTemp = map.get(table_1).get("attribute").get(0); for (i = 0; i < map.get(table_1).get(atriTemp).size(); i++) { c.correct2.add(i); } } } else if (where != 0) {//if where is written boolean andor = false; boolean and = false; boolean or = false; int mapcount; for (i = where + 1; s[i] != null; i = i + 4) { //i=s[], start from where System.out.println("--------------------" + i); /*1*/ String table1, attribute1; //---> atri[0](attri) atri[1](table-real name) /*2*/ String opt; /*3*/ String third; /*4*/ //bool : AND if (s[i + 3] != null) { if (s[i + 3].equalsIgnoreCase("AND")) { and = true; andor = true; } else if (s[i + 3].equalsIgnoreCase("OR")) { or = true; andor = true; } } System.out.println("Im here 2"); c.atri(s, i, tables, map); table1 = c.table; attribute1 = c.attribute; System.out.println("table1:" + table1); System.out.println("attribute1:" + attribute1); /*2*/ //opt opt = s[i + 1]; System.out.println("opt:" + opt); /*3*/ //third third = s[i + 2]; System.out.println("third:" + third); for (mapcount = 0; mapcount < map.get(table1).get("attribute") .size(); mapcount++) { if (map.get(table1).get("attribute").get(mapcount).contains(attribute1)) { System.out.println(map.get(table1).get("attribute").get(mapcount)); break; } } int at_size = map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)).size(); // the size of the selected attribute (compared to the number) //System.out.println("at_size:" + at_size); /*case 1*/ //+num if (StringUtils.isNumeric(third)) { System.out.println( "im here 3" + map.get(table1).get("attribute").get(mapcount)); int num = Integer.valueOf(third); // the number switch (opt) { case ">": //System.out.println(">>>>>>>"); for (j = 0; j < at_size; j++) { if (Integer.valueOf(map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)) .get(j)) > num) { c.COutput(map, table1, j, tables, compare); } } break; case "<": for (j = 0; j < at_size; j++) { if (Integer.valueOf(map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)) .get(j)) < num) { c.COutput(map, table1, j, tables, compare); } } break; case "=": for (j = 0; j < at_size; j++) { //System.out.println(j); if (Integer.valueOf(map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)) .get(j)) == num) { //System.out.println(map.get(table1).get(map.get(table1).get("attribute").get(mapcount)).get(j)); c.COutput(map, table1, j, tables, compare); } } break; case "<>": for (j = 0; j < at_size; j++) { if (Integer.valueOf(map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)) .get(j)) != num) { c.COutput(map, table1, j, tables, compare); } } break; default: break; } } // end if third = integer /*case 2*/ //+string else if (!third.contains(".") && !StringUtils.isNumeric(third)) { System.out.println("In case 2----------------"); third = third.substring(1, third.length() - 1); System.out.println("third:" + third); if (opt.equals("<>")) { for (j = 0; j < at_size; j++) { if (!map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)).get(j) .equals(third)) { c.COutput(map, table1, j, tables, compare); } } } else if (opt.equals("=")) { for (j = 0; j < map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)) .size(); j++) { System.out.println(map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)) .get(j)); if (map.get(table1) .get(map.get(table1).get("attribute").get(mapcount)).get(j) .equals(third)) { c.COutput(map, table1, j, tables, compare); } } } } else { /*case 3*/ //inner join a.studentID = b.studentID //String table2, attribute2; //process third dot c.atri(s, i, tables, map); innerJoin = true; key = s[i].substring(s[i].indexOf(".") + 1, s[i].length()); System.out.println("XXXXXXXXXXXXXXX:" + key); } System.out.println(c.correct); System.out.println(c.correct2); if (compare) { if (and && !innerJoin) { c.correct.retainAll(c.correct2); Set<Integer> hs = new HashSet<>(); hs.addAll(c.correct); c.correct.clear(); c.correct.addAll(hs); } else if (or && !innerJoin) { c.correct.addAll(c.correct2); Set<Integer> hs = new HashSet<>(); hs.addAll(c.correct); c.correct.clear(); c.correct.addAll(hs); } } System.out.println(c.correct); if (andor) { compare = true; } else { compare = false; } System.out.println("AND:" + and); System.out.println("OR:" + or); System.out.println("andor:" + andor); System.out.println("compare:" + compare); } if (c.aggre_b == true) { if (c.aggre.equalsIgnoreCase("COUNT")) { if (c.table.equals(tables.getKey(0))) { aggrenum = c.correct.size(); } else { aggrenum = c.correct2.size(); } } else if (c.aggre.equalsIgnoreCase("SUM")) { int value; List<Integer> cor; if (c.table.equals(tables.getKey(0))) { cor = c.correct; } else { cor = c.correct2; } for (i = 0; i < cor.size(); i++) { value = Integer .valueOf(map.get(c.table).get(c.attribute).get(cor.get(i))); aggrenum = aggrenum + value; } } else { System.out.println("Wrong aggregation function."); } } } //print!!! System.out.println("coltitle" + coltitle); System.out.println("tables" + tables); System.out.println("correct" + c.correct); System.out.println("SelAtr" + SelAtr); //System.out.println("table" + c.table); System.out.println("-----------------------------------------------------------"); String tab; if (innerJoin) { String biggerTable, smalTable, temp1; String attiTemp1 = null, attiTemp2 = null; if (tables.getKey(0).equals(c.table)) { tab = tables.getKey(1); } else { tab = tables.getKey(0); } for (i = 0; i < map.get(c.table).get("attribute").size(); i++) { //System.out.println("key:" + map.get(c.table).get("attribute").get(i)); if (map.get(c.table).get("attribute").get(i).contains(key)) { attiTemp1 = map.get(c.table).get("attribute").get(i); //System.out.println("key:" + attiTemp1); break; } } for (j = 0; j < map.get(tab).get("attribute").size(); j++) { //System.out.println("key:" + map.get(tab).get("attribute").get(l)); if (map.get(tab).get("attribute").get(j).contains(key)) { attiTemp2 = map.get(tab).get("attribute").get(j); //System.out.println("key:" + attiTemp2); break; } } if (map.get(c.table).get(attiTemp1).size() > map.get(tab).get(attiTemp2).size()) { biggerTable = c.table; smalTable = tab; } else { temp1 = attiTemp2; attiTemp2 = attiTemp1; attiTemp1 = temp1; biggerTable = tab; smalTable = c.table; } /* System.out.println(smalTable); System.out.println("key:" + attiTemp2); System.out.println(biggerTable); System.out.println("key:" + attiTemp1);*/ /*for (j = 0; j < c.correct2.size(); j++) { for (k = 0; k < map.get(tab).get(map.get(tab).get("attribute").get(l)).size(); k++) { //System.out.println(tab+":"+map.get(tab).get(key).get(k)); //System.out.println(c.table+"r:"+map.get(c.table).get(map.get(c.table).get("attribute").get(i)).get(c.correct2.get(j))); if (map.get(tab).get(map.get(tab).get("attribute").get(l)).get(k).equals(map.get(c.table).get(map.get(c.table).get("attribute").get(i)).get(c.correct2.get(j)))) { c.correct.add(k); } } }*/ for (l = 0; l < map.get(biggerTable) .get(map.get(biggerTable).get("attribute").get(0)).size(); l++) { c.correct.add(l); } //System.out.println("******************"); String[] stringArray = Arrays.copyOf(SelAtr.keySet().toArray(), SelAtr.keySet().toArray().length, String[].class); //System.out.println("SelAtr.size()" + SelAtr.size()); if (stringArray.length == 1) { if (!compare) { /* for (k = 0; k < c.correct.size(); k++) { System.out.println(k); for (String set : SelAtr.keySet()) { // System.out.println("Set:"+SelAtr.get(set).size()); for (i = 0; i < SelAtr.get(set).size(); i++) { //System.out.println(set); System.out.format("%30s", map.get(set).get(SelAtr.get(set).get(i)).get(c.correct.get(k))); } } System.out.println(); }*/ } else { c.correct.clear(); List<Integer> tempList = new ArrayList<>(); //System.out.println("c.correct2.size():"+c.correct2.size()); for (i = 0; i < map.get(biggerTable).get(attiTemp1).size(); i++) { for (j = 0; j < c.correct2.size(); j++) { if (map.get(biggerTable).get(attiTemp1).get(i).equals( map.get(smalTable).get(attiTemp2).get(c.correct2.get(j)))) { c.correct.add(i); tempList.add(c.correct2.get(j)); break; } } } for (k = 0; k < c.correct.size(); k++) { for (j = 0; j < SelAtr.get(biggerTable).size(); j++) { System.out.format("%30s", map.get(biggerTable) .get(SelAtr.get(biggerTable).get(j)).get(c.correct.get(k))); } for (i = 0; i < SelAtr.get(smalTable).size(); i++) { System.out.format("%30s", map.get(smalTable) .get(SelAtr.get(smalTable).get(i)).get(tempList.get(k))); } System.out.println(); } } } else { if (!compare) { for (i = 0; i < c.correct.size(); i++) { for (j = 0; j < map.get(smalTable).get(attiTemp2).size(); j++) { if (map.get(biggerTable).get(attiTemp1).get(i).equals( map.get(smalTable).get(attiTemp2).get(c.correct.get(j)))) { c.correct2.add(j); break; } if (j == map.get(smalTable).get(attiTemp2).size() - 1) { c.correct.remove(i); } } } System.out.println(c.correct); System.out.println(c.correct2); for (k = 0; k < c.correct.size(); k++) { for (j = 0; j < SelAtr.get(smalTable).size(); j++) { System.out.format("%30s", map.get(smalTable) .get(SelAtr.get(smalTable).get(j)).get(c.correct2.get(k))); } for (i = 0; i < SelAtr.get(biggerTable).size(); i++) { System.out.format("%30s", map.get(biggerTable) .get(SelAtr.get(biggerTable).get(i)).get(c.correct.get(k))); } System.out.println(); } } else { if (c.table.equals(biggerTable)) { for (i = 0; i < c.correct2.size(); i++) { for (j = 0; j < map.get(smalTable).get(attiTemp2).size(); j++) { if (map.get(smalTable).get(attiTemp2).get(j) .equals(map.get(biggerTable).get(attiTemp1) .get(c.correct2.get(i)))) { c.correct.add(j); } } } for (k = 0; k < c.correct2.size(); k++) { for (j = 0; j < SelAtr.get(smalTable).size(); j++) { System.out.format("%30s", map.get(smalTable).get(SelAtr.get(smalTable).get(j)) .get(c.correct.get(k))); } for (i = 0; i < SelAtr.get(biggerTable).size(); i++) { System.out.format("%30s", map.get(biggerTable).get(SelAtr.get(biggerTable).get(i)) .get(c.correct2.get(k))); } System.out.println(); } } else { c.correct.clear(); List<Integer> tempList = new ArrayList<>(); //System.out.println("c.correct2.size():"+c.correct2.size()); for (i = 0; i < map.get(biggerTable).get(attiTemp1).size(); i++) { for (j = 0; j < c.correct2.size(); j++) { //System.out.println("1----"+map.get(biggerTable).get(attiTemp1).get(i)); //System.out.println("2----"+map.get(smalTable).get(attiTemp2).get(c.correct2.get(j))); if (map.get(biggerTable).get(attiTemp1).get(i) .equals(map.get(smalTable).get(attiTemp2) .get(c.correct2.get(j)))) { //System.out.println("**************************"+j); //System.out.println("1----"+map.get(biggerTable).get(attiTemp1).get(i)); //System.out.println("2----"+map.get(smalTable).get(attiTemp2).get(c.correct2.get(j))); c.correct.add(i); tempList.add(c.correct2.get(j)); break; } } } System.out.println(c.correct); System.out.println(tempList); for (k = 0; k < c.correct.size(); k++) { for (j = 0; j < SelAtr.get(biggerTable).size(); j++) { System.out.format("%30s", map.get(biggerTable).get(SelAtr.get(biggerTable).get(j)) .get(c.correct.get(k))); } for (i = 0; i < SelAtr.get(smalTable).size(); i++) { System.out.format("%30s", map.get(smalTable).get(SelAtr.get(smalTable).get(i)) .get(tempList.get(k))); } System.out.println(); } } } /*for (k = 0; k < c.correct.size(); k++) { if (c.table.equals(stringArray[0])) { for (j = 0; j < SelAtr.get(stringArray[0]).size(); j++) { System.out.format("%30s", map.get(stringArray[0]).get(SelAtr.get(stringArray[0]).get(j)).get(c.correct2.get(k))); } for (i = 0; i < SelAtr.get(stringArray[1]).size(); i++) { System.out.format("%30s", map.get(stringArray[1]).get(SelAtr.get(stringArray[1]).get(i)).get(c.correct.get(k))); } System.out.println(); } else if (c.table.equals(stringArray[1])) { for (j = 0; j < SelAtr.get(stringArray[0]).size(); j++) { System.out.format("%30s", map.get(stringArray[0]).get(SelAtr.get(stringArray[0]).get(j)).get(c.correct.get(k))); } for (i = 0; i < SelAtr.get(stringArray[1]).size(); i++) { System.out.format("%30s", map.get(stringArray[1]).get(SelAtr.get(stringArray[1]).get(i)).get(c.correct2.get(k))); } System.out.println(); } }*/ } //System.out.println(c.correct); } else if (!c.aggre_b && !innerJoin) { if (SelAtr.get(tables.getKey(0)) != null) { //?table? --> correct //String tab = tables.getKey(0); for (k = 0; k < c.correct.size(); k++) { for (String set : SelAtr.keySet()) { for (i = 0; i < SelAtr.get(set).size(); i++) { System.out.format("%30s", map.get(set).get(SelAtr.get(set).get(i)) .get(c.correct.get(k))); } } System.out.println(); } } } else { System.out.println(c.correct.size()); } c.correct.clear(); c.correct2.clear(); //create table, insert values } else if (s[0].equalsIgnoreCase("CREATE") && s[1].equalsIgnoreCase("TABLE")) { if (!map.containsKey(s[2])) { map.put(s[2], CT.Create(s)); System.out.println("Table [" + s[2] + "] has been created successfully\n"); } else { System.out.println("Table [" + s[2] + "] has already existed\n"); } break; } else if (s[0].equalsIgnoreCase("INSERT") && s[1].equalsIgnoreCase("INTO")) { if (map.containsKey(s[2])) { for (i = 0; i < map.get(s[2]).get("attribute").size(); i++) { if (map.get(s[2]).get("attribute").get(i).contains("*")) { InsertWithKey = true; //System.out.println("PR:"+InsertWithKey); } } if (!InsertWithKey) { if (s[3].equalsIgnoreCase("VALUES")) { int attri_count = map.get(s[2]).get("attribute").size(); for (j = 4, k = 0; s[j] != null; j++, k++) { //System.out.println("attribute: " + (map.get(s[2]).get("attribute")).get(k)); //System.out.println("data: " + s[j]); if ((map.get(s[2]).get("attribute")).get(k).contains(")")) { int num = Integer .parseInt((map.get(s[2]).get("attribute")).get(k).substring( (map.get(s[2]).get("attribute")).get(k).indexOf("(") + 1, (map.get(s[2]).get("attribute")).get(k) .indexOf(")"))); //System.out.println(num); if (s[j].length() > num) { System.out.println(s[j] + " exceed varchar limit of " + num); error = true; break; } } else { if (s[j].charAt(0) == '-') { if (!StringUtils.isNumeric(s[j].substring(1))) { System.out.println(s[j] + " not in int type"); error = true; break; } } else { if (!StringUtils.isNumeric(s[j])) { System.out.println(s[j] + " not in int type"); error = true; break; } } } } if (!error) { for (j = 4, k = 0; s[j] != null; j++, k++) { System.out.println( "attribute: " + (map.get(s[2]).get("attribute")).get(k)); System.out.println("data: " + s[j]); map.get(s[2]).put((map.get(s[2]).get("attribute")).get(k), s[j]); } for (k = 0; k < attri_count; k++) { System.out.println( "attribute: " + (map.get(s[2]).get("attribute")).get(k)); str = map.get(s[2]).get("attribute").get(k); System.out.println(map.get(s[2]).get(str)); } } } else { int attri_count = map.get(s[2]).get("attribute").size(); for (i = 0; s[i] != null; i++) { if (s[i].equalsIgnoreCase("VALUES")) { break; } } for (j = 3, k = i + 1; !s[j].equalsIgnoreCase("VALUES"); j++, k++) { //System.out.println("attribute: " + s[j]); //System.out.println("data: " + s[k]); for (l = 0; l < attri_count; l++) { str = map.get(s[2]).get("attribute").get(l); //System.out.println("str: " + str); if (str.contains(s[j])) { break; } } if (str.contains(")")) { int num = Integer.parseInt( str.substring(str.indexOf("(") + 1, str.indexOf(")"))); //System.out.println(num); if (s[k].length() > num) { System.out.println(s[k] + " exceed varchar limit of " + num); error = true; break; } } else { if (s[k].charAt(0) == '-') { if (!StringUtils.isNumeric(s[k].substring(1))) { System.out.println(s[k] + " not in int type"); error = true; break; } } else { if (!StringUtils.isNumeric(s[k])) { System.out.println(s[k] + " not in int type"); error = true; break; } } } } if (!error) { for (j = 3, k = i + 1; !s[j].equalsIgnoreCase("VALUES"); j++, k++) { System.out.println("attribute: " + s[j]); System.out.println("data: " + s[k]); for (l = 0; l < attri_count; l++) { str = map.get(s[2]).get("attribute").get(l); //System.out.println("str: " + str); if (str.contains(s[j])) { break; } } map.get(s[2]).put(str, s[k]); } for (k = 0; k < attri_count; k++) { System.out.println( "attribute: " + (map.get(s[2]).get("attribute")).get(k)); str = map.get(s[2]).get("attribute").get(k); System.out.println(map.get(s[2]).get(str)); } } } } // PR else { if (s[3].equalsIgnoreCase("VALUES")) { int attri_count = map.get(s[2]).get("attribute").size(); //find primary key for (i = 0; i < attri_count; i++) { if (map.get(s[2]).get("attribute").get(i).contains("*")) { pr = map.get(s[2]).get("attribute").get(i); //primary key break; } } for (j = 0; j < map.get(s[2]).get(pr).size(); j++) { if (s[4 + i].equals(map.get(s[2]).get(pr).get(j))) { samekey = true; System.out.println("Error! Invalid key..."); break; } } if (samekey == false) { for (j = 4, k = 0; s[j] != null; j++, k++) { //System.out.println("attribute: " + (map.get(s[2]).get("attribute")).get(k)); //System.out.println("data: " + s[j]); if ((map.get(s[2]).get("attribute")).get(k).contains(")")) { int num = Integer.parseInt( (map.get(s[2]).get("attribute")).get(k).substring( (map.get(s[2]).get("attribute")).get(k) .indexOf("(") + 1, (map.get(s[2]).get("attribute")).get(k) .indexOf(")"))); //System.out.println(num); if (s[j].length() > num) { System.out .println(s[j] + " exceed varchar limit of " + num); error = true; break; } } else { if (s[j].charAt(0) == '-') { if (!StringUtils.isNumeric(s[j])) { System.out.println(s[j] + " not in int type"); error = true; break; } } else { if (!StringUtils.isNumeric(s[j].substring(1))) { System.out.println(s[j] + " not in int type"); error = true; break; } } } } if (!error) { for (j = 4, k = 0; s[j] != null; j++, k++) { System.out.println("attribute: " + (map.get(s[2]).get("attribute")).get(k)); System.out.println("data: " + s[j]); map.get(s[2]).put((map.get(s[2]).get("attribute")).get(k), s[j]); } for (k = 0; k < attri_count; k++) { System.out.println("attribute: " + (map.get(s[2]).get("attribute")).get(k)); str = map.get(s[2]).get("attribute").get(k); System.out.println(map.get(s[2]).get(str)); } } } } else { int attri_count = map.get(s[2]).get("attribute").size(); for (l = 0; s[l] != null; l++) { if (s[l].equalsIgnoreCase("VALUES")) { break; } } for (i = 0; i < attri_count; i++) { if (map.get(s[2]).get("attribute").get(i).contains("*")) { pr = map.get(s[2]).get("attribute").get(i); //primary key break; } } for (j = 3; j < l; j++) { System.out.println(pr.substring(0, pr.length() - 1)); System.out.println(s[j]); if (pr.contains(s[j])) { break; } } for (k = 0; k < map.get(s[2]).get(pr).size(); k++) { if (s[j - 2 + l].equals(map.get(s[2]).get(pr).get(k))) { samekey = true; System.out.println("Error! Invalid key..."); break; } } if (samekey == false) { for (j = 3, k = l + 1; !s[j].equalsIgnoreCase("VALUES"); j++, k++) { //System.out.println("attribute: " + s[j]); //System.out.println("data: " + s[k]); for (int n = 0; n < attri_count; n++) { str = map.get(s[2]).get("attribute").get(n); //System.out.println("str: " + str); if (str.contains(s[j])) { break; } } if (str.contains(")")) { int num = Integer.parseInt( str.substring(str.indexOf("(") + 1, str.indexOf(")"))); //System.out.println(num); if (s[k].length() > num) { System.out .println(s[k] + " exceed varchar limit of " + num); error = true; break; } } else { if (s[k].charAt(0) == '-') { if (!StringUtils.isNumeric(s[k].substring(1))) { System.out.println(s[k] + " not in int type"); error = true; break; } } else { if (!StringUtils.isNumeric(s[k])) { System.out.println(s[k] + " not in int type"); error = true; break; } } } } if (!error) { for (j = 3, k = l + 1; !s[j].equalsIgnoreCase("VALUES"); j++, k++) { System.out.println("attribute: " + s[j]); System.out.println("data: " + s[k]); for (int n = 0; n < attri_count; n++) { str = map.get(s[2]).get("attribute").get(n); //System.out.println("str: " + str); if (str.contains(s[j])) { break; } } map.get(s[2]).put(str, s[k]); } for (k = 0; k < attri_count; k++) { System.out.println("attribute: " + (map.get(s[2]).get("attribute")).get(k)); str = map.get(s[2]).get("attribute").get(k); System.out.println(map.get(s[2]).get(str)); } } } } } } else { System.out.println("No such table"); } } break; } catch (Exception e) { System.out.println(e.getClass().getCanonicalName()); e.printStackTrace(); System.out.println("SQL"); break; } } } } }
From source file:Main.java
static void parseLine(String line) { Scanner lineScanner = new Scanner(line); lineScanner.useDelimiter("\\s*,\\s*"); String name = lineScanner.next(); int age = lineScanner.nextInt(); boolean isCertified = lineScanner.nextBoolean(); System.out.println("It is " + isCertified + " that " + name + ", age " + age + ", is certified."); }
From source file:Main.java
public static String convertStreamToString(java.io.InputStream is) { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
From source file:Main.java
private static String drainStream(InputStream in) { Scanner s = new Scanner(in).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
From source file:Main.java
static String convertStreamToString(InputStream is) { Scanner s = new Scanner(is, "utf-8").useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
From source file:Main.java
public static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }