Skip to content

Commit 35fb37c

Browse files
apurtellApache9
andcommitted
HBASE-27234 Clean up error-prone warnings in hbase-examples
Close #4647 Co-authored-by: Duo Zhang <[email protected]> Signed-off-by: Duo Zhang <[email protected]> Signed-off-by: Viraj Jasani <[email protected]>
1 parent 1004876 commit 35fb37c

File tree

14 files changed

+79
-73
lines changed

14 files changed

+79
-73
lines changed

hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/HttpProxyExample.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import java.io.IOException;
2424
import java.net.InetSocketAddress;
25+
import java.util.Iterator;
26+
import java.util.List;
2527
import java.util.concurrent.ExecutionException;
2628
import org.apache.hadoop.conf.Configuration;
2729
import org.apache.hadoop.hbase.HBaseConfiguration;
@@ -35,6 +37,7 @@
3537
import org.apache.yetus.audience.InterfaceAudience;
3638

3739
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
40+
import org.apache.hbase.thirdparty.com.google.common.base.Splitter;
3841
import org.apache.hbase.thirdparty.com.google.common.base.Throwables;
3942
import org.apache.hbase.thirdparty.io.netty.bootstrap.ServerBootstrap;
4043
import org.apache.hbase.thirdparty.io.netty.buffer.ByteBuf;
@@ -158,12 +161,20 @@ private void write(ChannelHandlerContext ctx, HttpResponseStatus status, String
158161
}
159162

160163
private Params parse(FullHttpRequest req) {
161-
String[] components = new QueryStringDecoder(req.uri()).path().split("/");
162-
Preconditions.checkArgument(components.length == 4, "Unrecognized uri: %s", req.uri());
164+
List<String> components =
165+
Splitter.on('/').splitToList(new QueryStringDecoder(req.uri()).path());
166+
Preconditions.checkArgument(components.size() == 4, "Unrecognized uri: %s", req.uri());
167+
Iterator<String> i = components.iterator();
163168
// path is start with '/' so split will give an empty component
164-
String[] cfAndCq = components[3].split(":");
165-
Preconditions.checkArgument(cfAndCq.length == 2, "Unrecognized uri: %s", req.uri());
166-
return new Params(components[1], components[2], cfAndCq[0], cfAndCq[1]);
169+
i.next();
170+
String table = i.next();
171+
String row = i.next();
172+
List<String> cfAndCq = Splitter.on(':').splitToList(i.next());
173+
Preconditions.checkArgument(cfAndCq.size() == 2, "Unrecognized uri: %s", req.uri());
174+
i = cfAndCq.iterator();
175+
String family = i.next();
176+
String qualifier = i.next();
177+
return new Params(table, row, family, qualifier);
167178
}
168179

169180
private void get(ChannelHandlerContext ctx, FullHttpRequest req) {

hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/MultiThreadedClientExample.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.concurrent.TimeUnit;
3131
import org.apache.hadoop.conf.Configured;
3232
import org.apache.hadoop.hbase.Cell;
33-
import org.apache.hadoop.hbase.Cell.Type;
3433
import org.apache.hadoop.hbase.CellBuilderFactory;
3534
import org.apache.hadoop.hbase.CellBuilderType;
3635
import org.apache.hadoop.hbase.TableName;
@@ -240,7 +239,7 @@ public Boolean call() throws Exception {
240239
byte[] rk = Bytes.toBytes(ThreadLocalRandom.current().nextLong());
241240
Put p = new Put(rk);
242241
p.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(rk).setFamily(FAMILY)
243-
.setQualifier(QUAL).setTimestamp(p.getTimestamp()).setType(Type.Put).setValue(value)
242+
.setQualifier(QUAL).setTimestamp(p.getTimestamp()).setType(Cell.Type.Put).setValue(value)
244243
.build());
245244
t.put(p);
246245
}

hbase-examples/src/main/java/org/apache/hadoop/hbase/coprocessor/example/BulkDeleteEndpoint.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.apache.hadoop.hbase.CellUtil;
2929
import org.apache.hadoop.hbase.CoprocessorEnvironment;
3030
import org.apache.hadoop.hbase.HConstants;
31-
import org.apache.hadoop.hbase.HConstants.OperationStatusCode;
3231
import org.apache.hadoop.hbase.client.Delete;
3332
import org.apache.hadoop.hbase.client.Mutation;
3433
import org.apache.hadoop.hbase.client.Scan;
@@ -52,7 +51,6 @@
5251
import org.apache.hadoop.hbase.shaded.coprocessor.example.generated.BulkDeleteProtos.BulkDeleteRequest;
5352
import org.apache.hadoop.hbase.shaded.coprocessor.example.generated.BulkDeleteProtos.BulkDeleteRequest.DeleteType;
5453
import org.apache.hadoop.hbase.shaded.coprocessor.example.generated.BulkDeleteProtos.BulkDeleteResponse;
55-
import org.apache.hadoop.hbase.shaded.coprocessor.example.generated.BulkDeleteProtos.BulkDeleteResponse.Builder;
5654
import org.apache.hadoop.hbase.shaded.coprocessor.example.generated.BulkDeleteProtos.BulkDeleteService;
5755
import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
5856

