You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by db...@apache.org on 2016/05/31 19:44:31 UTC
[15/21] ambari git commit: AMBARI-16963: JDBC implementation of hive
view. (dipayanb)
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMParserFactory.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMParserFactory.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMParserFactory.java
new file mode 100644
index 0000000..4b28e64
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMParserFactory.java
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.rm;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.utils.ambari.AmbariApi;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RMParserFactory {
+ protected final static Logger LOG =
+ LoggerFactory.getLogger(RMParserFactory.class);
+
+ private final ViewContext context;
+ private final AmbariApi ambariApi;
+
+ public RMParserFactory(ViewContext context) {
+ this.context = context;
+ this.ambariApi = new AmbariApi(context);
+ }
+
+ public RMParser getRMParser() {
+ String rmUrl = getRMUrl();
+
+ RMRequestsDelegate delegate = new RMRequestsDelegateImpl(context, rmUrl);
+ return new RMParser(delegate);
+ }
+
+ public String getRMUrl() {
+ return ambariApi.getServices().getRMUrl();
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMRequestsDelegate.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMRequestsDelegate.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMRequestsDelegate.java
new file mode 100644
index 0000000..5c059c0
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMRequestsDelegate.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.rm;
+
+import org.json.simple.JSONObject;
+
+public interface RMRequestsDelegate {
+ String dagProgressUrl(String appId, String dagIdx);
+
+ String verticesProgressUrl(String appId, String dagIdx, String vertices);
+
+ JSONObject dagProgress(String appId, String dagIdx);
+
+ JSONObject verticesProgress(String appId, String dagIdx, String commaSeparatedVertices);
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMRequestsDelegateImpl.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMRequestsDelegateImpl.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMRequestsDelegateImpl.java
new file mode 100644
index 0000000..6d2d22e
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/rm/RMRequestsDelegateImpl.java
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.rm;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.utils.ServiceFormattedException;
+import org.apache.commons.io.IOUtils;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+
+public class RMRequestsDelegateImpl implements RMRequestsDelegate {
+ protected final static Logger LOG =
+ LoggerFactory.getLogger(RMRequestsDelegateImpl.class);
+ public static final String EMPTY_ENTITIES_JSON = "{ \"entities\" : [ ] }";
+
+ private ViewContext context;
+ private String rmUrl;
+
+ public RMRequestsDelegateImpl(ViewContext context, String rmUrl) {
+ this.context = context;
+ this.rmUrl = rmUrl;
+ }
+
+ @Override
+ public String dagProgressUrl(String appId, String dagIdx) {
+ return rmUrl + String.format("/proxy/%s/ws/v1/tez/dagProgress?dagID=%s", appId, dagIdx);
+ }
+
+ @Override
+ public String verticesProgressUrl(String appId, String dagIdx, String vertices) {
+ return rmUrl + String.format("/proxy/%s/ws/v1/tez/vertexProgresses?dagID=%s&vertexID=%s", appId, dagIdx, vertices);
+ }
+
+ @Override
+ public JSONObject dagProgress(String appId, String dagIdx) {
+ String url = dagProgressUrl(appId, dagIdx);
+ String response;
+ try {
+ InputStream responseInputStream = context.getURLStreamProvider().readFrom(url, "GET",
+ (String)null, new HashMap<String, String>());
+ response = IOUtils.toString(responseInputStream);
+ } catch (IOException e) {
+ throw new ServiceFormattedException(
+ String.format("R010 DAG %s in app %s not found or ResourceManager is unreachable", dagIdx, appId));
+ }
+ return (JSONObject) JSONValue.parse(response);
+ }
+
+ @Override
+ public JSONObject verticesProgress(String appId, String dagIdx, String commaSeparatedVertices) {
+ String url = verticesProgressUrl(appId, dagIdx, commaSeparatedVertices);
+ String response;
+ try {
+ InputStream responseInputStream = context.getURLStreamProvider().readFrom(url, "GET",
+ (String)null, new HashMap<String, String>());
+ response = IOUtils.toString(responseInputStream);
+ } catch (IOException e) {
+ throw new ServiceFormattedException(
+ String.format("R020 DAG %s in app %s not found or ResourceManager is unreachable", dagIdx, appId));
+ }
+ return (JSONObject) JSONValue.parse(response);
+ }
+
+ protected String readFromWithDefault(String url, String defaultResponse) {
+ String response;
+ try {
+ InputStream responseInputStream = context.getURLStreamProvider().readFrom(url, "GET",
+ (String)null, new HashMap<String, String>());
+ response = IOUtils.toString(responseInputStream);
+ } catch (IOException e) {
+ LOG.error("Error while reading from RM", e);
+ response = defaultResponse;
+ }
+ return response;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/IJobControllerFactory.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/IJobControllerFactory.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/IJobControllerFactory.java
new file mode 100644
index 0000000..1e1345e
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/IJobControllerFactory.java
@@ -0,0 +1,23 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.viewJobs;
+
+public interface IJobControllerFactory {
+ JobController createControllerForJob(Job job);
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/Job.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/Job.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/Job.java
new file mode 100644
index 0000000..2caba5e
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/Job.java
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.viewJobs;
+
+
+import org.apache.ambari.view.hive2.persistence.utils.Indexed;
+import org.apache.ambari.view.hive2.persistence.utils.PersonalResource;
+
+import java.io.Serializable;
+
+/**
+ * Interface for ExecuteJob bean to create Proxy for it
+ */
+public interface Job extends Serializable,Indexed,PersonalResource {
+ public static final String JOB_STATE_UNKNOWN = "UNKNOWN";
+ public static final String JOB_STATE_INITIALIZED = "INITIALIZED";
+ public static final String JOB_STATE_RUNNING = "RUNNING";
+ public static final String JOB_STATE_FINISHED = "SUCCEEDED";
+ public static final String JOB_STATE_CANCELED = "CANCELED";
+ public static final String JOB_STATE_CLOSED = "CLOSED";
+ public static final String JOB_STATE_ERROR = "ERROR";
+ public static final String JOB_STATE_PENDING = "PENDING";
+
+ String getId();
+
+ void setId(String id);
+
+ String getOwner();
+
+ void setOwner(String owner);
+
+ String getTitle();
+
+ void setTitle(String title);
+
+ String getQueryFile();
+
+ void setQueryFile(String queryFile);
+
+ Long getDateSubmitted();
+
+ void setDateSubmitted(Long dateSubmitted);
+
+ Long getDuration();
+
+ void setDuration(Long duration);
+
+ String getStatus();
+
+ void setStatus(String status);
+
+ String getForcedContent();
+
+ void setForcedContent(String forcedContent);
+
+ String getQueryId();
+
+ void setQueryId(String queryId);
+
+ String getStatusDir();
+
+ void setStatusDir(String statusDir);
+
+ String getDataBase();
+
+ void setDataBase(String dataBase);
+
+ String getLogFile();
+
+ void setLogFile(String logFile);
+
+ String getConfFile();
+
+ void setConfFile(String confFile);
+
+ String getApplicationId();
+
+ void setApplicationId(String applicationId);
+
+ String getDagName();
+
+ void setDagName(String dagName);
+
+ String getDagId();
+
+ void setDagId(String dagId);
+
+ String getSessionTag();
+
+ void setSessionTag(String sessionTag);
+
+ String getSqlState();
+
+ void setSqlState(String sqlState);
+
+ String getStatusMessage();
+
+ void setStatusMessage(String message);
+
+ String getReferrer();
+
+ void setReferrer(String referrer);
+
+ String getGlobalSettings();
+
+ void setGlobalSettings(String globalSettings);
+
+ String getGuid();
+
+ void setGuid(String guid);
+
+ String getErrorFile();
+
+ void setErrorFile(String errorFile);
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobController.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobController.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobController.java
new file mode 100644
index 0000000..8cc120b
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobController.java
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.viewJobs;
+
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+
+public interface JobController {
+
+ //OperationHandleController.OperationStatus getStatus() throws ItemNotFound, HiveClientException, NoOperationStatusSetException;
+
+ //TODO: New implementation
+
+ void submit() throws Throwable;
+
+ void cancel() throws ItemNotFound;
+
+ Job getJob();
+
+ /**
+ * Use carefully. Returns unproxied bean object
+ * @return unproxied bean object
+ */
+ Job getJobPOJO();
+
+ //Cursor getResults() throws ItemNotFound;
+
+ //TODO: New implementation
+
+ boolean hasResults() throws ItemNotFound;
+
+ void afterCreation();
+
+ void update();
+
+ boolean isModified();
+
+ void clearModified();
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobControllerFactory.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobControllerFactory.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobControllerFactory.java
new file mode 100644
index 0000000..45c7379
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobControllerFactory.java
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.viewJobs;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.utils.SharedObjectsFactory;
+
+public class JobControllerFactory implements IJobControllerFactory {
+ private SharedObjectsFactory sharedObjectsFactory;
+ private ViewContext context;
+
+ public JobControllerFactory(ViewContext context, SharedObjectsFactory sharedObjectsFactory) {
+ this.sharedObjectsFactory = sharedObjectsFactory;
+ this.context = context;
+ }
+
+ @Override
+ public JobController createControllerForJob(Job job) {
+ return new JobControllerImpl(context, job,
+ //sharedObjectsFactory.getOperationHandleControllerFactory(),
+ sharedObjectsFactory.getSavedQueryResourceManager(),
+ sharedObjectsFactory.getATSParser(),
+ sharedObjectsFactory.getHdfsApi());
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobControllerImpl.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobControllerImpl.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobControllerImpl.java
new file mode 100644
index 0000000..31b531b
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobControllerImpl.java
@@ -0,0 +1,397 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.viewJobs;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.client.AsyncJobRunner;
+import org.apache.ambari.view.hive2.client.AsyncJobRunnerImpl;
+import org.apache.ambari.view.hive2.client.ConnectionConfig;
+import org.apache.ambari.view.hive2.client.HiveClientRuntimeException;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.resources.jobs.ModifyNotificationDelegate;
+import org.apache.ambari.view.hive2.resources.jobs.ModifyNotificationInvocationHandler;
+import org.apache.ambari.view.hive2.resources.jobs.atsJobs.IATSParser;
+import org.apache.ambari.view.hive2.resources.savedQueries.SavedQuery;
+import org.apache.ambari.view.hive2.resources.savedQueries.SavedQueryResourceManager;
+import org.apache.ambari.view.hive2.utils.BadRequestFormattedException;
+import org.apache.ambari.view.hive2.utils.FilePaginator;
+import org.apache.ambari.view.hive2.utils.MisconfigurationFormattedException;
+import org.apache.ambari.view.hive2.utils.ServiceFormattedException;
+import org.apache.ambari.view.hive2.ConnectionFactory;
+import org.apache.ambari.view.hive2.ConnectionSystem;
+import org.apache.ambari.view.hive2.actor.message.AsyncJob;
+import org.apache.ambari.view.hive2.actor.message.job.AsyncExecutionFailed;
+import org.apache.ambari.view.hive2.internal.AsyncExecutionSuccess;
+import org.apache.ambari.view.hive2.internal.Either;
+import org.apache.ambari.view.utils.hdfs.HdfsApi;
+import org.apache.ambari.view.utils.hdfs.HdfsApiException;
+import org.apache.ambari.view.utils.hdfs.HdfsUtil;
+import org.apache.commons.lang3.ArrayUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.lang.reflect.Proxy;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+public class JobControllerImpl implements JobController, ModifyNotificationDelegate {
+ private final static Logger LOG =
+ LoggerFactory.getLogger(JobControllerImpl.class);
+
+ private ViewContext context;
+ private HdfsApi hdfsApi;
+ private Job jobUnproxied;
+ private Job job;
+ private boolean modified;
+
+ //private OperationHandleControllerFactory opHandleControllerFactory;
+ private SavedQueryResourceManager savedQueryResourceManager;
+ private IATSParser atsParser;
+
+ /**
+ * JobController constructor
+ * Warning: Create JobControllers ONLY using JobControllerFactory!
+ */
+ public JobControllerImpl(ViewContext context, Job job,
+ //OperationHandleControllerFactory opHandleControllerFactory,
+ SavedQueryResourceManager savedQueryResourceManager,
+ IATSParser atsParser,
+ HdfsApi hdfsApi) {
+ this.context = context;
+ setJobPOJO(job);
+ //this.opHandleControllerFactory = opHandleControllerFactory;
+ this.savedQueryResourceManager = savedQueryResourceManager;
+ this.atsParser = atsParser;
+ this.hdfsApi = hdfsApi;
+
+ //UserLocalConnection connectionLocal = new UserLocalConnection();
+ //this.hiveConnection = new ConnectionController(opHandleControllerFactory, connectionLocal.get(context));
+ }
+
+ public String getQueryForJob() {
+ FilePaginator paginator = new FilePaginator(job.getQueryFile(), hdfsApi);
+ String query;
+ try {
+ query = paginator.readPage(0); //warning - reading only 0 page restricts size of query to 1MB
+ } catch (IOException e) {
+ throw new ServiceFormattedException("F030 Error when reading file " + job.getQueryFile(), e);
+ } catch (InterruptedException e) {
+ throw new ServiceFormattedException("F030 Error when reading file " + job.getQueryFile(), e);
+ }
+ return query;
+ }
+
+ private static final String DEFAULT_DB = "default";
+
+ public String getJobDatabase() {
+ if (job.getDataBase() != null) {
+ return job.getDataBase();
+ } else {
+ return DEFAULT_DB;
+ }
+ }
+
+ /*@Override
+ public OperationHandleController.OperationStatus getStatus() throws ItemNotFound, HiveClientException, NoOperationStatusSetException {
+ OperationHandleController handle = opHandleControllerFactory.getHandleForJob(job);
+ return handle.getOperationStatus();
+ }*/
+
+ @Override
+ public void submit() throws Throwable {
+ String jobDatabase = getJobDatabase();
+ String query = getQueryForJob();
+ ConnectionSystem system = ConnectionSystem.getInstance();
+ AsyncJobRunner asyncJobRunner = new AsyncJobRunnerImpl(context, system.getOperationController(context), system.getActorSystem());
+ // create async Job
+ //
+ AsyncJob asyncJob = new AsyncJob(job.getId(), context.getUsername(), getStatements(jobDatabase, query), job.getLogFile(), context);
+ asyncJobRunner.submitJob(getHiveConnectionConfig(), asyncJob, job);
+
+ }
+
+ private String[] getStatements(String jobDatabase, String query) {
+ String[] queries = query.split(";");
+
+
+ String[] strings = {"use " + jobDatabase};
+ return ArrayUtils.addAll(strings, queries);
+ }
+
+
+ @Override
+ public void cancel() throws ItemNotFound {
+ //OperationHandleController handle = opHandleControllerFactory.getHandleForJob(job);
+ //handle.cancel();
+ }
+
+ @Override
+ public void update() {
+ updateOperationStatus();
+ updateOperationLogs();
+
+ updateJobDuration();
+ }
+
+ public void updateOperationStatus() {
+ try {
+
+ //OperationHandleController handle = opHandleControllerFactory.getHandleForJob(job);
+ //OperationHandleController.OperationStatus status = handle.getOperationStatus();
+ /*job.setStatus(status.status);
+ job.setStatusMessage(status.message);
+ job.setSqlState(status.sqlState);*/
+ LOG.debug("Status of job#" + job.getId() + " is " + job.getStatus());
+
+ } catch (Exception /*NoOperationStatusSetException*/ e) {
+ LOG.info("Operation state is not set for job#" + job.getId());
+
+ } /*catch (HiveErrorStatusException e) {
+ LOG.debug("Error updating status for job#" + job.getId() + ": " + e.getMessage());
+ job.setStatus(ExecuteJob.JOB_STATE_UNKNOWN);
+
+ } catch (HiveClientException e) {
+ throw new HiveClientFormattedException(e);
+
+ } catch (ItemNotFound itemNotFound) {
+ LOG.debug("No TOperationHandle for job#" + job.getId() + ", can't update status");
+ }*/
+ }
+
+ public void updateOperationLogs() {
+ try {
+ //OperationHandleController handle = opHandleControllerFactory.getHandleForJob(job);
+ //String logs = handle.getLogs();
+
+ //LogParser info = LogParser.parseLog(logs);
+ //LogParser.AppId app = info.getLastAppInList();
+ /*if (app != null) {
+ job.setApplicationId(app.getIdentifier());
+ }*/
+
+ String logFilePath = job.getLogFile();
+ //HdfsUtil.putStringToFile(hdfsApi, logFilePath, logs);
+
+ } catch (HiveClientRuntimeException ex) {
+ LOG.error("Error while fetching logs: " + ex.getMessage());
+ } /*catch (ItemNotFound itemNotFound) {
+ LOG.debug("No TOperationHandle for job#" + job.getId() + ", can't read logs");
+ } catch (HdfsApiException e) {
+ throw new ServiceFormattedException(e);
+ }*/
+ }
+
+ public boolean isJobEnded() {
+ String status = job.getStatus();
+ return status.equals(Job.JOB_STATE_FINISHED) || status.equals(Job.JOB_STATE_CANCELED) ||
+ status.equals(Job.JOB_STATE_CLOSED) || status.equals(Job.JOB_STATE_ERROR) ||
+ status.equals(Job.JOB_STATE_UNKNOWN); // Unknown is not finished, but polling makes no sense
+ }
+
+ @Override
+ public Job getJob() {
+ return job;
+ }
+
+ /**
+ * Use carefully. Returns unproxied bean object
+ * @return unproxied bean object
+ */
+ @Override
+ public Job getJobPOJO() {
+ return jobUnproxied;
+ }
+
+ public void setJobPOJO(Job jobPOJO) {
+ Job jobModifyNotificationProxy = (Job) Proxy.newProxyInstance(jobPOJO.getClass().getClassLoader(),
+ new Class[]{Job.class},
+ new ModifyNotificationInvocationHandler(jobPOJO, this));
+ this.job = jobModifyNotificationProxy;
+
+ this.jobUnproxied = jobPOJO;
+ }
+
+ /*@Override
+ public Cursor getResults() throws ItemNotFound {
+ OperationHandleController handle = opHandleControllerFactory.getHandleForJob(job);
+ return handle.getResults();
+ }*/
+
+ @Override
+ public boolean hasResults() throws ItemNotFound {
+ //OperationHandleController handle = opHandleControllerFactory.getHandleForJob(job);
+ //return handle.hasResults();
+ return false;
+ }
+
+ @Override
+ public void afterCreation() {
+ setupStatusDirIfNotPresent();
+ setupQueryFileIfNotPresent();
+ setupLogFileIfNotPresent();
+
+ setCreationDate();
+ }
+
+ public void setupLogFileIfNotPresent() {
+ if (job.getLogFile() == null || job.getLogFile().isEmpty()) {
+ setupLogFile();
+ }
+ }
+
+ public void setupQueryFileIfNotPresent() {
+ if (job.getQueryFile() == null || job.getQueryFile().isEmpty()) {
+ setupQueryFile();
+ }
+ }
+
+ public void setupStatusDirIfNotPresent() {
+ if (job.getStatusDir() == null || job.getStatusDir().isEmpty()) {
+ setupStatusDir();
+ }
+ }
+
+ private static final long MillisInSecond = 1000L;
+
+ public void updateJobDuration() {
+ job.setDuration(System.currentTimeMillis() / MillisInSecond - job.getDateSubmitted());
+ }
+
+ public void setCreationDate() {
+ job.setDateSubmitted(System.currentTimeMillis() / MillisInSecond);
+ }
+
+
+ private void setupLogFile() {
+ LOG.debug("Creating log file for job#" + job.getId());
+
+ String logFile = job.getStatusDir() + "/" + "logs";
+ try {
+ HdfsUtil.putStringToFile(hdfsApi, logFile, "");
+ } catch (HdfsApiException e) {
+ throw new ServiceFormattedException(e);
+ }
+
+ job.setLogFile(logFile);
+ LOG.debug("Log file for job#" + job.getId() + ": " + logFile);
+ }
+
+ private void setupStatusDir() {
+ String newDirPrefix = makeStatusDirectoryPrefix();
+ String newDir = null;
+ try {
+ newDir = HdfsUtil.findUnallocatedFileName(hdfsApi, newDirPrefix, "");
+ } catch (HdfsApiException e) {
+ throw new ServiceFormattedException(e);
+ }
+
+ job.setStatusDir(newDir);
+ LOG.debug("Status dir for job#" + job.getId() + ": " + newDir);
+ }
+
+ private String makeStatusDirectoryPrefix() {
+ String userScriptsPath = context.getProperties().get("jobs.dir");
+
+ if (userScriptsPath == null) { // TODO: move check to initialization code
+ String msg = "jobs.dir is not configured!";
+ LOG.error(msg);
+ throw new MisconfigurationFormattedException("jobs.dir");
+ }
+
+ String normalizedName = String.format("hive-job-%s", job.getId());
+ String timestamp = new SimpleDateFormat("yyyy-MM-dd_hh-mm").format(new Date());
+ return String.format(userScriptsPath +
+ "/%s-%s", normalizedName, timestamp);
+ }
+
+ private void setupQueryFile() {
+ String statusDir = job.getStatusDir();
+ assert statusDir != null : "setupStatusDir() should be called first";
+
+ String jobQueryFilePath = statusDir + "/" + "query.hql";
+
+ try {
+
+ if (job.getForcedContent() != null) {
+
+ HdfsUtil.putStringToFile(hdfsApi, jobQueryFilePath, job.getForcedContent());
+ job.setForcedContent(""); // prevent forcedContent to be written to DB
+
+ } else if (job.getQueryId() != null) {
+
+ String savedQueryFile = getRelatedSavedQueryFile();
+ hdfsApi.copy(savedQueryFile, jobQueryFilePath);
+ job.setQueryFile(jobQueryFilePath);
+
+ } else {
+
+ throw new BadRequestFormattedException("queryId or forcedContent should be passed!", null);
+
+ }
+
+ } catch (IOException e) {
+ throw new ServiceFormattedException("F040 Error when creating file " + jobQueryFilePath, e);
+ } catch (InterruptedException e) {
+ throw new ServiceFormattedException("F040 Error when creating file " + jobQueryFilePath, e);
+ } catch (HdfsApiException e) {
+ throw new ServiceFormattedException(e);
+ }
+ job.setQueryFile(jobQueryFilePath);
+
+ LOG.debug("Query file for job#" + job.getId() + ": " + jobQueryFilePath);
+ }
+
+
+ private ConnectionConfig getHiveConnectionConfig() {
+ return ConnectionFactory.create(context);
+ }
+
+ private String getRelatedSavedQueryFile() {
+ SavedQuery savedQuery;
+ try {
+ savedQuery = savedQueryResourceManager.read(job.getQueryId());
+ } catch (ItemNotFound itemNotFound) {
+ throw new BadRequestFormattedException("queryId not found!", itemNotFound);
+ }
+ return savedQuery.getQueryFile();
+ }
+
+ @Override
+ public boolean onModification(Object object) {
+ setModified(true);
+ return true;
+ }
+
+ @Override
+ public boolean isModified() {
+ return modified;
+ }
+
+ public void setModified(boolean modified) {
+ this.modified = modified;
+ }
+
+ @Override
+ public void clearModified() {
+ setModified(false);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobImpl.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobImpl.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobImpl.java
new file mode 100644
index 0000000..b71e2f7
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobImpl.java
@@ -0,0 +1,322 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.viewJobs;
+
+import org.apache.commons.beanutils.PropertyUtils;
+
+import java.beans.Transient;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+
+/**
+ * Bean to represent saved query
+ */
+public class JobImpl implements Job {
+ private String title = null;
+ private String queryFile = null;
+ private String statusDir = null;
+ private Long dateSubmitted = 0L;
+ private Long duration = 0L;
+ private String forcedContent = null;
+ private String dataBase = null;
+ private String queryId = null;
+
+ private String status = JOB_STATE_UNKNOWN;
+ private String statusMessage = null;
+ private String sqlState = null;
+
+ private String applicationId;
+ private String dagId;
+ private String dagName;
+
+ private String sessionTag;
+ private String referrer;
+ private String globalSettings;
+
+ private String id = null;
+ private String owner = null;
+
+ private String logFile;
+ private String confFile;
+ private String errorFile;
+
+ private String guid = null;
+
+ public JobImpl() {}
+ public JobImpl(Map<String, Object> stringObjectMap) throws InvocationTargetException, IllegalAccessException {
+ for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
+ try {
+ PropertyUtils.setProperty(this, entry.getKey(), entry.getValue());
+ } catch (NoSuchMethodException e) {
+ //do nothing, skip
+ }
+ }
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Job)) return false;
+
+ JobImpl job = (JobImpl) o;
+
+ if (id != null ? !id.equals(job.id) : job.id != null) return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return id != null ? id.hashCode() : 0;
+ }
+
+ @Override
+ public String getId() {
+ return id;
+ }
+
+ @Override
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public String getOwner() {
+ return owner;
+ }
+
+ @Override
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
+
+ @Override
+ public String getTitle() {
+ return title;
+ }
+
+ @Override
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ @Override
+ public String getQueryFile() {
+ return queryFile;
+ }
+
+ @Override
+ public void setQueryFile(String queryFile) {
+ this.queryFile = queryFile;
+ }
+
+ @Override
+ public Long getDateSubmitted() {
+ return dateSubmitted;
+ }
+
+ @Override
+ public void setDateSubmitted(Long dateSubmitted) {
+ this.dateSubmitted = dateSubmitted;
+ }
+
+ @Override
+ public Long getDuration() {
+ return duration;
+ }
+
+ @Override
+ public void setDuration(Long duration) {
+ this.duration = duration;
+ }
+
+ @Override
+ public String getStatus() {
+ return status;
+ }
+
+ @Override
+ public void setStatus(String status) {
+ this.status = status;
+ }
+
+ @Override
+ @Transient
+ public String getForcedContent() {
+ return forcedContent;
+ }
+
+ @Override
+ @Transient
+ public void setForcedContent(String forcedContent) {
+ this.forcedContent = forcedContent;
+ }
+
+ @Override
+ public String getQueryId() {
+ return queryId;
+ }
+
+ @Override
+ public void setQueryId(String queryId) {
+ this.queryId = queryId;
+ }
+
+ @Override
+ public String getStatusDir() {
+ return statusDir;
+ }
+
+ @Override
+ public void setStatusDir(String statusDir) {
+ this.statusDir = statusDir;
+ }
+
+ @Override
+ public String getDataBase() {
+ return dataBase;
+ }
+
+ @Override
+ public void setDataBase(String dataBase) {
+ this.dataBase = dataBase;
+ }
+
+ @Override
+ public String getLogFile() {
+ return logFile;
+ }
+
+ @Override
+ public void setLogFile(String logFile) {
+ this.logFile = logFile;
+ }
+
+ @Override
+ public String getConfFile() {
+ return confFile;
+ }
+
+ @Override
+ public void setConfFile(String confFile) {
+ this.confFile = confFile;
+ }
+
+ @Override
+ public String getApplicationId() {
+ return applicationId;
+ }
+
+ @Override
+ public void setApplicationId(String applicationId) {
+ this.applicationId = applicationId;
+ }
+
+ @Override
+ public String getDagName() {
+ return dagName;
+ }
+
+ @Override
+ public void setDagName(String dagName) {
+ this.dagName = dagName;
+ }
+
+ @Override
+ public String getDagId() {
+ return dagId;
+ }
+
+ @Override
+ public void setDagId(String dagId) {
+ this.dagId = dagId;
+ }
+
+ @Override
+ public String getSessionTag() {
+ return sessionTag;
+ }
+
+ @Override
+ public void setSessionTag(String sessionTag) {
+ this.sessionTag = sessionTag;
+ }
+
+ @Override
+ @Transient
+ public String getStatusMessage() {
+ return statusMessage;
+ }
+
+ @Override
+ @Transient
+ public void setStatusMessage(String statusMessage) {
+ this.statusMessage = statusMessage;
+ }
+
+ @Override
+ public String getSqlState() {
+ return sqlState;
+ }
+
+ @Override
+ public void setSqlState(String sqlState) {
+ this.sqlState = sqlState;
+ }
+
+ @Override
+ public String getReferrer() {
+ return referrer;
+ }
+
+ @Override
+ public void setReferrer(String referrer) {
+ this.referrer = referrer;
+ }
+
+ @Override
+ public String getGlobalSettings() {
+ return globalSettings;
+ }
+
+ @Override
+ public void setGlobalSettings(String globalSettings) {
+ this.globalSettings = globalSettings;
+ }
+
+ @Override
+ public String getGuid() {
+ return guid;
+ }
+
+ @Override
+ public void setGuid(String guid) {
+ this.guid = guid;
+ }
+
+ @Override
+ public String getErrorFile() {
+ return errorFile;
+ }
+
+ @Override
+ public void setErrorFile(String errorFile) {
+ this.errorFile = errorFile;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobResourceManager.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobResourceManager.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobResourceManager.java
new file mode 100644
index 0000000..f91363f
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/jobs/viewJobs/JobResourceManager.java
@@ -0,0 +1,108 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.jobs.viewJobs;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.persistence.utils.FilteringStrategy;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.resources.PersonalCRUDResourceManager;
+import org.apache.ambari.view.hive2.utils.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+
+/**
+ * Object that provides CRUD operations for job objects
+ */
+public class JobResourceManager extends PersonalCRUDResourceManager<Job> {
+ private final static Logger LOG =
+ LoggerFactory.getLogger(JobResourceManager.class);
+
+ private IJobControllerFactory jobControllerFactory;
+
+ /**
+ * Constructor
+ * @param context View Context instance
+ */
+ public JobResourceManager(SharedObjectsFactory sharedObjectsFactory, ViewContext context) {
+ super(JobImpl.class, sharedObjectsFactory, context);
+ jobControllerFactory = sharedObjectsFactory.getJobControllerFactory();
+ }
+
+ @Override
+ public Job create(Job object) {
+ super.create(object);
+ JobController jobController = jobControllerFactory.createControllerForJob(object);
+
+ try {
+
+ jobController.afterCreation();
+ saveIfModified(jobController);
+
+ } catch (ServiceFormattedException e) {
+ cleanupAfterErrorAndThrowAgain(object, e);
+ }
+
+ return object;
+ }
+
+ public void saveIfModified(JobController jobController) {
+ if (jobController.isModified()) {
+ save(jobController.getJobPOJO());
+ jobController.clearModified();
+ }
+ }
+
+
+ @Override
+ public Job read(Object id) throws ItemNotFound {
+ Job job = super.read(id);
+ JobController jobController = jobControllerFactory.createControllerForJob(job);
+ jobController.update();
+ saveIfModified(jobController);
+ return job;
+ }
+
+ @Override
+ public List<Job> readAll(FilteringStrategy filteringStrategy) {
+ return super.readAll(filteringStrategy);
+ }
+
+ @Override
+ public void delete(Object resourceId) throws ItemNotFound {
+ super.delete(resourceId);
+ }
+
+ public JobController readController(Object id) throws ItemNotFound {
+ Job job = read(id);
+ return jobControllerFactory.createControllerForJob(job);
+ }
+
+ /*public Cursor getJobResultsCursor(ExecuteJob job) {
+ try {
+ JobController jobController = jobControllerFactory.createControllerForJob(job);
+ return jobController.getResults();
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException("ExecuteJob results are expired", null);
+ }
+ }*/
+
+ //TODO: New implementation
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceItem.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceItem.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceItem.java
new file mode 100644
index 0000000..ed984e4
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceItem.java
@@ -0,0 +1,78 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.resources;
+
+import org.apache.ambari.view.hive2.persistence.utils.PersonalResource;
+import org.apache.commons.beanutils.BeanUtils;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+
+/**
+ * Bean to represent file resource
+ */
+public class FileResourceItem implements Serializable, PersonalResource {
+ private String name;
+ private String path;
+
+ private String id;
+ private String owner;
+
+ public FileResourceItem() {}
+ public FileResourceItem(Map<String, Object> stringObjectMap) throws InvocationTargetException, IllegalAccessException {
+ BeanUtils.populate(this, stringObjectMap);
+ }
+
+ @Override
+ public String getId() {
+ return id;
+ }
+
+ @Override
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public String getOwner() {
+ return owner;
+ }
+
+ @Override
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public void setPath(String path) {
+ this.path = path;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceResourceManager.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceResourceManager.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceResourceManager.java
new file mode 100644
index 0000000..a4ea1f6
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceResourceManager.java
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.resources;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.persistence.IStorageFactory;
+import org.apache.ambari.view.hive2.persistence.utils.FilteringStrategy;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.resources.PersonalCRUDResourceManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * Object that provides CRUD operations for resource objects
+ */
+public class FileResourceResourceManager extends PersonalCRUDResourceManager<FileResourceItem> {
+ private final static Logger LOG =
+ LoggerFactory.getLogger(FileResourceResourceManager.class);
+
+ /**
+ * Constructor
+ * @param context View Context instance
+ */
+ public FileResourceResourceManager(IStorageFactory storageFactory, ViewContext context) {
+ super(FileResourceItem.class, storageFactory, context);
+ }
+
+ @Override
+ public FileResourceItem create(FileResourceItem object) {
+ return super.create(object);
+ }
+
+ @Override
+ public FileResourceItem read(Object id) throws ItemNotFound {
+ return super.read(id);
+ }
+
+ @Override
+ public void delete(Object resourceId) throws ItemNotFound {
+ super.delete(resourceId);
+ }
+
+ @Override
+ public List<FileResourceItem> readAll(FilteringStrategy filteringStrategy) {
+ return super.readAll(filteringStrategy);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceResourceProvider.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceResourceProvider.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceResourceProvider.java
new file mode 100644
index 0000000..9e7c00a
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceResourceProvider.java
@@ -0,0 +1,110 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.resources;
+
+import org.apache.ambari.view.*;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.persistence.utils.OnlyOwnersFilteringStrategy;
+import org.apache.ambari.view.hive2.utils.SharedObjectsFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Resource provider for resource
+ */
+public class FileResourceResourceProvider implements ResourceProvider<FileResourceItem> {
+ @Inject
+ ViewContext context;
+
+ protected FileResourceResourceManager resourceManager = null;
+ protected final static Logger LOG =
+ LoggerFactory.getLogger(FileResourceResourceProvider.class);
+
+ protected synchronized FileResourceResourceManager getResourceManager() {
+ if (resourceManager == null) {
+ resourceManager = new FileResourceResourceManager(new SharedObjectsFactory(context), context);
+ }
+ return resourceManager;
+ }
+
+ @Override
+ public FileResourceItem getResource(String resourceId, Set<String> properties) throws SystemException, NoSuchResourceException, UnsupportedPropertyException {
+ try {
+ return getResourceManager().read(resourceId);
+ } catch (ItemNotFound itemNotFound) {
+ throw new NoSuchResourceException(resourceId);
+ }
+ }
+
+ @Override
+ public Set<FileResourceItem> getResources(ReadRequest readRequest) throws SystemException, NoSuchResourceException, UnsupportedPropertyException {
+ if (context == null) {
+ return new HashSet<FileResourceItem>();
+ }
+ return new HashSet<FileResourceItem>(getResourceManager().readAll(
+ new OnlyOwnersFilteringStrategy(this.context.getUsername())));
+ }
+
+ @Override
+ public void createResource(String s, Map<String, Object> stringObjectMap) throws SystemException, ResourceAlreadyExistsException, NoSuchResourceException, UnsupportedPropertyException {
+ FileResourceItem item = null;
+ try {
+ item = new FileResourceItem(stringObjectMap);
+ } catch (InvocationTargetException e) {
+ throw new SystemException("error on creating resource", e);
+ } catch (IllegalAccessException e) {
+ throw new SystemException("error on creating resource", e);
+ }
+ getResourceManager().create(item);
+ }
+
+ @Override
+ public boolean updateResource(String resourceId, Map<String, Object> stringObjectMap) throws SystemException, NoSuchResourceException, UnsupportedPropertyException {
+ FileResourceItem item = null;
+ try {
+ item = new FileResourceItem(stringObjectMap);
+ } catch (InvocationTargetException e) {
+ throw new SystemException("error on updating resource", e);
+ } catch (IllegalAccessException e) {
+ throw new SystemException("error on updating resource", e);
+ }
+ try {
+ getResourceManager().update(item, resourceId);
+ } catch (ItemNotFound itemNotFound) {
+ throw new NoSuchResourceException(resourceId);
+ }
+ return true;
+ }
+
+ @Override
+ public boolean deleteResource(String resourceId) throws SystemException, NoSuchResourceException, UnsupportedPropertyException {
+ try {
+ getResourceManager().delete(resourceId);
+ } catch (ItemNotFound itemNotFound) {
+ throw new NoSuchResourceException(resourceId);
+ }
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceService.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceService.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceService.java
new file mode 100644
index 0000000..2ce74c3
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/resources/FileResourceService.java
@@ -0,0 +1,180 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.resources;
+
+import org.apache.ambari.view.ViewResourceHandler;
+import org.apache.ambari.view.hive2.BaseService;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.persistence.utils.OnlyOwnersFilteringStrategy;
+import org.apache.ambari.view.hive2.utils.NotFoundFormattedException;
+import org.apache.ambari.view.hive2.utils.ServiceFormattedException;
+import org.json.simple.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.*;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import java.util.List;
+
+/**
+ * Servlet for Resources
+ * API:
+ * GET /:id
+ * read resource
+ * POST /
+ * create new resource
+ * GET /
+ * get all resource of current user
+ */
+public class FileResourceService extends BaseService {
+ @Inject
+ ViewResourceHandler handler;
+
+ protected FileResourceResourceManager resourceManager = null;
+ protected final static Logger LOG =
+ LoggerFactory.getLogger(FileResourceService.class);
+
+ protected synchronized FileResourceResourceManager getResourceManager() {
+ if (resourceManager == null) {
+ resourceManager = new FileResourceResourceManager(getSharedObjectsFactory(), context);
+ }
+ return resourceManager;
+ }
+
+ /**
+ * Get single item
+ */
+ @GET
+ @Path("{id}")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getOne(@PathParam("id") String id) {
+ try {
+ FileResourceItem fileResourceItem = getResourceManager().read(id);
+ JSONObject object = new JSONObject();
+ object.put("fileResource", fileResourceItem);
+ return Response.ok(object).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Delete single item
+ */
+ @DELETE
+ @Path("{id}")
+ public Response delete(@PathParam("id") String id) {
+ try {
+ getResourceManager().delete(id);
+ return Response.status(204).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Get all resources
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getList() {
+ try {
+ LOG.debug("Getting all resources");
+ List items = getResourceManager().readAll(
+ new OnlyOwnersFilteringStrategy(this.context.getUsername())); //TODO: move strategy to PersonalCRUDRM
+
+ JSONObject object = new JSONObject();
+ object.put("fileResources", items);
+ return Response.ok(object).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Update item
+ */
+ @PUT
+ @Path("{id}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response update(ResourceRequest request,
+ @PathParam("id") String id) {
+ try {
+ getResourceManager().update(request.fileResource, id);
+ return Response.status(204).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Create resource
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response create(ResourceRequest request, @Context HttpServletResponse response,
+ @Context UriInfo ui) {
+ try {
+ getResourceManager().create(request.fileResource);
+
+ FileResourceItem item = null;
+
+ item = getResourceManager().read(request.fileResource.getId());
+
+ response.setHeader("Location",
+ String.format("%s/%s", ui.getAbsolutePath().toString(), request.fileResource.getId()));
+
+ JSONObject object = new JSONObject();
+ object.put("fileResource", item);
+ return Response.ok(object).status(201).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Wrapper object for json mapping
+ */
+ public static class ResourceRequest {
+ public FileResourceItem fileResource;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQuery.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQuery.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQuery.java
new file mode 100644
index 0000000..f52cde3
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQuery.java
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.savedQueries;
+
+import org.apache.ambari.view.hive2.persistence.utils.PersonalResource;
+import org.apache.commons.beanutils.BeanUtils;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+
+/**
+ * Bean to represent saved query
+ */
+public class SavedQuery implements Serializable, PersonalResource {
+ private String queryFile;
+ private String dataBase;
+ private String title;
+ private String shortQuery;
+
+ private String id;
+ private String owner;
+
+ public SavedQuery() {}
+ public SavedQuery(Map<String, Object> stringObjectMap) throws InvocationTargetException, IllegalAccessException {
+ BeanUtils.populate(this, stringObjectMap);
+ }
+
+ @Override
+ public String getId() {
+ return id;
+ }
+
+ @Override
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public String getOwner() {
+ return owner;
+ }
+
+ @Override
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
+
+ public String getQueryFile() {
+ return queryFile;
+ }
+
+ public void setQueryFile(String queryFile) {
+ this.queryFile = queryFile;
+ }
+
+ public String getDataBase() {
+ return dataBase;
+ }
+
+ public void setDataBase(String dataBase) {
+ this.dataBase = dataBase;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public String getShortQuery() {
+ return shortQuery;
+ }
+
+ public void setShortQuery(String shortQuery) {
+ this.shortQuery = shortQuery;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryResourceManager.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryResourceManager.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryResourceManager.java
new file mode 100644
index 0000000..ff8632f
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryResourceManager.java
@@ -0,0 +1,162 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.savedQueries;
+
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.hive2.persistence.utils.FilteringStrategy;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.resources.PersonalCRUDResourceManager;
+import org.apache.ambari.view.hive2.utils.*;
+import org.apache.ambari.view.utils.hdfs.HdfsApiException;
+import org.apache.ambari.view.utils.hdfs.HdfsUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * Object that provides CRUD operations for query objects
+ */
+public class SavedQueryResourceManager extends PersonalCRUDResourceManager<SavedQuery> {
+ private final static Logger LOG =
+ LoggerFactory.getLogger(SavedQueryResourceManager.class);
+
+ private SharedObjectsFactory sharedObjectsFactory;
+
+ /**
+ * Constructor
+ * @param context View Context instance
+ */
+ public SavedQueryResourceManager(ViewContext context, SharedObjectsFactory sharedObjectsFactory) {
+ super(SavedQuery.class, sharedObjectsFactory, context);
+ this.sharedObjectsFactory = sharedObjectsFactory;
+ }
+
+ @Override
+ public SavedQuery create(SavedQuery object) {
+ object = super.create(object);
+ try {
+
+ if (object.getQueryFile() == null || object.getQueryFile().isEmpty()) {
+ createDefaultQueryFile(object);
+ }
+
+ } catch (ServiceFormattedException e) {
+ cleanupAfterErrorAndThrowAgain(object, e);
+ }
+ return object;
+ }
+
+ private void createDefaultQueryFile(SavedQuery object) {
+ String userScriptsPath = context.getProperties().get("scripts.dir");
+ if (userScriptsPath == null) {
+ String msg = "scripts.dir is not configured!";
+ LOG.error(msg);
+ throw new MisconfigurationFormattedException("scripts.dir");
+ }
+
+ String normalizedName = String.format("hive-query-%s", object.getId());
+ String timestamp = new SimpleDateFormat("yyyy-MM-dd_hh-mm").format(new Date());
+ String baseFileName = String.format(userScriptsPath +
+ "/%s-%s", normalizedName, timestamp);
+
+ String newFilePath = null;
+ try {
+ newFilePath = HdfsUtil.findUnallocatedFileName(sharedObjectsFactory.getHdfsApi(), baseFileName, ".hql");
+ HdfsUtil.putStringToFile(sharedObjectsFactory.getHdfsApi(), newFilePath, "");
+ } catch (HdfsApiException e) {
+ throw new ServiceFormattedException(e);
+ }
+
+ object.setQueryFile(newFilePath);
+ storageFactory.getStorage().store(SavedQuery.class, object);
+ }
+
+ @Override
+ public SavedQuery read(Object id) throws ItemNotFound {
+ SavedQuery savedQuery = super.read(id);
+ fillShortQueryField(savedQuery);
+ return savedQuery;
+ }
+
+ private void fillShortQueryField(SavedQuery savedQuery) {
+ if (savedQuery.getQueryFile() != null) {
+ FilePaginator paginator = new FilePaginator(savedQuery.getQueryFile(), sharedObjectsFactory.getHdfsApi());
+ String query = null;
+ try {
+ query = paginator.readPage(0);
+ } catch (IOException e) {
+ LOG.error("Can't read query file " + savedQuery.getQueryFile());
+ return;
+ } catch (InterruptedException e) {
+ LOG.error("Can't read query file " + savedQuery.getQueryFile());
+ return;
+ }
+ savedQuery.setShortQuery(makeShortQuery(query));
+ }
+ storageFactory.getStorage().store(SavedQuery.class, savedQuery);
+ }
+
+ private void emptyShortQueryField(SavedQuery query) {
+ query.setShortQuery("");
+ storageFactory.getStorage().store(SavedQuery.class, query);
+ }
+
+ /**
+ * Generate short preview of query.
+ * Remove SET settings like "set hive.execution.engine=tez;" from beginning
+ * and trim to 42 symbols.
+ * @param query full query
+ * @return shortened query
+ */
+ protected static String makeShortQuery(String query) {
+ query = query.replaceAll("(?i)set\\s+[\\w\\-.]+(\\s*)=(\\s*)[\\w\\-.]+(\\s*);", "");
+ query = query.trim();
+ return query.substring(0, (query.length() > 42)?42:query.length());
+ }
+
+ @Override
+ public SavedQuery update(SavedQuery newObject, String id) throws ItemNotFound {
+ SavedQuery savedQuery = super.update(newObject, id);
+ // Emptying short query so that in next read, this gets updated with proper value
+ // from the queryFile
+ emptyShortQueryField(savedQuery);
+ return savedQuery;
+ }
+
+ @Override
+ public List<SavedQuery> readAll(FilteringStrategy filteringStrategy) {
+ List<SavedQuery> queries = super.readAll(filteringStrategy);
+ for(SavedQuery query : queries) {
+ String shortQuery = query.getShortQuery();
+ if(shortQuery == null || shortQuery.isEmpty()) {
+ fillShortQueryField(query);
+ }
+ }
+ return queries;
+ }
+
+ @Override
+ public void delete(Object resourceId) throws ItemNotFound {
+ super.delete(resourceId);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryResourceProvider.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryResourceProvider.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryResourceProvider.java
new file mode 100644
index 0000000..87f3321
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryResourceProvider.java
@@ -0,0 +1,113 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.savedQueries;
+
+import org.apache.ambari.view.*;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.persistence.utils.OnlyOwnersFilteringStrategy;
+import org.apache.ambari.view.hive2.utils.SharedObjectsFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Resource provider for SavedQuery
+ */
+public class SavedQueryResourceProvider implements ResourceProvider<SavedQuery> {
+ @Inject
+ ViewContext context;
+
+ protected final static Logger LOG =
+ LoggerFactory.getLogger(SavedQueryResourceProvider.class);
+ private SharedObjectsFactory sharedObjectsFactory;
+
+ public SharedObjectsFactory getSharedObjectsFactory() {
+ if (sharedObjectsFactory == null)
+ sharedObjectsFactory = new SharedObjectsFactory(context);
+ return sharedObjectsFactory;
+ }
+
+ protected synchronized SavedQueryResourceManager getResourceManager() {
+ return getSharedObjectsFactory().getSavedQueryResourceManager();
+ }
+
+ @Override
+ public SavedQuery getResource(String resourceId, Set<String> properties) throws SystemException, NoSuchResourceException, UnsupportedPropertyException {
+ try {
+ return getResourceManager().read(resourceId);
+ } catch (ItemNotFound itemNotFound) {
+ throw new NoSuchResourceException(resourceId);
+ }
+ }
+
+ @Override
+ public Set<SavedQuery> getResources(ReadRequest readRequest) throws SystemException, NoSuchResourceException, UnsupportedPropertyException {
+ if (context == null) {
+ return new HashSet<SavedQuery>();
+ }
+ return new HashSet<SavedQuery>(getResourceManager().readAll(
+ new OnlyOwnersFilteringStrategy(this.context.getUsername())));
+ }
+
+ @Override
+ public void createResource(String s, Map<String, Object> stringObjectMap) throws SystemException, ResourceAlreadyExistsException, NoSuchResourceException, UnsupportedPropertyException {
+ SavedQuery item = null;
+ try {
+ item = new SavedQuery(stringObjectMap);
+ } catch (InvocationTargetException e) {
+ throw new SystemException("error on creating resource", e);
+ } catch (IllegalAccessException e) {
+ throw new SystemException("error on creating resource", e);
+ }
+ getResourceManager().create(item);
+ }
+
+ @Override
+ public boolean updateResource(String resourceId, Map<String, Object> stringObjectMap) throws SystemException, NoSuchResourceException, UnsupportedPropertyException {
+ SavedQuery item = null;
+ try {
+ item = new SavedQuery(stringObjectMap);
+ } catch (InvocationTargetException e) {
+ throw new SystemException("error on updating resource", e);
+ } catch (IllegalAccessException e) {
+ throw new SystemException("error on updating resource", e);
+ }
+ try {
+ getResourceManager().update(item, resourceId);
+ } catch (ItemNotFound itemNotFound) {
+ throw new NoSuchResourceException(resourceId);
+ }
+ return true;
+ }
+
+ @Override
+ public boolean deleteResource(String resourceId) throws SystemException, NoSuchResourceException, UnsupportedPropertyException {
+ try {
+ getResourceManager().delete(resourceId);
+ } catch (ItemNotFound itemNotFound) {
+ throw new NoSuchResourceException(resourceId);
+ }
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java
new file mode 100644
index 0000000..ccc4512
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/savedQueries/SavedQueryService.java
@@ -0,0 +1,230 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.savedQueries;
+
+import org.apache.ambari.view.ViewResourceHandler;
+import org.apache.ambari.view.hive2.BaseService;
+import org.apache.ambari.view.hive2.persistence.utils.ItemNotFound;
+import org.apache.ambari.view.hive2.persistence.utils.OnlyOwnersFilteringStrategy;
+import org.apache.ambari.view.hive2.utils.NotFoundFormattedException;
+import org.apache.ambari.view.hive2.utils.ServiceFormattedException;
+import org.apache.ambari.view.utils.hdfs.HdfsApi;
+import org.apache.ambari.view.utils.hdfs.HdfsUtil;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.*;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import java.util.List;
+
+/**
+ * Servlet for queries
+ * API:
+ * GET /:id
+ * read SavedQuery
+ * POST /
+ * create new SavedQuery
+ * Required: title, queryFile
+ * GET /
+ * get all SavedQueries of current user
+ */
+public class SavedQueryService extends BaseService {
+ @Inject
+ ViewResourceHandler handler;
+
+ protected SavedQueryResourceManager resourceManager = null;
+ protected final static Logger LOG =
+ LoggerFactory.getLogger(SavedQueryService.class);
+
+ protected synchronized SavedQueryResourceManager getResourceManager() {
+ return getSharedObjectsFactory().getSavedQueryResourceManager();
+ }
+
+ protected void setResourceManager(SavedQueryResourceManager resourceManager) {
+ this.resourceManager = resourceManager;
+ }
+
+ /**
+ * Get single item
+ */
+ @GET
+ @Path("{queryId}")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getOne(@PathParam("queryId") String queryId) {
+ try {
+ SavedQuery savedQuery = getResourceManager().read(queryId);
+ JSONObject object = new JSONObject();
+ object.put("savedQuery", savedQuery);
+ return Response.ok(object).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Delete single item
+ */
+ @DELETE
+ @Path("{queryId}")
+ public Response delete(@PathParam("queryId") String queryId) {
+ try {
+ getResourceManager().delete(queryId);
+ return Response.status(204).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Get all SavedQueries
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getList() {
+ try {
+ LOG.debug("Getting all SavedQuery");
+ List allSavedQueries = getResourceManager().readAll(
+ new OnlyOwnersFilteringStrategy(this.context.getUsername())); //TODO: move strategy to PersonalCRUDRM
+
+ JSONObject object = new JSONObject();
+ object.put("savedQueries", allSavedQueries);
+ return Response.ok(object).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Update item
+ */
+ @PUT
+ @Path("{queryId}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response update(SavedQueryRequest request,
+ @PathParam("queryId") String queryId) {
+ try {
+ getResourceManager().update(request.savedQuery, queryId);
+ return Response.status(204).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Create savedQuery
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response create(SavedQueryRequest request, @Context HttpServletResponse response,
+ @Context UriInfo ui) {
+ try {
+ getResourceManager().create(request.savedQuery);
+
+ SavedQuery item = null;
+
+ item = getResourceManager().read(request.savedQuery.getId());
+
+ response.setHeader("Location",
+ String.format("%s/%s", ui.getAbsolutePath().toString(), request.savedQuery.getId()));
+
+ JSONObject object = new JSONObject();
+ object.put("savedQuery", item);
+ return Response.ok(object).status(201).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (ItemNotFound itemNotFound) {
+ throw new NotFoundFormattedException(itemNotFound.getMessage(), itemNotFound);
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Get default settings for query
+ */
+ @GET
+ @Path("defaultSettings")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getDefaultSettings() {
+ try {
+ String defaultsFile = context.getProperties().get("scripts.settings.defaults-file");
+ HdfsApi hdfsApi = getSharedObjectsFactory().getHdfsApi();
+
+ String defaults = "{\"settings\": {}}";
+ if (hdfsApi.exists(defaultsFile)) {
+ defaults = HdfsUtil.readFile(hdfsApi, defaultsFile);
+ }
+ return Response.ok(JSONValue.parse(defaults)).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Set default settings for query (overwrites if present)
+ */
+ @POST
+ @Path("defaultSettings")
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response setDefaultSettings(JSONObject settings) {
+ try {
+ String defaultsFile = context.getProperties().get("scripts.settings.defaults-file");
+ HdfsApi hdfsApi = getSharedObjectsFactory().getHdfsApi();
+
+ HdfsUtil.putStringToFile(hdfsApi, defaultsFile,
+ settings.toString());
+ String defaults = HdfsUtil.readFile(hdfsApi, defaultsFile);
+ return Response.ok(JSONValue.parse(defaults)).build();
+ } catch (WebApplicationException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new ServiceFormattedException(ex.getMessage(), ex);
+ }
+ }
+
+ /**
+ * Wrapper object for json mapping
+ */
+ public static class SavedQueryRequest {
+ public SavedQuery savedQuery;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/9dc382a5/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/udfs/UDF.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/udfs/UDF.java b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/udfs/UDF.java
new file mode 100644
index 0000000..aa863a6
--- /dev/null
+++ b/contrib/views/hive-next/src/main/java/org/apache/ambari/view/hive2/resources/udfs/UDF.java
@@ -0,0 +1,87 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.view.hive2.resources.udfs;
+
+import org.apache.ambari.view.hive2.persistence.utils.PersonalResource;
+import org.apache.commons.beanutils.BeanUtils;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
+
+/**
+ * Bean to represent UDF
+ */
+public class UDF implements Serializable, PersonalResource {
+ private String name;
+ private String classname;
+ private String fileResource;
+
+ private String id;
+ private String owner;
+
+ public UDF() {}
+ public UDF(Map<String, Object> stringObjectMap) throws InvocationTargetException, IllegalAccessException {
+ BeanUtils.populate(this, stringObjectMap);
+ }
+
+ @Override
+ public String getId() {
+ return id;
+ }
+
+ @Override
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public String getOwner() {
+ return owner;
+ }
+
+ @Override
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getClassname() {
+ return classname;
+ }
+
+ public void setClassname(String classname) {
+ this.classname = classname;
+ }
+
+ public String getFileResource() {
+ return fileResource;
+ }
+
+ public void setFileResource(String fileResource) {
+ this.fileResource = fileResource;
+ }
+}