Project

General

Profile

1
package eu.dnetlib.data.hadoop;
2

    
3
import java.io.IOException;
4
import java.util.Map;
5

    
6
import com.google.common.collect.Maps;
7
import com.google.gson.Gson;
8
import eu.dnetlib.data.hadoop.config.ClusterName;
9
import eu.dnetlib.data.hadoop.hbase.HBaseAdminFactory;
10
import eu.dnetlib.data.hadoop.mapred.JobClientFactory;
11
import eu.dnetlib.data.hadoop.oozie.OozieClientFactory;
12
import eu.dnetlib.data.hadoop.rmi.HadoopServiceException;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.apache.hadoop.hbase.client.HBaseAdmin;
16
import org.apache.hadoop.mapred.JobClient;
17
import org.apache.oozie.client.OozieClient;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Required;
20

    
21

    
22
public class HadoopClientMap {
23

    
24
	private static final Log log = LogFactory.getLog(HadoopClientMap.class); // NOPMD by marko on 11/24/08 5:02 PM
25

    
26
	@Autowired
27
	private JobClientFactory jobClientFactory;
28

    
29
	@Autowired
30
	private OozieClientFactory oozieClientFactory;
31

    
32
	@Autowired
33
	private HBaseAdminFactory hbaseAdminFactory;
34

    
35
	private Map<String, Map<String, String>> enabledClients = Maps.newHashMap();
36

    
37
	public boolean isMapreduceAvailable(final ClusterName name) {
38
		return isClientAvailable(name, "mapred");
39
	}
40

    
41
	public boolean isOozieAvailable(final ClusterName name) {
42
		return isClientAvailable(name, "oozie");
43
	}
44

    
45
	public boolean isHbaseAvailable(final ClusterName name) {
46
		return isClientAvailable(name, "hbase");
47
	}
48

    
49
	private boolean isClientAvailable(final ClusterName name, final String clientName) {
50
		final String clusterName = name.toString();
51
		return enabledClients.containsKey(clusterName) && "true".equals(enabledClients.get(clusterName).get(clientName));
52
	}
53

    
54
	public JobClient getJtClient(final ClusterName clusterName, final String username) throws HadoopServiceException, IOException {
55
		if (!isMapreduceAvailable(clusterName)) {
56
			throw new HadoopServiceException("jobtracker is not available for cluster " + clusterName.toString());
57
		}
58
		return getJobClientFactory().newInstance(clusterName, username);
59
	}
60

    
61
	public JobClient getJtClient(final ClusterName clusterName) throws HadoopServiceException, IOException {
62
		if (!isMapreduceAvailable(clusterName)) {
63
			throw new HadoopServiceException("jobtracker is not available for cluster " + clusterName.toString());
64
		}
65
		return getJobClientFactory().newInstance(clusterName);
66
	}
67

    
68
	public OozieClient getOozieClient(final ClusterName name) throws HadoopServiceException {
69
		if (!isOozieAvailable(name)) {
70
			throw new HadoopServiceException("oozie is not available for cluster " + name.toString());
71
		}
72
		return getOozieClientFactory().newInstance(name);
73
	}
74

    
75
	public HBaseAdmin getHbaseAdmin(final ClusterName name) throws HadoopServiceException {
76
		if (!isHbaseAvailable(name)) {
77
			throw new HadoopServiceException("hbase is not available for cluster " + name.toString());
78
		}
79
		return getHbaseAdminFactory().newInstance(name);
80
	}
81

    
82
	// //////////
83

    
84
	public String getEnabledClients() {
85
		return new Gson().toJson(enabledClients);
86
	}
87

    
88
	@Required
89
	@SuppressWarnings("unchecked")
90
	public void setEnabledClients(final String enabledClients) {
91
		this.enabledClients = new Gson().fromJson(enabledClients, Map.class);
92
	}
93

    
94
	public JobClientFactory getJobClientFactory() {
95
		return jobClientFactory;
96
	}
97

    
98
	public void setJobClientFactory(JobClientFactory jobClientFactory) {
99
		this.jobClientFactory = jobClientFactory;
100
	}
101

    
102
	public OozieClientFactory getOozieClientFactory() {
103
		return oozieClientFactory;
104
	}
105

    
106
	public void setOozieClientFactory(OozieClientFactory oozieClientFactory) {
107
		this.oozieClientFactory = oozieClientFactory;
108
	}
109

    
110
	public HBaseAdminFactory getHbaseAdminFactory() {
111
		return hbaseAdminFactory;
112
	}
113

    
114
	public void setHbaseAdminFactory(HBaseAdminFactory hbaseAdminFactory) {
115
		this.hbaseAdminFactory = hbaseAdminFactory;
116
	}
117

    
118
}
(2-2/7)