@@ -157,7 +155,7 @@ public void delete(RpcController controller, BulkDeleteRequest request,
157155
}
158156
OperationStatus[] opStatus = region.batchMutate(deleteArr);
159157
for (i = 0; i < opStatus.length; i++) {
160-
if (opStatus[i].getOperationStatusCode() != OperationStatusCode.SUCCESS) {
158+
if (opStatus[i].getOperationStatusCode() != HConstants.OperationStatusCode.SUCCESS) {
161159
break;
162160
}
163161
totalRowsDeleted++;
@@ -183,7 +181,7 @@ public void delete(RpcController controller, BulkDeleteRequest request,
183181
}
184182
}
185183
}
186-
Builder responseBuilder = BulkDeleteResponse.newBuilder();
184+
BulkDeleteResponse.Builder responseBuilder = BulkDeleteResponse.newBuilder();
187185
responseBuilder.setRowsDeleted(totalRowsDeleted);
188186
if (deleteType == DeleteType.VERSION) {
189187
responseBuilder.setVersionsDeleted(totalVersionsDeleted);

hbase-examples/src/main/java/org/apache/hadoop/hbase/coprocessor/example/ExampleRegionObserverWithMetrics.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ private void performCostlyOperation() {
109109
// simulate the operation by sleeping.
110110
Thread.sleep(ThreadLocalRandom.current().nextLong(100));
111111
} catch (InterruptedException ignore) {
112+
// Restore the interrupt status
113+
Thread.currentThread().interrupt();
112114
}
113115
}
114116
}

hbase-examples/src/main/java/org/apache/hadoop/hbase/coprocessor/example/RowCountEndpoint.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.Collections;
2323
import java.util.List;
24+
import org.apache.commons.io.IOUtils;
2425
import org.apache.hadoop.hbase.Cell;
2526
import org.apache.hadoop.hbase.CellUtil;
2627
import org.apache.hadoop.hbase.CoprocessorEnvironment;
@@ -34,6 +35,7 @@
3435
import org.apache.hadoop.hbase.util.Bytes;
3536
import org.apache.yetus.audience.InterfaceAudience;
3637

38+
import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
3739
import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback;
3840
import org.apache.hbase.thirdparty.com.google.protobuf.RpcController;
3941
import org.apache.hbase.thirdparty.com.google.protobuf.Service;
@@ -97,10 +99,7 @@ public void getRowCount(RpcController controller, CountRequest request,
9799
CoprocessorRpcUtils.setControllerException(controller, ioe);
98100
} finally {
99101
if (scanner != null) {
100-
try {
101-
scanner.close();
102-
} catch (IOException ignored) {
103-
}
102+
IOUtils.closeQuietly(scanner);
104103
}
105104
}
106105
done.run(response);
@@ -121,21 +120,15 @@ public void getKeyValueCount(RpcController controller, CountRequest request,
121120
long count = 0;
122121
do {
123122
hasMore = scanner.next(results);
124-
for (Cell kv : results) {
125-
count++;
126-
}
123+
count += Iterables.size(results);
127124
results.clear();
128125
} while (hasMore);
129-
130126
response = CountResponse.newBuilder().setCount(count).build();
131127
} catch (IOException ioe) {
132128
CoprocessorRpcUtils.setControllerException(controller, ioe);
133129
} finally {
134130
if (scanner != null) {
135-
try {
136-
scanner.close();
137-
} catch (IOException ignored) {
138-
}
131+
IOUtils.closeQuietly(scanner);
139132
}
140133
}
141134
done.run(response);

hbase-examples/src/main/java/org/apache/hadoop/hbase/mapreduce/IndexBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public static Job configureJob(Configuration conf, String[] args) throws IOExcep
131131
return job;
132132
}
133133

