Index: other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/PerVolumeCatalogLangStreamFlatmap.java
===================================================================
--- other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/PerVolumeCatalogLangStreamFlatmap.java	(revision 31294)
+++ other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/PerVolumeCatalogLangStreamFlatmap.java	(revision 31294)
@@ -0,0 +1,88 @@
+package org.hathitrust.extractedfeatures;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import org.apache.spark.api.java.function.FlatMapFunction;
+import org.apache.spark.util.DoubleAccumulator;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+class PerVolumeCatalogLangStreamFlatmap implements FlatMapFunction<String, String>
+{
+	private static final long serialVersionUID = 1L;
+	
+	protected String _input_dir;
+	protected int    _verbosity;
+	
+	protected DoubleAccumulator _progress_accum;
+	protected double            _progress_step;
+	
+	boolean _strict_file_io;
+	
+	public PerVolumeCatalogLangStreamFlatmap(String input_dir, int verbosity, 
+					 		  DoubleAccumulator progress_accum, double progress_step,
+					 		  boolean strict_file_io)
+	{
+		_input_dir  = input_dir;
+		_verbosity  = verbosity;
+		
+		_progress_accum = progress_accum;
+		_progress_step  = progress_step;
+		
+		_strict_file_io = strict_file_io;
+	}
+	
+	public Iterator<String> call(String json_file_in) throws IOException
+	{ 
+	     
+		String full_json_file_in = _input_dir + "/" + json_file_in;
+		JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(full_json_file_in);
+		
+		ArrayList<String> catalog_lang_list = new ArrayList<String>();
+		
+		if (extracted_feature_record != null) {
+			String volume_id = extracted_feature_record.getString("id");
+
+			JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
+
+			if (_verbosity >= 1) {
+				System.out.println("Processing: " + json_file_in);
+			}
+
+			if (ef_metadata != null) {
+				String ef_catalog_language = ef_metadata.getString("language");
+				if (!ef_catalog_language.equals("")) {
+
+					catalog_lang_list.add(ef_catalog_language);
+				}
+				else {
+					System.err.println("No catalog 'language' metadata => Skipping id: " + volume_id);
+				}
+			}
+			else {
+				System.err.println("No 'metadata' section in JSON file => Skipping id: " + volume_id);
+			}
+			
+		}
+		else {
+			// File did not exist, or could not be parsed
+			String mess = "Failed to read in bzipped JSON file '" + full_json_file_in + "'";
+			if (_strict_file_io) {
+				throw new IOException(mess);
+			}
+			else {
+				System.err.println("Warning: " + mess);
+				System.out.println("Warning: " + mess);
+			}
+		}
+
+		_progress_accum.add(_progress_step);
+		
+		return catalog_lang_list.iterator();
+	}
+	
+	
+}
+
Index: other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/ProcessForCatalogLangCount.java
===================================================================
--- other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/ProcessForCatalogLangCount.java	(revision 31294)
+++ other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/ProcessForCatalogLangCount.java	(revision 31294)
@@ -0,0 +1,184 @@
+package org.hathitrust.extractedfeatures;
+
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.Serializable;
+import org.apache.commons.cli.*;
+
+import org.apache.spark.api.java.*;
+import org.apache.spark.api.java.function.Function2;
+import org.apache.spark.api.java.function.PairFunction;
+import org.apache.spark.storage.StorageLevel;
+import org.apache.spark.util.DoubleAccumulator;
+import scala.Tuple2;
+
+import org.apache.spark.SparkConf;
+
+public class ProcessForCatalogLangCount implements Serializable
+{
+	private static final long serialVersionUID = 1L;
+
+	protected static final int DEFAULT_FILES_PER_PARTITION = 3000;
+	
+	protected String _input_dir;
+	protected String _json_list_filename;
+	
+	protected int    _verbosity;
+
+	public ProcessForCatalogLangCount(String input_dir, String json_list_filename, int verbosity)
+	{
+		_input_dir = input_dir;
+		_json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
+
+		_verbosity  = verbosity;
+	}
+
+	protected String generateSparkAppName(String exec_mode)
+	{
+		String spark_app_name = "[" + exec_mode + "] Extracted Features: Process for Catalog Language Labels";
+		spark_app_name += " [" + _json_list_filename + "]";
+	
+		return spark_app_name;
+	}
+		
+	public void execCatalogLangCount()
+	{	
+		String spark_app_name = generateSparkAppName("Per Volume");		
+		
+		SparkConf conf = new SparkConf().setAppName(spark_app_name);
+		JavaSparkContext jsc = new JavaSparkContext(conf);
+
+		String filename_root = _json_list_filename.replaceAll(".*/","").replaceAll("\\..*$","");
+		String output_directory = "catalog-lang-" + filename_root + "-out";
+		if (ClusterFileIO.exists(output_directory))
+		{
+			System.err.println("Error: " + output_directory + " already exists.  Spark unable to write output data");
+			jsc.close();
+			System.exit(1);
+		}
+		
+		//int num_partitions = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_NUM_PARTITIONS);
+		int files_per_partition = Integer.getInteger("wcsa-ef-ingest.files-per-partition", DEFAULT_FILES_PER_PARTITION);
+		
+		JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename).cache();
+		json_list_data.setName("JSON-file-list");
+		
+		long num_volumes = json_list_data.count();
+		double per_vol = 100.0/(double)num_volumes;
+
+		int num_partitions = (int)(num_volumes/files_per_partition)+1;
+		
+		JavaRDD<String> json_list_data_rp = json_list_data.repartition(num_partitions);
+		json_list_data_rp.setName("JSON-file-list--repartitioned");
+		
+		DoubleAccumulator per_vol_progress_accum = jsc.sc().doubleAccumulator("Per Volume Progress Percent");
+		
+		boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
+		
+		PerVolumeCatalogLangStreamFlatmap volume_catalog_langfreq_flatmap 
+			= new PerVolumeCatalogLangStreamFlatmap(_input_dir,_verbosity, 
+								     per_vol_progress_accum,per_vol,
+								     strict_file_io);
+		JavaRDD<String> catalog_lang_list = json_list_data_rp.flatMap(volume_catalog_langfreq_flatmap); 
+		catalog_lang_list.persist(StorageLevel.MEMORY_AND_DISK());
+		catalog_lang_list.setName("catalog-lang-stream");
+	
+		
+		JavaPairRDD<String, Long> catalog_lang_pairs = catalog_lang_list.mapToPair(s -> new Tuple2<String, Long>(s, 1L));
+		catalog_lang_pairs.setName("single-catalog-lang-count");
+		
+		JavaPairRDD<String, Long> catalog_lang_counts = catalog_lang_pairs.reduceByKey((a, b) -> a + b);
+		catalog_lang_counts.setName("catalog-lang-frequency");
+		
+		JavaPairRDD<Long, String> catalog_lang_counts_swapped_pair
+			= catalog_lang_counts.mapToPair(item -> item.swap());
+		catalog_lang_counts_swapped_pair.setName("frequency-catalog-lang-swap");
+		
+		JavaPairRDD<Long, String> catalog_lang_counts_swapped_pair_sorted 
+			= catalog_lang_counts_swapped_pair.sortByKey(false, num_partitions);
+		catalog_lang_counts_swapped_pair_sorted.setName("descending-sorted-frequency-catalog-lang");
+		
+		JavaPairRDD<String, Long> catalog_lang_count_sorted 
+			= catalog_lang_counts_swapped_pair_sorted.mapToPair(item -> item.swap());
+		catalog_lang_count_sorted.setName("descending-catalog-lang-frequency");
+		
+		
+		catalog_lang_count_sorted.saveAsTextFile(output_directory);
+		jsc.close();
+	}
+
+	
+	public static void print_usage(HelpFormatter formatter, Options options)
+	{
+		formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
+	}
+	
+	public static void main(String[] args) {
+		Options options = new Options();
+
+		Option verbosity_opt = new Option("v", "verbosity", true, 
+				"Set to control the level of debugging output [0=none, 1=some, 2=lots]");
+		verbosity_opt.setRequired(false);
+		options.addOption(verbosity_opt);
+		
+		Option properties_opt = new Option("p", "properties", true, 
+				"Read in the specified Java properties file");
+		properties_opt.setRequired(false);
+		options.addOption(properties_opt);
+		
+		// Need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark 
+		CommandLineParser parser = new GnuParser(); 
+		//CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
+		
+		HelpFormatter formatter = new HelpFormatter();
+		CommandLine cmd = null;
+
+		try {
+			cmd = parser.parse(options, args);
+		}
+		catch (ParseException e) {
+			System.err.println(e.getMessage());
+			print_usage(formatter,options);
+			System.exit(1);
+		}
+
+		String verbosity_str = cmd.getOptionValue("verbosity","1");
+		int verbosity = Integer.parseInt(verbosity_str);
+
+		String property_filename = cmd.getOptionValue("properties",null);
+		
+		String[] filtered_args = cmd.getArgs();
+
+		if (filtered_args.length != 2) {
+			print_usage(formatter,options);
+			System.exit(1);
+		}
+	
+		if (property_filename != null) {
+			try {
+				FileInputStream fis = new FileInputStream(property_filename);
+				BufferedInputStream bis = new BufferedInputStream(fis);
+				
+				System.getProperties().load(bis);
+			} 
+			catch (FileNotFoundException e) {
+				e.printStackTrace();
+				System.err.println("File not found: '" + property_filename + "'. Skipping property file read");
+			}
+			catch (IOException e) {
+				System.err.println("IO Exception for: '" + property_filename + "'. Malformed syntax? Skipping property file read");
+			}
+		}
+				
+		String input_dir  = filtered_args[0];
+		String json_list_filename = filtered_args[1];
+		
+		ProcessForCatalogLangCount prep_for_lang 
+			= new ProcessForCatalogLangCount(input_dir,json_list_filename,verbosity);
+		
+		prep_for_lang.execCatalogLangCount();
+	
+	}
+}
