Skip to content

Instantly share code, notes, and snippets.

@bbhavsar
Created October 30, 2020 21:33
Show Gist options
  • Save bbhavsar/08d5804b8dc19bef394cc7e27961d8f0 to your computer and use it in GitHub Desktop.
Save bbhavsar/08d5804b8dc19bef394cc7e27961d8f0 to your computer and use it in GitHub Desktop.
Kerberos with UGI
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.kudu.client.CreateTableOptions;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduClient.KuduClientBuilder;
import org.apache.kudu.ColumnSchema.ColumnSchemaBuilder;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.KuduException;
import org.apache.kudu.client.ListTablesResponse;
import java.io.IOException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
public class Main {
static void CreateTable(KuduClient client) {
Schema schema = new Schema(Arrays.asList(
new ColumnSchemaBuilder("ssn", Type.STRING).key(true).build(),
new ColumnSchemaBuilder("firstName", Type.STRING).build(),
new ColumnSchemaBuilder("lastName", Type.STRING).build(),
new ColumnSchemaBuilder("email", Type.STRING).build())
);
CreateTableOptions tableOptions =
new CreateTableOptions().setNumReplicas(1).addHashPartitions(Arrays.asList("ssn"), 4);
try {
client.createTable("kudu_app_table", schema, tableOptions);
} catch (KuduException ke) {
System.out.println("Failed creating table: " + ke.getMessage());
}
}
static void ListTables(KuduClient client) {
ListTablesResponse resp;
try {
resp = client.getTablesList();
} catch (KuduException ke) {
System.out.println("Error listing tables: " + ke.getMessage());
return;
}
for (String table : resp.getTablesList()) {
System.out.println(table);
}
}
public static void main(String[] args) {
KuduClient client;
try {
UserGroupInformation.loginUserFromKeytab("hive/dwx-env-lqbmsl@GCE.CLOUDERA.COM",
"/etc/security/keytabs/hive.service.keytab");
client = UserGroupInformation.getLoginUser().doAs(
new PrivilegedExceptionAction<KuduClient>() {
public KuduClient run() throws Exception {
return new KuduClientBuilder("bankim-b-1.gce.cloudera.com:7051").build();
}
}
);
} catch (Exception ex) {
System.out.println("Error in UserGroupInformation: " + ex.getMessage());
return;
}
ListTables(client);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment