Index: hcatalog/src/test/e2e/templeton/tests/jobstatus.conf =================================================================== --- hcatalog/src/test/e2e/templeton/tests/jobstatus.conf (revision 1535888) +++ hcatalog/src/test/e2e/templeton/tests/jobstatus.conf (working copy) @@ -152,6 +152,37 @@ 'json_path' => {'$[-1:]' => 'job_.*'}, 'status_code' => 200, }, + { + # GET jobs?user.name=UNAME_OTHER&fields=*, get all the details of the oldest 2 jobs whose + # start time is 201*, ie. year starts with 201* + 'num' => 9, + 'depends_on' => 'JOBS_1,JOBS_2,JOBS_3', + 'method' => 'GET', + 'url' => ':TEMPLETON_URL:/templeton/v1/jobs?user.name=:UNAME_OTHER:&fields=*&numrecords=2&starttime=201', + 'format_header' => 'Content-Type: application/json', + 'json_path' => {'$[-1:].id' => 'job_.*', + '$[-1:].detail.status.jobId' => 'job_.*', + '$[-1:].detail.status.runState' => '\\d+', + '$[-1:].detail.status.jobId' => 'job_.*', + '$[-1:].detail.status.jobComplete' => 'true', + '$[-1:].detail.profile.user' => ':UNAME_OTHER:', + '$[-1:].detail.profile.jobFile' => '^.+$', + '$[-1:].detail.profile.url' => '^.+$', + '$[-1:].detail.profile.queueName' => '^.+$', + '$[-1:].detail.profile.jobName' => 'loadstore\.pig', + '$[-1:].detail.profile.jobID.id' => '\\d+', + '$[-1:].detail.profile.jobID.jtIdentifier' => '\\d+', + '$[-1:].detail.profile.jobId' => 'job_.*', + '$[-1:].detail.id' => 'job_.*', + '$[-1:].detail.parentId' => 'job_.*', + '$[-1:].detail.percentComplete' => '100%', + '$[-1:].detail.exitValue' => '0', + '$[-1:].detail.user' => ':UNAME_OTHER:', + '$[-1:].detail.callback' => '^.+$', + '$[-1:].detail.completed' => 'done', + }, + 'status_code' => 200, + }, ] } Index: hcatalog/webhcat/svr/src/main/java/org/apache/hive/hcatalog/templeton/Server.java =================================================================== --- hcatalog/webhcat/svr/src/main/java/org/apache/hive/hcatalog/templeton/Server.java (revision 1535888) +++ hcatalog/webhcat/svr/src/main/java/org/apache/hive/hcatalog/templeton/Server.java (working copy) @@ -856,14 +856,42 @@ /** * Return all the known job ids for this user. + * Example usages: + * 1. curl -s 'http://localhost:50111/templeton/v1/jobs?user.name=hsubramaniyan' + * Return all the jobids submitted by hsubramaniyan + * 2. curl -s 'http://localhost:50111/templeton/v1/jobs?user.name=hsubramaniyan&showall=true' + * Return all the jobids that are visible to hsubramaniyan + * 3. curl -s + * 'http://localhost:50111/templeton/v1/jobs?user.name=hsubramaniyan&starttime=20131209' + * Return all the jobids submitted by hsubramaniyan after 2013/12/09 + * 4. curl -s + * 'http://localhost:50111/templeton/v1/jobs?user.name=hsubramaniyan&starttime=20131209&numrecords=5' + * Return the first 5(atmost) jobids submitted by hsubramaniyan after 2013/12/09 + * 5. curl -s 'http://localhost:50111/templeton/v1/jobs?user.name=hsubramaniyan&numrecords=5' + * Return the first 5(atmost) jobids submitted by hsubramaniyan sorted by the + * timestamp of job submission. + * @param fields : If fields set to "*", the request will return full details of the job. + * If fields is missing, will only return the job ID. Currently the value can only + * be "*", other values are not allowed and will throw exception. + * @param showall : If showall is set to "true", the request will return all jobs the user + * has permission to view, not only the jobs belonging to the user. + * @param starttime : If starttime is not null, the records whose jobid is lexicographically + * greater than the job_ are only returned. For example, if starttime = + * 20131209, the jobs whose jobid > job_20131209 are returned. + * @param numrecords : If the number of records are set, the top #numrecords records will be + * returned after sorting the job ids based on starttime. If starttime is set to null, + * the top #numrecords will be returned after lexicographically sorting the jobid list. + * @return list of job items based on the filter conditions specified by the user. */ @GET @Path("jobs") @Produces({MediaType.APPLICATION_JSON}) public List showJobList(@QueryParam("fields") String fields, - @QueryParam("showall") boolean showall) + @QueryParam("showall") boolean showall, + @QueryParam("starttime") String starttime, + @QueryParam("numrecords") String numrecords) throws NotAuthorizedException, BadParam, IOException, InterruptedException { - + verifyUser(); boolean showDetails = false; @@ -877,7 +905,42 @@ ListDelegator ld = new ListDelegator(appConf); List list = ld.run(getDoAsUser(), showall); List detailList = new ArrayList(); + int currRecord = 0; + int numRecords; + + // Parse numrecords to an integer + try { + if (numrecords != null) { + numRecords = Integer.parseInt(numrecords); + if (numRecords <= 0) { + throw new BadParam("numrecords should be a integer > 0"); + } + } + else { + numRecords = -1; + } + } + catch(Exception e) { + throw new BadParam("Invalid numrecords format: numrecords should be a valid integer > 0"); + } + + // Sort the list by starttime to get the top numRecords + Collections.sort(list); + for (String job : list) { + // If numRecords = -1, fetch all records since the starttime. + // Hence skip all the below checks when numRecords = -1. + // If currRecord >= numRecords, we have fetched the top #numRecords + // for the given starttime + if (numRecords != -1 && currRecord >= numRecords) { + break; + } + // If the current record needs to be returned based on the filter conditions specified by the user, + // increment the counter + else if (numRecords != -1 && ((starttime != null && job.compareTo("job_"+starttime) > 0) || + starttime == null)){ + currRecord++; + } JobItemBean jobItem = new JobItemBean(); jobItem.id = job; if (showDetails) {