Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FLINK-1615] [java api] SimpleTweetInputFormat #442

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions flink-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ under the License.
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.contrib.tweetinputformat.io;

import org.apache.flink.api.common.io.DelimitedInputFormat;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.GenericTypeInfo;
import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
import org.apache.flink.contrib.tweetinputformat.model.tweet.Tweet;
import org.apache.flink.core.fs.FileInputSplit;
import org.codehaus.jackson.JsonParseException;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


public class SimpleTweetInputFormat extends DelimitedInputFormat<Tweet> implements ResultTypeQueryable<Tweet> {

private static final Logger LOG = LoggerFactory.getLogger(SimpleTweetInputFormat.class);

private transient JSONParser parser;
private transient TweetHandler handler;

@Override
public void open(FileInputSplit split) throws IOException {
super.open(split);
this.handler = new TweetHandler();
this.parser = new JSONParser();
}

@Override
public Tweet nextRecord(Tweet record) throws IOException {
Boolean result = false;

do {
try {
record.reset(0);
record = super.nextRecord(record);
result = true;

} catch (JsonParseException e) {
result = false;

}
} while (!result);

return record;
}

@Override
public Tweet readRecord(Tweet reuse, byte[] bytes, int offset, int numBytes) throws IOException {

InputStreamReader jsonReader = new InputStreamReader(new ByteArrayInputStream(bytes));
jsonReader.skip(offset);

try {

handler.reuse = reuse;
parser.parse(jsonReader, handler, false);
} catch (ParseException e) {

LOG.debug(" Tweet Parsing Exception : "+e.getMessage() );
}

return reuse;
}

@Override
public TypeInformation<Tweet> getProducedType() {
return new GenericTypeInfo<Tweet>(Tweet.class);
}
}