Access HBase Rest API through KNOX - use jars: httpcore-4.4.9.jar, commons-logging-1.2.jar, json-20180130.jar and httpclient-4.5.2.jar
import java.util.Base64;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.ssl.SSLContextBuilder;
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.conn.ssl.*;
public class knoxDemoClientRest {
public static void main(String[] args) throws Exception {
JSONObject jsonObj = new JSONObject();
CloseableHttpClient client = createAcceptSelfSignedCertificateClient();
HttpGet http_get = new HttpGet("https://127.0.0.1:8443/gateway/default/hbase/NS:MARCOS/row1");
String user = "guest";
String pass = "guest-password";
String auth = Base64.getEncoder().encodeToString(String.format("%s:%s", user, pass).getBytes());
http_get.addHeader("Accept", "application/json");
http_get.addHeader("Authorization", "Basic " + auth);
CloseableHttpResponse response = client.execute(http_get);
String body = EntityUtils.toString(response.getEntity(), "UTF-8");
client.close();
if (response.getStatusLine().getStatusCode() != 200) {
System.out.println("Error: " + response.getStatusLine());
System.exit(1);
}
try {
jsonObj = new JSONObject(body);
} catch (JSONException e) {
System.out.println("Error: " + e.getMessage());
System.exit(1);
}
for(int i = 0; i < jsonObj.getJSONArray("Row").getJSONObject(0).getJSONArray("Cell").length(); i++) {
long timestamp = jsonObj.getJSONArray("Row").getJSONObject(0).getJSONArray("Cell").getJSONObject(i).getLong("timestamp");
String family = new String(Base64.getDecoder().decode(jsonObj.getJSONArray("Row").getJSONObject(0).getJSONArray("Cell").getJSONObject(i).getString("column"))).split(":")[0];
String qualifier = new String(Base64.getDecoder().decode(jsonObj.getJSONArray("Row").getJSONObject(0).getJSONArray("Cell").getJSONObject(i).getString("column"))).split(":")[1];
String value = new String(Base64.getDecoder().decode(jsonObj.getJSONArray("Row").getJSONObject(0).getJSONArray("Cell").getJSONObject(i).getString("$")));
if (i > 0){
System.out.println("--------");
}
System.out.println("family = " + family);
System.out.println("qualifier = " + qualifier);
System.out.println("value = " + value);
System.out.println("timestamp = " + timestamp);
}
}
private static CloseableHttpClient createAcceptSelfSignedCertificateClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(new TrustSelfSignedStrategy()).build();
HostnameVerifier allowAllHosts = new NoopHostnameVerifier();
SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, allowAllHosts);
return HttpClients.custom().setSSLSocketFactory(connectionFactory).build();
}
}