ambari 服务如何重启
时间: 2023-09-03 13:13:09 浏览: 392
要重启Ambari服务,可以按照以下步骤进行操作:
1. 登录到Ambari服务器的命令行界面。
2. 运行以下命令以停止Ambari服务:
```
ambari-server stop
```
3. 等待Ambari服务完全停止。
4. 运行以下命令以启动Ambari服务:
```
ambari-server start
```
5. 等待Ambari服务完全启动。
重启Ambari服务后,您应该能够访问Ambari Web界面并开始管理您的Hadoop集群。请注意,重启Ambari服务可能会导致您当前正在运行的任何作业或任务中断,请确保在执行此操作之前备份您的数据。
相关问题
java通过登录ambari页面重启各服务。服务名称可传入
您可以使用 Ambari 的 REST API 通过 Java 代码来重启服务。以下是一个示例代码片段:
```java
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AmbariServiceRestart {
private static final String AMBARI_SERVER_URL = "http://your.ambari.server:8080";
private static final String CLUSTER_NAME = "your-cluster-name";
private static final String USERNAME = "your-username";
private static final String PASSWORD = "your-password";
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
// 登录 Ambari
String ambariSessionId = login(httpClient, AMBARI_SERVER_URL, USERNAME, PASSWORD);
// 获取集群服务
String services = getServices(httpClient, AMBARI_SERVER_URL, CLUSTER_NAME, ambariSessionId);
// 重启指定服务
restartService(httpClient, AMBARI_SERVER_URL, CLUSTER_NAME, ambariSessionId, "HDFS");
// 退出 Ambari
logout(httpClient, AMBARI_SERVER_URL, ambariSessionId);
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String login(CloseableHttpClient httpClient, String ambariServerUrl, String username,
String password) {
String sessionId = null;
String loginUrl = ambariServerUrl + "/api/v1/session";
HttpPost httpPost = new HttpPost(loginUrl);
httpPost.setHeader("X-Requested-By", "ambari");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
JSONObject requestBody = new JSONObject();
requestBody.put("username", username);
requestBody.put("password", password);
StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
httpPost.setEntity(requestEntity);
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String inputLine;
StringBuffer responseBuffer = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
responseBuffer.append(inputLine);
}
JSONObject jsonResponse = new JSONObject(responseBuffer.toString());
sessionId = jsonResponse.getString("session_id");
} catch (IOException e) {
e.printStackTrace();
}
return sessionId;
}
private static void logout(CloseableHttpClient httpClient, String ambariServerUrl, String sessionId) {
String logoutUrl = ambariServerUrl + "/api/v1/logout";
HttpDelete httpDelete = new HttpDelete(logoutUrl);
httpDelete.setHeader("X-Requested-By", "ambari");
httpDelete.setHeader("Cookie", "AMBARISESSIONID=" + sessionId);
try {
httpClient.execute(httpDelete);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getServices(CloseableHttpClient httpClient, String ambariServerUrl, String clusterName,
String sessionId) {
String services = null;
String servicesUrl = ambariServerUrl + "/api/v1/clusters/" + clusterName + "/services";
HttpGet httpGet = new HttpGet(servicesUrl);
httpGet.setHeader("X-Requested-By", "ambari");
httpGet.setHeader("Cookie", "AMBARISESSIONID=" + sessionId);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String inputLine;
StringBuffer responseBuffer = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
responseBuffer.append(inputLine);
}
services = responseBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return services;
}
private static void restartService(CloseableHttpClient httpClient, String ambariServerUrl, String clusterName,
String sessionId, String serviceName) {
String restartUrl = ambariServerUrl + "/api/v1/clusters/" + clusterName + "/services/" + serviceName;
HttpPost httpPost = new HttpPost(restartUrl);
httpPost.setHeader("X-Requested-By", "ambari");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Cookie", "AMBARISESSIONID=" + sessionId);
JSONObject requestBody = new JSONObject();
requestBody.put("RequestInfo", new JSONObject().put("command", "RESTART"));
requestBody.put("ServiceInfo", new JSONObject().put("state", "INSTALLED"));
StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
httpPost.setEntity(requestEntity);
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String inputLine;
StringBuffer responseBuffer = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
responseBuffer.append(inputLine);
}
System.out.println(responseBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
此示例代码通过 `login()` 方法登录 Ambari,并获取当前集群的服务列表;然后使用 `restartService()` 方法重启指定服务;最后使用 `logout()` 方法退出 Ambari。
请注意,您需要将 `AMBARI_SERVER_URL`、`CLUSTER_NAME`、`USERNAME` 和 `PASSWORD` 替换为您的 Ambari 服务器 URL、集群名称、用户名和密码。另外,如果您要重启多个服务,只需多次调用 `restartService()` 方法即可。
python脚本通过登重启录ambari页面重启各服务。服务名称可指定
可以使用 Python 的 requests 模块来编写一个脚本,通过 Ambari REST API 来重启指定的服务。
以下是一个简单的示例代码,需要替换 `AMBARI_HOST`、`AMBARI_PORT`、`AMBARI_USERNAME`、`AMBARI_PASSWORD`、`CLUSTER_NAME` 和 `SERVICE_NAME` 的值,以适应你的环境和需求。
```python
import requests
from requests.auth import HTTPBasicAuth
AMBARI_HOST = 'your.ambari.host'
AMBARI_PORT = '8080'
AMBARI_USERNAME = 'your.ambari.username'
AMBARI_PASSWORD = 'your.ambari.password'
CLUSTER_NAME = 'your.cluster.name'
SERVICE_NAME = 'your.service.name'
# 构建 REST API URL
url = 'http://{0}:{1}/api/v1/clusters/{2}/services/{3}'.format(
AMBARI_HOST, AMBARI_PORT, CLUSTER_NAME, SERVICE_NAME)
# 构建请求头和认证信息
headers = {'X-Requested-By': 'ambari', 'Content-Type': 'application/json'}
auth = HTTPBasicAuth(AMBARI_USERNAME, AMBARI_PASSWORD)
# 发送重启请求
response = requests.put(url=url, headers=headers, auth=auth, json={'RequestInfo': {'command': 'RESTART'}})
if response.status_code == 202:
print('Successfully restarted {0} service.'.format(SERVICE_NAME))
else:
print('Failed to restart {0} service. Reason: {1}'.format(SERVICE_NAME, response.text))
```
如果需要同时重启多个服务,可以在循环中调用该脚本,并在 `SERVICE_NAME` 参数中传入不同的服务名称。
阅读全文