18
18
package org .apache .hadoop .hbase .mapreduce ;
19
19
20
20
import java .io .IOException ;
21
+ import java .net .URI ;
22
+ import java .net .URISyntaxException ;
21
23
import java .util .Collections ;
22
24
import java .util .Iterator ;
25
+ import org .apache .commons .lang3 .StringUtils ;
23
26
import org .apache .hadoop .conf .Configuration ;
24
27
import org .apache .hadoop .conf .Configured ;
25
28
import org .apache .hadoop .fs .FileStatus ;
@@ -65,7 +68,17 @@ public class SyncTable extends Configured implements Tool {
65
68
static final String SOURCE_HASH_DIR_CONF_KEY = "sync.table.source.hash.dir" ;
66
69
static final String SOURCE_TABLE_CONF_KEY = "sync.table.source.table.name" ;
67
70
static final String TARGET_TABLE_CONF_KEY = "sync.table.target.table.name" ;
71
+ static final String SOURCE_URI_CONF_KEY = "sync.table.source.uri" ;
72
+ /**
73
+ * @deprecated Since 3.0.0, will be removed in 4.0.0 Use {@link #SOURCE_URI_CONF_KEY} instead.
74
+ */
75
+ @ Deprecated
68
76
static final String SOURCE_ZK_CLUSTER_CONF_KEY = "sync.table.source.zk.cluster" ;
77
+ static final String TARGET_URI_CONF_KEY = "sync.table.target.uri" ;
78
+ /**
79
+ * @deprecated Since 3.0.0, will be removed in 4.0.0 Use {@link #TARGET_URI_CONF_KEY} instead.
80
+ */
81
+ @ Deprecated
69
82
static final String TARGET_ZK_CLUSTER_CONF_KEY = "sync.table.target.zk.cluster" ;
70
83
static final String DRY_RUN_CONF_KEY = "sync.table.dry.run" ;
71
84
static final String DO_DELETES_CONF_KEY = "sync.table.do.deletes" ;
@@ -76,7 +89,17 @@ public class SyncTable extends Configured implements Tool {
76
89
String sourceTableName ;
77
90
String targetTableName ;
78
91
92
+ URI sourceUri ;
93
+ /**
94
+ * @deprecated Since 3.0.0, will be removed in 4.0.0 Use {@link #sourceUri} instead.
95
+ */
96
+ @ Deprecated
79
97
String sourceZkCluster ;
98
+ URI targetUri ;
99
+ /**
100
+ * @deprecated Since 3.0.0, will be removed in 4.0.0 Use {@link #targetUri} instead.
101
+ */
102
+ @ Deprecated
80
103
String targetZkCluster ;
81
104
boolean dryRun ;
82
105
boolean doDeletes = true ;
@@ -89,9 +112,9 @@ public SyncTable(Configuration conf) {
89
112
super (conf );
90
113
}
91
114
92
- private void initCredentialsForHBase (String zookeeper , Job job ) throws IOException {
115
+ private void initCredentialsForHBase (String clusterKey , Job job ) throws IOException {
93
116
Configuration peerConf =
94
- HBaseConfiguration .createClusterConf (job .getConfiguration (), zookeeper );
117
+ HBaseConfiguration .createClusterConf (job .getConfiguration (), clusterKey );
95
118
TableMapReduceUtil .initCredentialsForCluster (job , peerConf );
96
119
}
97
120
@@ -142,11 +165,17 @@ public Job createSubmittableJob(String[] args) throws IOException {
142
165
jobConf .set (SOURCE_HASH_DIR_CONF_KEY , sourceHashDir .toString ());
143
166
jobConf .set (SOURCE_TABLE_CONF_KEY , sourceTableName );
144
167
jobConf .set (TARGET_TABLE_CONF_KEY , targetTableName );
145
- if (sourceZkCluster != null ) {
168
+ if (sourceUri != null ) {
169
+ jobConf .set (SOURCE_URI_CONF_KEY , sourceUri .toString ());
170
+ TableMapReduceUtil .initCredentialsForCluster (job , jobConf , sourceUri );
171
+ } else if (sourceZkCluster != null ) {
146
172
jobConf .set (SOURCE_ZK_CLUSTER_CONF_KEY , sourceZkCluster );
147
173
initCredentialsForHBase (sourceZkCluster , job );
148
174
}
149
- if (targetZkCluster != null ) {
175
+ if (targetUri != null ) {
176
+ jobConf .set (TARGET_URI_CONF_KEY , targetUri .toString ());
177
+ TableMapReduceUtil .initCredentialsForCluster (job , jobConf , targetUri );
178
+ } else if (targetZkCluster != null ) {
150
179
jobConf .set (TARGET_ZK_CLUSTER_CONF_KEY , targetZkCluster );
151
180
initCredentialsForHBase (targetZkCluster , job );
152
181
}
@@ -165,8 +194,11 @@ public Job createSubmittableJob(String[] args) throws IOException {
165
194
} else {
166
195
// No reducers. Just write straight to table. Call initTableReducerJob
167
196
// because it sets up the TableOutputFormat.
168
- TableMapReduceUtil .initTableReducerJob (targetTableName , null , job , null , targetZkCluster );
169
-
197
+ if (targetUri != null ) {
198
+ TableMapReduceUtil .initTableReducerJob (targetTableName , null , job , null , targetUri );
199
+ } else {
200
+ TableMapReduceUtil .initTableReducerJob (targetTableName , null , job , null , targetZkCluster );
201
+ }
170
202
// would be nice to add an option for bulk load instead
171
203
}
172
204
@@ -214,9 +246,10 @@ public static enum Counter {
214
246
protected void setup (Context context ) throws IOException {
215
247
Configuration conf = context .getConfiguration ();
216
248
sourceHashDir = new Path (conf .get (SOURCE_HASH_DIR_CONF_KEY ));
217
- sourceConnection = openConnection (conf , SOURCE_ZK_CLUSTER_CONF_KEY , null );
218
- targetConnection =
219
- openConnection (conf , TARGET_ZK_CLUSTER_CONF_KEY , TableOutputFormat .OUTPUT_CONF_PREFIX );
249
+ sourceConnection =
250
+ openConnection (conf , SOURCE_URI_CONF_KEY , SOURCE_ZK_CLUSTER_CONF_KEY , null );
251
+ targetConnection = openConnection (conf , TARGET_URI_CONF_KEY , TARGET_ZK_CLUSTER_CONF_KEY ,
252
+ TableOutputFormat .OUTPUT_CONF_PREFIX );
220
253
sourceTable = openTable (sourceConnection , conf , SOURCE_TABLE_CONF_KEY );
221
254
targetTable = openTable (targetConnection , conf , TARGET_TABLE_CONF_KEY );
222
255
dryRun = conf .getBoolean (DRY_RUN_CONF_KEY , false );
@@ -241,12 +274,21 @@ protected void setup(Context context) throws IOException {
241
274
targetHasher .ignoreTimestamps = ignoreTimestamp ;
242
275
}
243
276
244
- private static Connection openConnection (Configuration conf , String zkClusterConfKey ,
245
- String configPrefix ) throws IOException {
246
- String zkCluster = conf .get (zkClusterConfKey );
247
- Configuration clusterConf =
248
- HBaseConfiguration .createClusterConf (conf , zkCluster , configPrefix );
249
- return ConnectionFactory .createConnection (clusterConf );
277
+ private static Connection openConnection (Configuration conf , String uriConfKey ,
278
+ String zkClusterConfKey , String configPrefix ) throws IOException {
279
+ String uri = conf .get (uriConfKey );
280
+ if (!StringUtils .isBlank (uri )) {
281
+ try {
282
+ return ConnectionFactory .createConnection (new URI (uri ), conf );
283
+ } catch (URISyntaxException e ) {
284
+ throw new IOException (e );
285
+ }
286
+ } else {
287
+ String zkCluster = conf .get (zkClusterConfKey );
288
+ Configuration clusterConf =
289
+ HBaseConfiguration .createClusterConf (conf , zkCluster , configPrefix );
290
+ return ConnectionFactory .createConnection (clusterConf );
291
+ }
250
292
}
251
293
252
294
private static Table openTable (Connection connection , Configuration conf ,
@@ -747,10 +789,18 @@ private static void printUsage(final String errorMsg) {
747
789
System .err .println ();
748
790
System .err .println ("Options:" );
749
791
792
+ System .err .println (" sourceuri Cluster connection uri of the source table" );
793
+ System .err .println (" (defaults to cluster in classpath's config)" );
750
794
System .err .println (" sourcezkcluster ZK cluster key of the source table" );
751
795
System .err .println (" (defaults to cluster in classpath's config)" );
796
+ System .err .println (" Do not take effect if sourceuri is specified" );
797
+ System .err .println (" Deprecated, please use sourceuri instead" );
798
+ System .err .println (" targeturi Cluster connection uri of the target table" );
799
+ System .err .println (" (defaults to cluster in classpath's config)" );
752
800
System .err .println (" targetzkcluster ZK cluster key of the target table" );
753
801
System .err .println (" (defaults to cluster in classpath's config)" );
802
+ System .err .println (" Do not take effect if targeturi is specified" );
803
+ System .err .println (" Deprecated, please use targeturi instead" );
754
804
System .err .println (" dryrun if true, output counters but no writes" );
755
805
System .err .println (" (defaults to false)" );
756
806
System .err .println (" doDeletes if false, does not perform deletes" );
@@ -792,13 +842,24 @@ private boolean doCommandLine(final String[] args) {
792
842
printUsage (null );
793
843
return false ;
794
844
}
845
+ final String sourceUriKey = "--sourceuri=" ;
846
+ if (cmd .startsWith (sourceUriKey )) {
847
+ sourceUri = new URI (cmd .substring (sourceUriKey .length ()));
848
+ continue ;
849
+ }
795
850
796
851
final String sourceZkClusterKey = "--sourcezkcluster=" ;
797
852
if (cmd .startsWith (sourceZkClusterKey )) {
798
853
sourceZkCluster = cmd .substring (sourceZkClusterKey .length ());
799
854
continue ;
800
855
}
801
856
857
+ final String targetUriKey = "--targeturi=" ;
858
+ if (cmd .startsWith (targetUriKey )) {
859
+ targetUri = new URI (cmd .substring (targetUriKey .length ()));
860
+ continue ;
861
+ }
862
+
802
863
final String targetZkClusterKey = "--targetzkcluster=" ;
803
864
if (cmd .startsWith (targetZkClusterKey )) {
804
865
targetZkCluster = cmd .substring (targetZkClusterKey .length ());
0 commit comments