-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lisong
committed
Jul 15, 2019
1 parent
997d28b
commit 435fa34
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.transwarp</groupId> | ||
<artifactId>HdfsKerberosDemo</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.apache.hadoop</groupId> | ||
<artifactId>hadoop-hdfs</artifactId> | ||
<version>2.7.2-transwarp-5.2.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.hadoop</groupId> | ||
<artifactId>hadoop-common</artifactId> | ||
<version>2.7.2-transwarp-5.2.2</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.transwarp; | ||
|
||
/** | ||
* Created by qls on 18-10-26. | ||
*/ | ||
public class Constants { | ||
|
||
//hdfs | ||
static String HDFS_KERBEROS_PRINCIPAL = System.getenv("HDFS_KERBEROS_PRINCIPAL"); | ||
static String DEFAULT_HDFS_KEYTAB_FILE = "/etc/keytabs/keytab"; | ||
static String DEFAULT_HDFS_SITE_FILE = "/etc/hadoop/conf/hdfs-site.xml"; | ||
static String DEFAULT_HDFS_CORE_SITE_FILE = "/etc/hadoop/conf/core-site.xml"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.transwarp; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.security.UserGroupInformation; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Created by qls on 18-10-24. | ||
*/ | ||
public class HdfsClient { | ||
|
||
private FileSystem fileSystem; | ||
private Configuration conf = new Configuration(); | ||
|
||
private static String DEFAULT_HDFS_KEYTAB_FILE = Constants.DEFAULT_HDFS_KEYTAB_FILE; | ||
private static String DEFAULT_HDFS_SITE_FILE = Constants.DEFAULT_HDFS_SITE_FILE; | ||
private static String DEFAULT_HDFS_CORE_SITE_FILE = Constants.DEFAULT_HDFS_CORE_SITE_FILE; | ||
private static String HDFS_KERBEROS_PRINCIPAL = Constants.HDFS_KERBEROS_PRINCIPAL; | ||
|
||
public FileSystem getFileSystem() { | ||
return fileSystem; | ||
} | ||
|
||
public void setFileSystem(FileSystem fileSystem) { | ||
this.fileSystem = fileSystem; | ||
} | ||
|
||
public HdfsClient() throws Exception { | ||
|
||
conf.addResource(new Path(DEFAULT_HDFS_SITE_FILE)); | ||
conf.addResource(new Path(DEFAULT_HDFS_CORE_SITE_FILE)); | ||
conf.set("hadoop.security.authentication", "kerberos"); | ||
initKerberos(); | ||
|
||
this.fileSystem = FileSystem.get(conf); | ||
|
||
} | ||
|
||
private void initKerberos() throws Exception{ | ||
|
||
String principal = HDFS_KERBEROS_PRINCIPAL; | ||
System.out.println("===== HDFS_KERBEROS_PRINCIPAL: " + principal); | ||
System.out.println("===== DEFAULT_HDFS_KEYTAB_FILE: " + DEFAULT_HDFS_KEYTAB_FILE); | ||
if( principal == null || principal.length() <= 0 ){ | ||
System.out.println("cannot get principal"); | ||
throw new Exception("principal is null "); | ||
} | ||
System.out.println("Kerberos enabled, use principal: " + principal); | ||
UserGroupInformation.setConfiguration(conf); | ||
UserGroupInformation.loginUserFromKeytab(principal, DEFAULT_HDFS_KEYTAB_FILE); | ||
|
||
} | ||
|
||
|
||
public void mkdirDir(String dir) throws IOException { | ||
|
||
if (!fileSystem.exists(new Path(dir))) { | ||
|
||
fileSystem.mkdirs(new Path(dir)); | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.transwarp; | ||
|
||
/** | ||
* Created by qls on 18-10-22. | ||
*/ | ||
public class Main { | ||
|
||
private static HdfsClient hdfsClient; | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
hdfsClient = new HdfsClient(); | ||
System.out.println("INFO init hdfsClinet SUCCESS"); | ||
String dir = "/tmp/xxx"; | ||
System.out.println("[INFO] begin exec: hdfs dfs -mkdir -p " + dir); | ||
hdfsClient.mkdirDir(dir); | ||
System.out.println("[INFO] begin exec: hdfs dfs -mkdir -p " + dir + " SUCCESS"); | ||
|
||
} | ||
|
||
|
||
} |