Skip to content

Commit

Permalink
master Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashishkr007 committed Jan 19, 2019
1 parent 037168f commit 347d82a
Show file tree
Hide file tree
Showing 30 changed files with 2,299 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Facebook_Insight_Streaming</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
6 changes: 6 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
14 changes: 14 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
127 changes: 127 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.riveriq.fbinsight</groupId>
<artifactId>Facebook_Insight_Streaming</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FacebookInsightExtract</name>
<description>Facebook Page/ Ad Insight Extract</description>

<developers>
<developer>
<email>[email protected]</email>
<name>Ashish Kumar</name>
<id>niits007</id>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<commons.io.version>2.4</commons.io.version>
<commons.lang.version>3.3.2</commons.lang.version>
<log4j.version>1.2.17</log4j.version>
<org.json.version>20140107</org.json.version>
</properties>
<dependencies>
<dependency>
<groupId>com.restfb</groupId>
<artifactId>restfb</artifactId>
<version>1.6.9</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.10</version>
</dependency>

<!-- JSON.org -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${org.json.version}</version>
</dependency>

<!-- APACHE COMMONS LANG -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang.version}</version>
</dependency>

<!-- APACHE COMMONS IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>

<!-- LOG4J -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

</dependencies>
<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- for fatjar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.riveriq.fbinsight.FBPostInsight</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<timestamp>${timestamp}</timestamp>
</manifestEntries>
</archive>
</configuration>
</plugin>

</plugins>
</build>

</project>
151 changes: 151 additions & 0 deletions src/main/java/org/riveriq/csvwriter/CSVWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@

package org.riveriq.csvwriter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

public class CSVWriter {
/**
* The class logger
*/
private static final Logger LOGGER = Logger.getLogger(CSVWriter.class);

/**
* Convert the given List of String keys-values as a CSV String.
*
* @param flatJson The List of key-value pairs generated from the JSON String
*
* @return The generated CSV string
*/
public static String getCSV(List<Map<String, String>> flatJson) {
// Use the default separator
return getCSV(flatJson, ",");
}

/**
* Convert the given List of String keys-values as a CSV String.
*
* @param flatJson The List of key-value pairs generated from the JSON String
* @param separator The separator can be: ',', ';' or '\t'
*
* @return The generated CSV string
*/
public static String getCSV(List<Map<String, String>> flatJson, String separator) {
Set<String> headers = collectHeaders(flatJson);
String csvString = StringUtils.join(headers.toArray(), separator) + "\n";

for (Map<String, String> map : flatJson) {
csvString = csvString + getSeperatedColumns(headers, map, separator) + "\n";

}

return csvString;
}

/**
* Write the given CSV string to the given file.
*
* @param csvString The csv string to write into the file
* @param fileName The file to write (included the path)
*/
public static void writeToFile(String csvString, String fileName) {
try {
FileUtils.write(new File(fileName), csvString);
} catch (IOException e) {
LOGGER.error("CSVWriter#writeToFile(csvString, fileName) IOException: ", e);
}
}

/**
* Write the given CSV from a flat json to the given file.
*
* @param flatJson
* @param separator
* @param fileName
* @param headers
*/
public static void writeLargeFile(List<Map<String, String>> flatJson, String separator, String fileName,
Set<String> headers) {
String csvString;
csvString = StringUtils.join(headers.toArray(), separator) + "\n";
File file = new File(fileName);

try {
// ISO8859_1 char code to Latin alphabet
FileUtils.write(file, csvString, "ISO8859_1");

for (Map<String, String> map : flatJson) {
csvString = "";
csvString = getSeperatedColumns(headers, map, separator) + "\n";
Files.write(Paths.get(fileName), csvString.getBytes("ISO8859_1"), StandardOpenOption.APPEND);
// Files.write(Paths.get(fileName), csvString.getBytes("UTF-8"),
// StandardOpenOption.APPEND);
}
} catch (IOException e) {
LOGGER.error("CSVWriter#writeLargeFile(flatJson, separator, fileName, headers) IOException: ", e);
}
}

/**
* Get separated comlumns used a separator (comma, semi column, tab).
*
* @param headers The CSV headers
* @param map Map of key-value pairs contains the header and the value
*
* @return a string composed of columns separated by a specific separator.
*/
private static String getSeperatedColumns(Set<String> headers, Map<String, String> map, String separator) {
List<String> items = new ArrayList<String>();
for (String header : headers) {
String value = map.get(header) == null ? "" : map.get(header).replaceAll("[\\,\\;\\r\\n\\t\\s]+", " ");
items.add(value);

}

return StringUtils.join(items.toArray(), separator);
}

/**
* Get the CSV header.
*
* @param flatJson
*
* @return a Set of headers
*/
private static Set<String> collectHeaders(List<Map<String, String>> flatJson) {
Set<String> headers = new LinkedHashSet<String>();

for (Map<String, String> map : flatJson) {
headers.addAll(map.keySet());
}

return headers;
}

/**
* Get the CSV ordered header
*
* @param flatJson
*
* @return a Set of ordered headers
*/
public static Set<String> collectOrderedHeaders(List<Map<String, String>> flatJson) {
Set<String> headers = new TreeSet<String>();
for (Map<String, String> map : flatJson) {
headers.addAll(map.keySet());
}
return headers;
}
}
Loading

0 comments on commit 347d82a

Please sign in to comment.