List of usage examples for com.google.common.collect ArrayListMultimap create
public static <K, V> ArrayListMultimap<K, V> create()
From source file:PCC.java
/** * @param args the command line arguments * @throws java.io.IOException/* w ww. j a v a 2 s. c o m*/ */ public static void main(String[] args) throws IOException { // TODO code application logic here PearsonsCorrelation corel = new PearsonsCorrelation(); PCC method = new PCC(); ArrayList<String> name = new ArrayList<>(); Multimap<String, String> genes = ArrayListMultimap.create(); BufferedWriter bw = new BufferedWriter(new FileWriter(args[1])); BufferedReader br = new BufferedReader(new FileReader(args[0])); String str; while ((str = br.readLine()) != null) { String[] a = str.split("\t"); name.add(a[0]); for (int i = 1; i < a.length; i++) { genes.put(a[0], a[i]); } } for (String key : genes.keySet()) { double[] first = new double[genes.get(key).size()]; int element1 = 0; for (String value : genes.get(key)) { double d = Double.parseDouble(value); first[element1] = d; element1++; } for (String key1 : genes.keySet()) { if (!key.equals(key1)) { double[] second = new double[genes.get(key1).size()]; int element2 = 0; for (String value : genes.get(key1)) { double d = Double.parseDouble(value); second[element2] = d; element2++; } double corrlation = corel.correlation(first, second); if (corrlation > 0.5) { bw.write(key + "\t" + key1 + "\t" + corrlation + "\t" + method.pvalue(corrlation, second.length) + "\n"); } } } } br.close(); bw.close(); }
From source file:org.terasology.documentation.BindingScraper.java
/** * @param args (ignored)//from w w w . ja v a 2 s . co m * @throws Exception if the module environment cannot be loaded */ public static void main(String[] args) throws Exception { ModuleManager moduleManager = ModuleManagerFactory.create(); // Holds normal input mappings where there is only one key Multimap<InputCategory, String> categories = ArrayListMultimap.create(); Multimap<String, Input> keys = ArrayListMultimap.create(); Map<String, String> desc = new HashMap<>(); for (Class<?> holdingType : moduleManager.getEnvironment().getTypesAnnotatedWith(InputCategory.class)) { InputCategory inputCategory = holdingType.getAnnotation(InputCategory.class); categories.put(inputCategory, null); for (String button : inputCategory.ordering()) { categories.put(inputCategory, button); } } for (Class<?> buttonEvent : moduleManager.getEnvironment() .getTypesAnnotatedWith(RegisterBindButton.class)) { DefaultBinding defBinding = buttonEvent.getAnnotation(DefaultBinding.class); RegisterBindButton info = buttonEvent.getAnnotation(RegisterBindButton.class); String cat = info.category(); String id = "engine:" + info.id(); desc.put(id, info.description()); if (cat.isEmpty()) { InputCategory inputCategory = findEntry(categories, id); if (inputCategory == null) { System.out.println("Invalid category for: " + info.id()); } } else { InputCategory inputCategory = findCategory(categories, cat); if (inputCategory != null) { categories.put(inputCategory, id); } else { System.out.println("Invalid category for: " + info.id()); } } if (defBinding != null) { // This handles bindings with just one key Input input = defBinding.type().getInput(defBinding.id()); keys.put(id, input); } else { // See if there is a multi-mapping for this button DefaultBindings multiBinding = buttonEvent.getAnnotation(DefaultBindings.class); // Annotation math magic. We're expecting a DefaultBindings containing one DefaultBinding pair if (multiBinding != null && multiBinding.value().length == 2) { DefaultBinding[] bindings = multiBinding.value(); Input primary = bindings[0].type().getInput(bindings[0].id()); Input secondary = bindings[1].type().getInput(bindings[1].id()); keys.put(id, primary); keys.put(id, secondary); } } } for (InputCategory row : categories.keySet()) { System.out.println("# " + row.displayName()); categories.get(row).stream().filter(entry -> entry != null) .forEach(entry -> System.out.println(desc.get(entry) + ": " + keys.get(entry))); } }
From source file:DataStructures.DataStructures.java
/** * @param args the command line arguments *//*from w w w . ja v a 2s .com*/ public static void main(String[] args) { Queue<Integer> pp = new LinkedList<>(); // pp.offer(); // pp.peek(); // pp.poll(); Queue<Integer> heap = new PriorityQueue<>((a, b) -> b - a); // max heap Set<Character> set; heap.offer(5); heap.offer(22); heap.offer(3); heap.offer(7); System.out.println(heap.poll()); Hashtable<String, Integer> hashTable = new Hashtable<>(); // put, containsKey HashSet ss = new HashSet(); // add Stack<Integer> stack = new Stack<>(); LinkedList<String> list = new LinkedList<>(); // list.stream().fil PriorityQueue<String> queue = new PriorityQueue<>(); // max heap?? Deque<String> deck = new ArrayDeque<>(); Map<String, Integer> map = new HashMap<>(); // Google Guava data structures Multimap<String, Integer> multiMap = ArrayListMultimap.create(); Table<String, Integer, Integer> table = HashBasedTable.create(); // Hashtable hashTable.put("key", 0); hashTable.put("bob", 1); hashTable.get("key"); hashTable.containsKey("key"); hashTable.containsValue(0); hashTable.remove("key"); hashTable.clear(); hashTable.size(); // Stack stack.add(0); // add to top of stack stack.peek(); // look at top of stack stack.pop(); // pop and return value from top of stack // stack.firstElement(); // returns bottom, first element // stack.remove(0); // remove value at that index stack.size(); // List list.add("added to end"); list.addFirst("now this is the first element"); list.addLast("this is now actually the end"); list.isEmpty(); list.size(); // Proprity Queue queue.add("first Element in and first element out"); // adds to front of queue queue.peek(); // returns value of what is in front of queue, or null queue.poll(); // returns AND REMOVES what is at the head of the queue, or null queue.remove("tryAndRemoveThis"); // Deque deck.add("first Element in deck"); deck.addFirst("now this is the first one"); deck.addLast("this is the last one"); deck.peekFirst(); // gets value of first deck.peekLast(); // gets value of last element deck.removeFirst(); deck.removeLast(); }
From source file:com.netflix.genie.client.sample.ExecutionServiceSampleClient.java
/** * Main for running client code ./*from w w w.j a va 2s . c o m*/ * * @param args command line arguments * @throws Exception On any issue. */ public static void main(final String[] args) throws Exception { // Initialize Eureka, if it is being used // LOG.info("Initializing Eureka"); // ExecutionServiceClient.initEureka("test"); LOG.info("Initializing list of Genie servers"); ConfigurationManager.getConfigInstance().setProperty("genie2Client.ribbon.listOfServers", "http://localhost:7001"); LOG.info("Initializing ExecutionServiceClient"); final ExecutionServiceClient client = ExecutionServiceClient.getInstance(); final String userName = "genietest"; final String jobName = "sampleClientTestJob"; LOG.info("Getting jobs using specified filter criteria"); final Multimap<String, String> params = ArrayListMultimap.create(); params.put("userName", userName); params.put("status", JobStatus.FAILED.name()); params.put("limit", "3"); for (final Job ji : client.getJobs(params)) { LOG.info("Job: {id, status, finishTime} - {" + ji.getId() + ", " + ji.getStatus() + ", " + ji.getFinished() + "}"); } LOG.info("Running Hive job"); final Set<String> criteriaTags = new HashSet<>(); criteriaTags.add("adhoc"); final ClusterCriteria criteria = new ClusterCriteria(criteriaTags); final List<ClusterCriteria> clusterCriterias = new ArrayList<>(); final Set<String> commandCriteria = new HashSet<>(); clusterCriterias.add(criteria); commandCriteria.add("hive"); Job job = new Job(userName, jobName, "1.0", "-f hive.q", commandCriteria, clusterCriterias); job.setDescription("This is a test"); // Add some tags for metadata about the job. This really helps for reporting on // the jobs and categorization. final Set<String> jobTags = new HashSet<>(); jobTags.add("testgenie"); jobTags.add("sample"); job.setTags(jobTags); // send the query as an attachment final Set<FileAttachment> attachments = new HashSet<>(); final FileAttachment attachment = new FileAttachment(); attachment.setName("hive.q"); attachment.setData("select count(*) from counters where dateint=20120430 and hour=10;".getBytes("UTF-8")); attachments.add(attachment); job.setAttachments(attachments); job = client.submitJob(job); final String jobID = job.getId(); final String outputURI = job.getOutputURI(); LOG.info("Job ID: " + jobID); LOG.info("Output URL: " + outputURI); LOG.info("Getting jobInfo by jobID"); job = client.getJob(jobID); LOG.info(job.toString()); LOG.info("Waiting for job to finish"); final int blockTimeout = 600000; final int pollTime = 5000; job = client.waitForCompletion(jobID, blockTimeout, pollTime); LOG.info("Job status: " + job.getStatus()); LOG.info("Killing jobs using jobID"); final Job killedJob = client.killJob(jobID); LOG.info("Job status: " + killedJob.getStatus()); LOG.info("Done"); }
From source file:com.netflix.genie.client.sample.PigConfigServiceSampleClient.java
/** * Main for running client code./* w w w . ja va 2 s. c om*/ */ public static void main(String[] args) throws Exception { // Initialize Eureka, if it is being used // System.out.println("Initializing Eureka"); // PigConfigServiceClient.initEureka("test"); System.out.println("Initializing list of Genie servers"); ConfigurationManager.getConfigInstance().setProperty("genieClient.ribbon.listOfServers", "localhost:7001"); System.out.println("Initializing PigConfigServiceClient"); PigConfigServiceClient client = PigConfigServiceClient.getInstance(); String userName = "genietest"; String name = "MY_TEST_PIG_CONFIG"; System.out.println("Creating new pig config"); PigConfigElement pigConfigElement = new PigConfigElement(); pigConfigElement.setUser(userName); pigConfigElement.setName(name); pigConfigElement.setType(Types.Configuration.TEST.name()); pigConfigElement.setStatus(Types.ConfigStatus.INACTIVE.name()); pigConfigElement.setS3PigProperties("s3://BUCKET/PATH/TO/PIG.PROPERTIES"); pigConfigElement = client.createPigConfig(pigConfigElement); String id = pigConfigElement.getId(); System.out.println("Pig config created with id: " + id); System.out.println("Getting pigConfigs using specified filter criteria"); Multimap<String, String> params = ArrayListMultimap.create(); params.put("name", name); params.put("limit", "3"); PigConfigElement[] responses = client.getPigConfigs(params); for (PigConfigElement hce : responses) { System.out.println("Pig Configs: {id, status, updateTime} - {" + hce.getId() + ", " + hce.getStatus() + ", " + hce.getUpdateTime() + "}"); } System.out.println("Getting pig config by id"); pigConfigElement = client.getPigConfig(id); System.out.println("Pig config status: " + pigConfigElement.getStatus()); System.out.println("Updating existing pig config"); pigConfigElement.setStatus(Types.ConfigStatus.DEPRECATED.name()); pigConfigElement = client.updatePigConfig(id, pigConfigElement); System.out.println("Updated status: " + pigConfigElement.getStatus() + " at time: " + pigConfigElement.getUpdateTime()); System.out.println("Deleting pig config using id"); pigConfigElement = client.deletePigConfig(id); System.out.println("Deleted pig config with id: " + pigConfigElement.getId()); System.out.println("Done"); }
From source file:com.netflix.genie.client.sample.HiveConfigServiceSampleClient.java
/** * Main for running client code./*from www . j ava 2s .c om*/ */ public static void main(String[] args) throws Exception { // Initialize Eureka, if it is being used // System.out.println("Initializing Eureka"); // HiveConfigServiceClient.initEureka("test"); System.out.println("Initializing list of Genie servers"); ConfigurationManager.getConfigInstance().setProperty("genieClient.ribbon.listOfServers", "localhost:7001"); System.out.println("Initializing HiveConfigServiceClient"); HiveConfigServiceClient client = HiveConfigServiceClient.getInstance(); String userName = "genietest"; String name = "MY_TEST_HIVE_CONFIG"; System.out.println("Creating new hive config"); HiveConfigElement hiveConfigElement = new HiveConfigElement(); hiveConfigElement.setUser(userName); hiveConfigElement.setName(name); hiveConfigElement.setType(Types.Configuration.TEST.name()); hiveConfigElement.setStatus(Types.ConfigStatus.INACTIVE.name()); hiveConfigElement.setS3HiveSiteXml("s3://BUCKET/PATH/TO/HIVE-SITE.XML"); hiveConfigElement = client.createHiveConfig(hiveConfigElement); String id = hiveConfigElement.getId(); System.out.println("Hive config created with id: " + id); System.out.println("Getting hiveConfigs using specified filter criteria"); Multimap<String, String> params = ArrayListMultimap.create(); params.put("name", name); params.put("limit", "3"); HiveConfigElement[] responses = client.getHiveConfigs(params); for (HiveConfigElement hce : responses) { System.out.println("Hive Configs: {id, status, updateTime} - {" + hce.getId() + ", " + hce.getStatus() + ", " + hce.getUpdateTime() + "}"); } System.out.println("Getting hive config by id"); hiveConfigElement = client.getHiveConfig(id); System.out.println("Hive config status: " + hiveConfigElement.getStatus()); System.out.println("Updating existing hive config"); hiveConfigElement.setStatus(Types.ConfigStatus.DEPRECATED.name()); hiveConfigElement = client.updateHiveConfig(id, hiveConfigElement); System.out.println("Updated status: " + hiveConfigElement.getStatus() + " at time: " + hiveConfigElement.getUpdateTime()); System.out.println("Deleting hive config using id"); hiveConfigElement = client.deleteHiveConfig(id); System.out.println("Deleted hive config with id: " + hiveConfigElement.getId()); System.out.println("Done"); }
From source file:com.netflix.genie.client.sample.ApplicationServiceSampleClient.java
/** * Main for running client code.//ww w .java 2 s . c o m * * @param args program arguments * @throws Exception On issue. */ public static void main(final String[] args) throws Exception { // //Initialize Eureka, if it is being used // LOG.info("Initializing Eureka"); // ApplicationServiceClient.initEureka("test"); LOG.info("Initializing list of Genie servers"); ConfigurationManager.getConfigInstance().setProperty("genie2Client.ribbon.listOfServers", "localhost:7001"); LOG.info("Initializing ApplicationServiceClient"); final ApplicationServiceClient appClient = ApplicationServiceClient.getInstance(); LOG.info("Creating new application config"); final Application app1 = appClient.createApplication(getSampleApplication(null)); LOG.info("Application configuration created with id: " + app1.getId()); LOG.info(app1.toString()); LOG.info("Getting Applications using specified filter criteria name = " + APP_NAME); final Multimap<String, String> params = ArrayListMultimap.create(); params.put("name", APP_NAME); final List<Application> appResponses = appClient.getApplications(params); if (appResponses.isEmpty()) { LOG.info("No applications found for specified criteria."); } else { LOG.info("Applications found:"); for (final Application appResponse : appResponses) { LOG.info(appResponse.toString()); } } LOG.info("Getting application config by id"); final Application app2 = appClient.getApplication(app1.getId()); LOG.info(app2.toString()); LOG.info("Updating existing application config"); app2.setStatus(ApplicationStatus.INACTIVE); final Application app3 = appClient.updateApplication(app1.getId(), app2); LOG.info(app3.toString()); LOG.info("Configurations for application with id " + app1.getId()); final Set<String> configs = appClient.getConfigsForApplication(app1.getId()); for (final String config : configs) { LOG.info("Config = " + config); } LOG.info("Adding configurations to application with id " + app1.getId()); final Set<String> newConfigs = new HashSet<>(); newConfigs.add("someNewConfigFile"); newConfigs.add("someOtherNewConfigFile"); final Set<String> configs2 = appClient.addConfigsToApplication(app1.getId(), newConfigs); for (final String config : configs2) { LOG.info("Config = " + config); } LOG.info("Updating set of configuration files associated with id " + app1.getId()); //This should remove the original config leaving only the two in this set final Set<String> configs3 = appClient.updateConfigsForApplication(app1.getId(), newConfigs); for (final String config : configs3) { LOG.info("Config = " + config); } LOG.info("Deleting all the configuration files from the application with id " + app1.getId()); //This should remove the original config leaving only the two in this set final Set<String> configs4 = appClient.removeAllConfigsForApplication(app1.getId()); for (final String config : configs4) { //Shouldn't print anything LOG.info("Config = " + config); } /**************** Begin tests for tag Api's *********************/ LOG.info("Get tags for application with id " + app1.getId()); final Set<String> tags = app1.getTags(); for (final String tag : tags) { LOG.info("Tag = " + tag); } LOG.info("Adding tags to application with id " + app1.getId()); final Set<String> newTags = new HashSet<>(); newTags.add("tag1"); newTags.add("tag2"); final Set<String> tags2 = appClient.addTagsToApplication(app1.getId(), newTags); for (final String tag : tags2) { LOG.info("Tag = " + tag); } LOG.info("Updating set of tags associated with id " + app1.getId()); //This should remove the original config leaving only the two in this set final Set<String> tags3 = appClient.updateTagsForApplication(app1.getId(), newTags); for (final String tag : tags3) { LOG.info("Tag = " + tag); } LOG.info("Deleting one tag from the application with id " + app1.getId()); //This should remove the "tag3" from the tags final Set<String> tags5 = appClient.removeTagForApplication(app1.getId(), "tag1"); for (final String tag : tags5) { //Shouldn't print anything LOG.info("Tag = " + tag); } LOG.info("Deleting all the tags from the application with id " + app1.getId()); //This should remove the original config leaving only the two in this set final Set<String> tags4 = appClient.removeAllConfigsForApplication(app1.getId()); for (final String tag : tags4) { //Shouldn't print anything LOG.info("Config = " + tag); } /********************** End tests for tag Api's **********************/ LOG.info("Jars for application with id " + app1.getId()); final Set<String> jars = appClient.getJarsForApplication(app1.getId()); for (final String jar : jars) { LOG.info("jar = " + jar); } LOG.info("Adding jars to application with id " + app1.getId()); final Set<String> newJars = new HashSet<>(); newJars.add("someNewJarFile.jar"); newJars.add("someOtherNewJarFile.jar"); final Set<String> jars2 = appClient.addJarsToApplication(app1.getId(), newJars); for (final String jar : jars2) { LOG.info("jar = " + jar); } LOG.info("Updating set of jars associated with id " + app1.getId()); //This should remove the original jar leaving only the two in this set final Set<String> jars3 = appClient.updateJarsForApplication(app1.getId(), newJars); for (final String jar : jars3) { LOG.info("jar = " + jar); } LOG.info("Deleting all the jars from the application with id " + app1.getId()); //This should remove the original jar leaving only the two in this set final Set<String> jars4 = appClient.removeAllJarsForApplication(app1.getId()); for (final String jar : jars4) { //Shouldn't print anything LOG.info("jar = " + jar); } LOG.info("Getting the commands associated with id " + app1.getId()); final Set<Command> commands = appClient.getCommandsForApplication(app1.getId()); for (final Command command : commands) { LOG.info("Command: " + command.toString()); } LOG.info("Deleting application using id"); final Application app4 = appClient.deleteApplication(app1.getId()); LOG.info("Deleted application with id: " + app4.getId()); LOG.info(app4.toString()); LOG.info("Done"); }
From source file:com.cloudbees.api.Main.java
public static void main(String[] args) throws Exception { File beesCredentialsFile = new File(System.getProperty("user.home"), ".bees/bees.config"); Preconditions.checkArgument(beesCredentialsFile.exists(), "File %s not found", beesCredentialsFile); Properties beesCredentials = new Properties(); beesCredentials.load(new FileInputStream(beesCredentialsFile)); String apiUrl = "https://api.cloudbees.com/api"; String apiKey = beesCredentials.getProperty("bees.api.key"); String secret = beesCredentials.getProperty("bees.api.secret"); BeesClient client = new BeesClient(apiUrl, apiKey, secret, "xml", "1.0"); client.setVerbose(false);/*from ww w .j a va 2 s . co m*/ URL databasesUrl = Thread.currentThread().getContextClassLoader().getResource("databases.txt"); Preconditions.checkNotNull(databasesUrl, "File 'databases.txt' NOT found in the classpath"); Collection<String> databaseNames; try { databaseNames = Sets.newTreeSet(Resources.readLines(databasesUrl, Charsets.ISO_8859_1)); } catch (Exception e) { throw Throwables.propagate(e); } databaseNames = Collections2.transform(databaseNames, new Function<String, String>() { @Nullable @Override public String apply(@Nullable String input) { // {host_db_create,<<"tco_q5rm">>,<<"TCO_q5rm">>, if (input == null) return null; if (input.startsWith("#")) return null; if (input.indexOf('"') == -1) { logger.warn("Skip invalid line {}", input); return null; } input = input.substring(input.indexOf('"') + 1); if (input.indexOf('"') == -1) { logger.warn("Skip invalid line {}", input); return null; } return input.substring(0, input.indexOf('"')); } }); databaseNames = Collections2.filter(databaseNames, new Predicate<String>() { @Override public boolean apply(@Nullable String s) { return !Strings.isNullOrEmpty(s); } }); Multimap<String, String> databasesByAccount = ArrayListMultimap.create(); Class.forName("com.mysql.jdbc.Driver"); for (String databaseName : databaseNames) { try { DatabaseInfo databaseInfo = client.databaseInfo(databaseName, true); databasesByAccount.put(databaseInfo.getOwner(), databaseInfo.getName()); logger.debug("Evaluate " + databaseInfo.getName()); if (true == false) { // Hibernate logger.info("Hibernate {}", databaseName); Map<String, String> params = new HashMap<String, String>(); params.put("database_id", databaseName); String url = client.getRequestURL("database.hibernate", params); String response = client.executeRequest(url); DatabaseInfoResponse apiResponse = (DatabaseInfoResponse) client.readResponse(response); logger.info("DB {} status: {}", apiResponse.getDatabaseInfo().getName(), apiResponse.getDatabaseInfo().getStatus()); } if (true == false) { // Hibernate logger.info("Activate {}", databaseName); Map<String, String> params = new HashMap<String, String>(); params.put("database_id", databaseName); String url = client.getRequestURL("database.activate", params); String response = client.executeRequest(url); DatabaseInfoResponse apiResponse = (DatabaseInfoResponse) client.readResponse(response); logger.info("DB {} status: {}", apiResponse.getDatabaseInfo().getName(), apiResponse.getDatabaseInfo().getStatus()); } String dbUrl = "jdbc:mysql://" + databaseInfo.getMaster() + "/" + databaseInfo.getName(); logger.info("Connect to {} user={}", dbUrl, databaseInfo.getUsername()); Connection cnn = DriverManager.getConnection(dbUrl, databaseInfo.getUsername(), databaseInfo.getPassword()); cnn.setAutoCommit(false); cnn.close(); } catch (Exception e) { logger.warn("Exception for {}", databaseName, e); } } System.out.println("OWNERS"); for (String account : databasesByAccount.keySet()) { System.out.println(account + ": " + Joiner.on(", ").join(databasesByAccount.get(account))); } }
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 av a 2 s. c o m*/ 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:com.netflix.genie.client.sample.ClusterConfigServiceSampleClient.java
/** * Main for running client code./* w w w. ja va2 s . c o m*/ */ public static void main(String[] args) throws Exception { // Initialize Eureka, if it is being used // System.out.println("Initializing Eureka"); // ClusterConfigServiceClient.initEureka("test"); System.out.println("Initializing list of Genie servers"); ConfigurationManager.getConfigInstance().setProperty("genieClient.ribbon.listOfServers", "localhost:7001"); System.out.println("Initializing ClusterConfigServiceClient"); ClusterConfigServiceClient client = ClusterConfigServiceClient.getInstance(); String userName = "genietest"; String name = "MY_TEST_CLUSTER_CONFIG"; System.out.println("Creating new hive config for cluster"); HiveConfigElement hiveConfigElement = new HiveConfigElement(); hiveConfigElement.setUser(userName); String hiveConfigName = "MY_TEST_HIVE_CONFIG"; hiveConfigElement.setName(hiveConfigName); hiveConfigElement.setType(Types.Configuration.TEST.name()); hiveConfigElement.setStatus(Types.ConfigStatus.INACTIVE.name()); hiveConfigElement.setS3HiveSiteXml("s3://BUCKET/PATH/TO/HIVE-SITE.XML"); HiveConfigServiceClient hiveConfigClient = HiveConfigServiceClient.getInstance(); hiveConfigElement = hiveConfigClient.createHiveConfig(hiveConfigElement); String hiveConfigId = hiveConfigElement.getId(); System.out.println("Hive config created with id: " + hiveConfigId); System.out.println("Creating new cluster config"); ClusterConfigElement clusterConfigElement = new ClusterConfigElement(); clusterConfigElement.setUser(userName); clusterConfigElement.setName(name); clusterConfigElement.setTest(Boolean.TRUE); clusterConfigElement.setAdHoc(Boolean.FALSE); clusterConfigElement.setStatus(Types.ClusterStatus.OUT_OF_SERVICE.name()); clusterConfigElement.setS3MapredSiteXml("s3://PATH/TO/MAPRED-SITE.XML"); clusterConfigElement.setS3HdfsSiteXml("s3://PATH/TO/HDFS-SITE.XML"); clusterConfigElement.setS3CoreSiteXml("s3://PATH/TO/CORE-SITE.XML"); clusterConfigElement.setTestHiveConfigId(hiveConfigId); clusterConfigElement = client.createClusterConfig(clusterConfigElement); String id = clusterConfigElement.getId(); System.out.println("Cluster config created with id: " + id); System.out.println("Getting clusterConfigs using specified filter criteria"); Multimap<String, String> params = ArrayListMultimap.create(); params.put("name", name); params.put("adHoc", "false"); params.put("test", "true"); params.put("limit", "3"); ClusterConfigElement[] responses = client.getClusterConfigs(params); for (ClusterConfigElement hce : responses) { System.out.println("Cluster Configs: {id, status, updateTime} - {" + hce.getId() + ", " + hce.getStatus() + ", " + hce.getUpdateTime() + "}"); } System.out.println("Getting cluster config by id"); clusterConfigElement = client.getClusterConfig(id); System.out.println("Cluster config status: " + clusterConfigElement.getStatus()); System.out.println("Updating existing cluster config"); clusterConfigElement.setStatus(Types.ClusterStatus.TERMINATED.name()); clusterConfigElement = client.updateClusterConfig(id, clusterConfigElement); System.out.println("Updated status: " + clusterConfigElement.getStatus() + " at time: " + clusterConfigElement.getUpdateTime()); System.out.println("Deleting cluster config using id"); clusterConfigElement = client.deleteClusterConfig(id); System.out.println("Deleted cluster config with id: " + clusterConfigElement.getId()); System.out.println("Deleting hive config using id"); hiveConfigElement = hiveConfigClient.deleteHiveConfig(hiveConfigId); System.out.println("Deleted hive config with id: " + hiveConfigElement.getId()); System.out.println("Done"); }