134+
@Override
134135
public int run(String[] args) throws Exception {
135136
Configuration conf = HBaseConfiguration.create(getConf());
136137
if (args.length < 3) {

hbase-examples/src/main/java/org/apache/hadoop/hbase/mapreduce/SampleUploader.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.apache.hadoop.hbase.mapreduce;
1919

2020
import java.io.IOException;
21+
import java.util.Iterator;
22+
import java.util.List;
2123
import org.apache.hadoop.conf.Configuration;
2224
import org.apache.hadoop.conf.Configured;
2325
import org.apache.hadoop.fs.Path;
@@ -37,6 +39,8 @@
3739
import org.slf4j.Logger;
3840
import org.slf4j.LoggerFactory;
3941

42+
import org.apache.hbase.thirdparty.com.google.common.base.Splitter;
43+
4044
/**
4145
* Sample Uploader MapReduce
4246
* <p>
@@ -80,16 +84,16 @@ public void map(LongWritable key, Text line, Context context) throws IOException
8084
// Each line is comma-delimited; row,family,qualifier,value
8185

8286
// Split CSV line
83-
String[] values = line.toString().split(",");
84-
if (values.length != 4) {
87+
List<String> values = Splitter.on(',').splitToList(line.toString());
88+
if (values.size() != 4) {
8589
return;
8690
}
87-
91+
Iterator<String> i = values.iterator();
8892
// Extract each value
89-
byte[] row = Bytes.toBytes(values[0]);
90-
byte[] family = Bytes.toBytes(values[1]);
91-
byte[] qualifier = Bytes.toBytes(values[2]);
92-
byte[] value = Bytes.toBytes(values[3]);
93+
byte[] row = Bytes.toBytes(i.next());
94+
byte[] family = Bytes.toBytes(i.next());
95+
byte[] qualifier = Bytes.toBytes(i.next());
96+
byte[] value = Bytes.toBytes(i.next());
9397

9498
// Create Put
9599
Put put = new Put(row);
@@ -136,6 +140,7 @@ public static Job configureJob(Configuration conf, String[] args) throws IOExcep
136140
* @param otherArgs The command line parameters after ToolRunner handles standard.
137141
* @throws Exception When running the job fails.
138142
*/
143+
@Override
139144
public int run(String[] otherArgs) throws Exception {
140145
if (otherArgs.length != 2) {
141146
System.err.println("Wrong number of arguments: " + otherArgs.length);

hbase-examples/src/main/java/org/apache/hadoop/hbase/security/provider/example/ShadeSaslServerAuthenticationProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.BufferedReader;
2121
import java.io.IOException;
2222
import java.io.InputStreamReader;
23+
import java.nio.charset.StandardCharsets;
2324
import java.util.Arrays;
2425
import java.util.HashMap;
2526
import java.util.Map;
@@ -86,8 +87,8 @@ Map<String, char[]> readPasswordDB(Configuration conf) throws IOException {
8687
}
8788

8889
Map<String, char[]> passwordDb = new HashMap<>();
89-
try (FSDataInputStream fdis = fs.open(passwordFile);
90-
BufferedReader reader = new BufferedReader(new InputStreamReader(fdis))) {
90+
try (FSDataInputStream fdis = fs.open(passwordFile); BufferedReader reader =
91+
new BufferedReader(new InputStreamReader(fdis, StandardCharsets.UTF_8))) {
9192
String line = null;
9293
int offset = 0;
9394
while ((line = reader.readLine()) != null) {

hbase-examples/src/main/java/org/apache/hadoop/hbase/thrift/DemoClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@
4141
import org.apache.thrift.transport.TSocket;
4242
import org.apache.thrift.transport.TTransport;
4343
import org.apache.yetus.audience.InterfaceAudience;
44-
import org.slf4j.Logger;
45-
import org.slf4j.LoggerFactory;
4644

4745
/**
4846
* See the instructions under hbase-examples/README.txt
4947
*/
5048
@InterfaceAudience.Private
5149
public class DemoClient {
52-
private static final Logger LOG = LoggerFactory.getLogger(DemoClient.class);
5350

5451
static protected int port;
5552
static protected String host;
@@ -128,15 +125,15 @@ private void run() throws Exception {
128125
System.out.println("scanning tables...");
129126

130127
for (ByteBuffer name : client.getTableNames()) {
131-
System.out.println(" found: " + ClientUtils.utf8(name.array()));
128+
System.out.println(" found: " + ClientUtils.utf8(name));
132129

133130
if (name.equals(demoTable) || name.equals(disabledTable)) {
134131
if (client.isTableEnabled(name)) {
135-
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
132+
System.out.println(" disabling table: " + ClientUtils.utf8(name));
136133
client.disableTable(name);
137134
}
138135

139-
System.out.println(" deleting table: " + ClientUtils.utf8(name.array()));
136+
System.out.println(" deleting table: " + ClientUtils.utf8(name));
140137
client.deleteTable(name);
141138
}
142139
}
@@ -324,7 +321,7 @@ private void run() throws Exception {
324321
columnNames.clear();
325322

326323
for (ColumnDescriptor col2 : client.getColumnDescriptors(demoTable).values()) {
327-
System.out.println("column with name: " + new String(col2.name.array()));
324+
System.out.println("column with name: " + ClientUtils.utf8(col2.name));
328325
System.out.println(col2.toString());
329326

330327
columnNames.add(col2.name);
@@ -356,7 +353,7 @@ private void printVersions(ByteBuffer row, List<TCell> versions) {
356353
rowStr.append("; ");
357354
}
358355

359-
System.out.println("row: " + ClientUtils.utf8(row.array()) + ", values: " + rowStr);
356+
System.out.println("row: " + ClientUtils.utf8(row) + ", values: " + rowStr);
360357
}
361358

362359
private void printRow(TRowResult rowResult) {

hbase-examples/src/main/java/org/apache/hadoop/hbase/thrift/HttpDoAsClient.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.Base64;
2626
import java.util.HashMap;
2727
import java.util.HashSet;
28-
import java.util.List;
2928
import java.util.Map;
3029
import java.util.Set;
3130
import javax.security.auth.Subject;
@@ -36,8 +35,6 @@
3635
import org.apache.hadoop.hbase.thrift.generated.AlreadyExists;
3736
import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor;
3837
import org.apache.hadoop.hbase.thrift.generated.Hbase;
39-
import org.apache.hadoop.hbase.thrift.generated.TCell;
40-
import org.apache.hadoop.hbase.thrift.generated.TRowResult;
4138
import org.apache.hadoop.hbase.util.Bytes;
4239
import org.apache.hadoop.hbase.util.ClientUtils;
4340
import org.apache.thrift.protocol.TBinaryProtocol;
@@ -129,13 +126,13 @@ private void run() throws Exception {
129126
//
130127
System.out.println("scanning tables...");
131128
for (ByteBuffer name : refresh(client, httpClient).getTableNames()) {
132-
System.out.println(" found: " + ClientUtils.utf8(name.array()));
133-
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t))) {
129+
System.out.println(" found: " + ClientUtils.utf8(name));
130+
if (ClientUtils.utf8(name).equals(ClientUtils.utf8(t))) {
134131
if (refresh(client, httpClient).isTableEnabled(name)) {
135-
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
132+
System.out.println(" disabling table: " + ClientUtils.utf8(name));
136133
refresh(client, httpClient).disableTable(name);
137134
}
138-
System.out.println(" deleting table: " + ClientUtils.utf8(name.array()));
135+
System.out.println(" deleting table: " + ClientUtils.utf8(name));
139136
refresh(client, httpClient).deleteTable(name);
140137
}
141138
}
@@ -167,8 +164,8 @@ private void run() throws Exception {
167164
Map<ByteBuffer, ColumnDescriptor> columnMap =
168165
refresh(client, httpClient).getColumnDescriptors(ByteBuffer.wrap(t));
169166
for (ColumnDescriptor col2 : columnMap.values()) {
170-
System.out.println(
171-
" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: " + col2.maxVersions);
167+
System.out
168+
.println(" column: " + ClientUtils.utf8(col2.name) + ", maxVer: " + col2.maxVersions);
172169
}
173170

174171
transport.close();
@@ -205,26 +202,13 @@ private String generateTicket() throws GSSException {
205202
context.requestInteg(true);
206203

207204
final byte[] outToken = context.initSecContext(new byte[0], 0, 0);
208-
StringBuffer outputBuffer = new StringBuffer();
205+
StringBuilder outputBuffer = new StringBuilder();
209206
outputBuffer.append("Negotiate ");
210207
outputBuffer.append(Bytes.toString(Base64.getEncoder().encode(outToken)));
211208
System.out.print("Ticket is: " + outputBuffer);
212209
return outputBuffer.toString();
213210
}
214211

215-
private void printVersions(ByteBuffer row, List<TCell> versions) {
216-
StringBuilder rowStr = new StringBuilder();
217-
for (TCell cell : versions) {
218-
rowStr.append(ClientUtils.utf8(cell.value.array()));
219-
rowStr.append("; ");
220-
}
221-
System.out.println("row: " + ClientUtils.utf8(row.array()) + ", values: " + rowStr);
222-
}
223-
224-
private void printRow(TRowResult rowResult) {
225-
ClientUtils.printRow(rowResult);
226-
}
227-
228212
static Subject getSubject() throws Exception {
229213
if (!secure) {
230214
return new Subject();

0 commit comments

Comments
 (0)