Skip to content

Commit fd724f0

Browse files
cwenseljbaiera
authored andcommitted
Fix Cascading Local Mode NoClassDefFoundError when es.input.json=true (#937)
When using Cascading local mode any attempt to load JSON data with es.input.json=true fails with a NoClassDefFoundError attempting to load non-existent Hadoop classes.
1 parent 45696cc commit fd724f0

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.hadoop.mr;
21+
22+
import org.apache.hadoop.io.BytesWritable;
23+
import org.apache.hadoop.io.Text;
24+
import org.elasticsearch.hadoop.util.BytesArray;
25+
26+
/**
27+
*
28+
*/
29+
class SafeWritableConverter {
30+
public SafeWritableConverter() {
31+
Text.class.getName(); // force class to be loaded
32+
}
33+
34+
public void invoke(Object from, BytesArray to) {
35+
// handle common cases
36+
if (from instanceof Text) {
37+
Text t = (Text) from;
38+
to.bytes(t.getBytes(), t.getLength());
39+
}
40+
if (from instanceof BytesWritable) {
41+
BytesWritable b = (BytesWritable) from;
42+
to.bytes(b.getBytes(), b.getLength());
43+
}
44+
}
45+
}

mr/src/main/java/org/elasticsearch/hadoop/mr/WritableBytesConverter.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@
1818
*/
1919
package org.elasticsearch.hadoop.mr;
2020

21-
import org.apache.hadoop.io.BytesWritable;
22-
import org.apache.hadoop.io.Text;
2321
import org.elasticsearch.hadoop.serialization.JdkBytesConverter;
2422
import org.elasticsearch.hadoop.util.BytesArray;
2523

2624
public class WritableBytesConverter extends JdkBytesConverter {
2725

26+
private static SafeWritableConverter safeWritableConverter;
27+
28+
static {
29+
try {
30+
safeWritableConverter = new SafeWritableConverter();
31+
} catch (Error e) {
32+
// no Hadoop libs loaded
33+
}
34+
}
35+
2836
@Override
2937
public void convert(Object from, BytesArray to) {
30-
// handle common cases
31-
if (from instanceof Text) {
32-
Text t = (Text) from;
33-
to.bytes(t.getBytes(), t.getLength());
34-
return;
35-
}
36-
if (from instanceof BytesWritable) {
37-
BytesWritable b = (BytesWritable) from;
38-
to.bytes(b.getBytes(), b.getLength());
39-
return;
40-
}
38+
39+
if (safeWritableConverter != null)
40+
safeWritableConverter.invoke(from, to);
4141

4242
super.convert(from, to);
4343
}

0 commit comments

Comments
 (0)