18
18
19
19
package org .apache .hadoop .hive .ql .session ;
20
20
21
- import java .util .HashMap ;
22
21
import java .util .Map ;
23
22
import java .util .Optional ;
23
+
24
+ import com .google .common .collect .Maps ;
24
25
import org .apache .hadoop .conf .Configuration ;
25
26
import org .apache .hadoop .hive .conf .HiveConf ;
26
27
import org .apache .hadoop .hive .ql .QueryState ;
27
- import org .slf4j .Logger ;
28
- import org .slf4j .LoggerFactory ;
29
28
30
29
public class SessionStateUtil {
31
30
32
- private static final Logger LOG = LoggerFactory .getLogger (SessionStateUtil .class );
33
31
private static final String COMMIT_INFO_PREFIX = "COMMIT_INFO." ;
34
32
public static final String DEFAULT_TABLE_LOCATION = "defaultLocation" ;
35
33
36
34
private SessionStateUtil () {
37
-
38
35
}
39
36
40
37
/**
@@ -44,7 +41,8 @@ private SessionStateUtil() {
44
41
* could not be found
45
42
*/
46
43
public static Optional <Object > getResource (Configuration conf , String key ) {
47
- return getQueryState (conf ).map (state -> state .getResource (key ));
44
+ return getQueryState (conf )
45
+ .map (queryState -> queryState .getResource (key ));
48
46
}
49
47
50
48
/**
@@ -54,29 +52,24 @@ public static Optional<Object> getResource(Configuration conf, String key) {
54
52
* resource itself could not be found, or the resource is not of type String
55
53
*/
56
54
public static Optional <String > getProperty (Configuration conf , String key ) {
57
- return getResource (conf , key ).filter (o -> o instanceof String ).map (o -> (String ) o );
55
+ return getResource (conf , key ).filter (obj -> obj instanceof String )
56
+ .map (obj -> (String ) obj );
58
57
}
59
58
60
59
/**
61
60
* @param conf Configuration object used for getting the query state, should contain the query id
62
61
* @param key The resource identifier
63
62
* @param resource The resource to save into the QueryState
64
- * @return whether operation succeeded
65
63
*/
66
- public static boolean addResource (Configuration conf , String key , Object resource ) {
67
- Optional <QueryState > queryState = getQueryState (conf );
68
- if (queryState .isPresent ()) {
69
- queryState .get ().addResource (key , resource );
70
- return true ;
71
- } else {
72
- return false ;
73
- }
64
+ public static void addResource (Configuration conf , String key , Object resource ) {
65
+ getQueryState (conf )
66
+ .ifPresent (queryState -> queryState .addResource (key , resource ));
74
67
}
75
68
76
69
public static void addResourceOrThrow (Configuration conf , String key , Object resource ) {
77
70
getQueryState (conf )
78
- .orElseThrow (() -> new IllegalStateException ("Query state is missing; failed to add resource for " + key ))
79
- .addResource (key , resource );
71
+ .orElseThrow (() -> new IllegalStateException ("Query state is missing; failed to add resource for " + key ))
72
+ .addResource (key , resource );
80
73
}
81
74
82
75
/**
@@ -85,7 +78,8 @@ public static void addResourceOrThrow(Configuration conf, String key, Object res
85
78
* @return the CommitInfo map. Key: jobId, Value: {@link CommitInfo}, or empty Optional if not present
86
79
*/
87
80
public static Optional <Map <String , CommitInfo >> getCommitInfo (Configuration conf , String tableName ) {
88
- return getResource (conf , COMMIT_INFO_PREFIX + tableName ).map (o -> (Map <String , CommitInfo >)o );
81
+ return getResource (conf , COMMIT_INFO_PREFIX + tableName )
82
+ .map (obj -> (Map <String , CommitInfo >) obj );
89
83
}
90
84
91
85
/**
@@ -94,30 +88,28 @@ public static Optional<Map<String, CommitInfo>> getCommitInfo(Configuration conf
94
88
* @param jobId The job ID
95
89
* @param taskNum The number of successful tasks for the job
96
90
* @param additionalProps Any additional properties related to the job commit
97
- * @return whether the operation succeeded
98
91
*/
99
- public static boolean addCommitInfo (Configuration conf , String tableName , String jobId , int taskNum ,
100
- Map <String , String > additionalProps ) {
92
+ public static void addCommitInfo (
93
+ Configuration conf , String tableName , String jobId , int taskNum , Map <String , String > additionalProps ) {
101
94
102
95
CommitInfo commitInfo = new CommitInfo ()
103
- .withJobID (jobId )
104
- .withTaskNum (taskNum )
105
- .withProps (additionalProps );
106
-
107
- Optional <Map <String , CommitInfo >> commitInfoMap = getCommitInfo (conf , tableName );
108
- if (commitInfoMap .isPresent ()) {
109
- commitInfoMap .get ().put (jobId , commitInfo );
110
- return true ;
111
- }
112
-
113
- Map <String , CommitInfo > newCommitInfoMap = new HashMap <>();
114
- newCommitInfoMap .put (jobId , commitInfo );
115
-
116
- return addResource (conf , COMMIT_INFO_PREFIX + tableName , newCommitInfoMap );
96
+ .withJobID (jobId )
97
+ .withTaskNum (taskNum )
98
+ .withProps (additionalProps );
99
+
100
+ getCommitInfo (conf , tableName )
101
+ .ifPresentOrElse (commitInfoMap -> commitInfoMap .put (jobId , commitInfo ),
102
+ () -> {
103
+ Map <String , CommitInfo > newCommitInfoMap = Maps .newHashMap ();
104
+ newCommitInfoMap .put (jobId , commitInfo );
105
+
106
+ addResource (conf , COMMIT_INFO_PREFIX + tableName , newCommitInfoMap );
107
+ });
117
108
}
118
109
119
110
public static Optional <QueryState > getQueryState (Configuration conf ) {
120
- return Optional .ofNullable (SessionState .get ()).map (ss -> ss .getQueryState (HiveConf .getQueryId (conf )));
111
+ return Optional .ofNullable (SessionState .get ())
112
+ .map (ss -> ss .getQueryState (HiveConf .getQueryId (conf )));
121
113
}
122
114
123
115
/**
0 commit comments