This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from wso2-dev/master
adding avro datam write pattern to format the output data
- Loading branch information
Showing
6 changed files
with
353 additions
and
9 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...atamapper.engine/src/main/java/org/wso2/datamapper/engine/core/writer/CSVDatumWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2005,2014 WSO2, Inc. http://www.wso2.org | ||
* | ||
* Licensed 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.wso2.datamapper.engine.core.writer; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericDatumWriter; | ||
import org.apache.avro.io.Encoder; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.json.CDL; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
|
||
|
||
public class CSVDatumWriter extends GenericDatumWriter<GenericRecord> { | ||
|
||
@Override | ||
protected void writeArray(Schema schema, Object datum, Encoder out) | ||
throws IOException { | ||
try { | ||
JSONArray jsonArray = new JSONArray(datum.toString()); | ||
out.writeString(CDL.toString(jsonArray)); | ||
} catch (JSONException e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
} |
146 changes: 146 additions & 0 deletions
146
....datamapper.engine/src/main/java/org/wso2/datamapper/engine/core/writer/DummyEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2005,2014 WSO2, Inc. http://www.wso2.org | ||
* | ||
* Licensed 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.wso2.datamapper.engine.core.writer; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
|
||
import org.apache.avro.io.Encoder; | ||
import org.apache.avro.util.Utf8; | ||
|
||
public class DummyEncoder extends Encoder { | ||
|
||
OutputStream outputStream ; | ||
|
||
public DummyEncoder(OutputStream outputStream) { | ||
this.outputStream = outputStream; | ||
} | ||
|
||
public void flush() throws IOException { | ||
outputStream.flush(); | ||
|
||
} | ||
|
||
@Override | ||
public void setItemCount(long arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void startItem() throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeArrayEnd() throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeArrayStart() throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeBoolean(boolean arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeBytes(ByteBuffer arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeBytes(byte[] arg0, int arg1, int arg2) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeDouble(double arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeEnum(int arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeFixed(byte[] arg0, int arg1, int arg2) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeFloat(float arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeIndex(int arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeInt(int arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeLong(long arg0) throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeMapEnd() throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeMapStart() throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeNull() throws IOException { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void writeString(Utf8 data) throws IOException { | ||
outputStream.write(data.toString().getBytes()); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...atamapper.engine/src/main/java/org/wso2/datamapper/engine/core/writer/WriterRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2005,2014 WSO2, Inc. http://www.wso2.org | ||
* | ||
* Licensed 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.wso2.datamapper.engine.core.writer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.avro.generic.GenericRecord; | ||
import org.apache.avro.io.DatumWriter; | ||
|
||
/** | ||
* A registry of writer. | ||
*/ | ||
public class WriterRegistry { | ||
|
||
/** | ||
* Singleton instance. | ||
*/ | ||
private static WriterRegistry singleton; | ||
|
||
|
||
/** | ||
* writer map. | ||
*/ | ||
private Map<String, Class<? extends DatumWriter<GenericRecord>>> writerMap; | ||
|
||
/** | ||
* | ||
*/ | ||
private WriterRegistry() { | ||
writerMap = new HashMap<String, Class<? extends DatumWriter<GenericRecord>>>(); | ||
|
||
// FIXME : use java service provider interface rather than hard-coding class names/ importing classes | ||
writerMap.put("text/csv",CSVDatumWriter.class); | ||
writerMap.put("application/xml", XMLDatumWriter.class); | ||
} | ||
|
||
/** | ||
* @return singleton instance. | ||
*/ | ||
public static WriterRegistry getInstance() { | ||
if (null == singleton) { | ||
singleton = new WriterRegistry(); | ||
} | ||
return singleton; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Class<DatumWriter<GenericRecord>> get(String mediaType){ | ||
Class<DatumWriter<GenericRecord>> writer = null; | ||
if(writerMap.containsKey(mediaType)){ | ||
writer = (Class<DatumWriter<GenericRecord>>) writerMap.get(mediaType); | ||
} else { | ||
throw new RuntimeException("No writer found for " + mediaType); | ||
} | ||
//FIXME: use proper error handling | ||
return writer; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...atamapper.engine/src/main/java/org/wso2/datamapper/engine/core/writer/XMLDatumWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2005,2014 WSO2, Inc. http://www.wso2.org | ||
* | ||
* Licensed 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.wso2.datamapper.engine.core.writer; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericDatumWriter; | ||
import org.apache.avro.io.Encoder; | ||
import org.apache.avro.generic.GenericRecord; | ||
import org.json.XML; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
public class XMLDatumWriter extends GenericDatumWriter<GenericRecord> { | ||
|
||
|
||
@Override | ||
protected void writeRecord(Schema schema, Object datum, Encoder out) | ||
throws IOException { | ||
try { | ||
GenericRecord record = (GenericRecord) datum; | ||
String name = record.getSchema().getName(); | ||
JSONObject rootObj = new JSONObject(); | ||
rootObj.putOnce(name, new JSONObject(record.toString())); | ||
out.writeString(XML.toString(rootObj)); | ||
} catch (JSONException e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.