Project

General

Profile

1
package eu.dnetlib.data.hadoop;
2

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

    
6
import eu.dnetlib.data.hadoop.mapred.JobClientFactory;
7
import eu.dnetlib.data.hadoop.rmi.HadoopServiceException;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.apache.hadoop.hbase.client.HBaseAdmin;
11
import org.apache.hadoop.mapred.JobClient;
12
import org.apache.oozie.client.OozieClient;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Required;
15
import org.springframework.context.annotation.Lazy;
16

    
17
import com.google.common.collect.Maps;
18
import com.google.gson.Gson;
19

    
20
import eu.dnetlib.data.hadoop.config.ClusterName;
21
import eu.dnetlib.data.hadoop.hbase.HBaseAdminFactory;
22
import eu.dnetlib.data.hadoop.oozie.OozieClientFactory;
23

    
24

    
25
public class HadoopClientMap {
26

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

    
29
	@Lazy
30
	@Autowired
31
	private JobClientFactory jobClientFactory;
32

    
33
	@Lazy
34
	@Autowired
35
	private OozieClientFactory oozieClientFactory;
36

    
37
	@Lazy
38
	@Autowired
39
	private HBaseAdminFactory hbaseAdminFactory;
40

    
41
	private Map<String, Map<String, String>> enabledClients = Maps.newHashMap();
42

    
43
	public boolean isMapreduceAvailable(final ClusterName name) {
44
		return isClientAvailable(name, "mapred");
45
	}
46

    
47
	public boolean isOozieAvailable(final ClusterName name) {
48
		return isClientAvailable(name, "oozie");
49
	}
50

    
51
	public boolean isHbaseAvailable(final ClusterName name) {
52
		return isClientAvailable(name, "hbase");
53
	}
54

    
55
	private boolean isClientAvailable(final ClusterName name, final String clientName) {
56
		final String clusterName = name.toString();
57
		return enabledClients.containsKey(clusterName) && "true".equals(enabledClients.get(clusterName).get(clientName));
58
	}
59

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

    
67
	public JobClient getJtClient(final ClusterName clusterName) throws HadoopServiceException, IOException {
68
		if (!isMapreduceAvailable(clusterName)) {
69
			throw new HadoopServiceException("jobtracker is not available for cluster " + clusterName.toString());
70
		}
71
		return getJobClientFactory().newInstance(clusterName);
72
	}
73

    
74
	public OozieClient getOozieClient(final ClusterName name) throws HadoopServiceException {
75
		if (!isOozieAvailable(name)) {
76
			throw new HadoopServiceException("oozie is not available for cluster " + name.toString());
77
		}
78
		return getOozieClientFactory().newInstance(name);
79
	}
80

    
81
	public HBaseAdmin getHbaseAdmin(final ClusterName name) throws HadoopServiceException {
82
		if (!isHbaseAvailable(name)) {
83
			throw new HadoopServiceException("hbase is not available for cluster " + name.toString());
84
		}
85
		return getHbaseAdminFactory().newInstance(name);
86
	}
87

    
88
	// //////////
89

    
90
	public String getEnabledClients() {
91
		return new Gson().toJson(enabledClients);
92
	}
93

    
94
	@Required
95
	@SuppressWarnings("unchecked")
96
	public void setEnabledClients(final String enabledClients) {
97
		this.enabledClients = new Gson().fromJson(enabledClients, Map.class);
98
	}
99

    
100
	public JobClientFactory getJobClientFactory() {
101
		return jobClientFactory;
102
	}
103

    
104
	public void setJobClientFactory(JobClientFactory jobClientFactory) {
105
		this.jobClientFactory = jobClientFactory;
106
	}
107

    
108
	public OozieClientFactory getOozieClientFactory() {
109
		return oozieClientFactory;
110
	}
111

    
112
	public void setOozieClientFactory(OozieClientFactory oozieClientFactory) {
113
		this.oozieClientFactory = oozieClientFactory;
114
	}
115

    
116
	public HBaseAdminFactory getHbaseAdminFactory() {
117
		return hbaseAdminFactory;
118
	}
119

    
120
	public void setHbaseAdminFactory(HBaseAdminFactory hbaseAdminFactory) {
121
		this.hbaseAdminFactory = hbaseAdminFactory;
122
	}
123

    
124
}
(2-2/7)