Вам необходимо указать значение столбца, по которому вы хотите выполнить фильтрацию. Проверьте этот фрагмент ниже:
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.SparkSession;
import java.util.List;
public class App {
public static void main(String[] args) {
String logFile = "/Users/ajay/input.txt";
SparkSession spark = SparkSession.builder().appName("Simple Application")
.config("spark.master", "local").getOrCreate();
Dataset<String> logData = spark.read().textFile(logFile).cache();
List<String> rowList = logData.collectAsList();
System.out.println("rowList is = " + rowList);
Dataset<String> rowDatasetWithA = logData.filter((logData.col("value").contains("a")));
List<String> rowWithA = rowDatasetWithA.collectAsList();
System.out.println("rowWithA is = " + rowWithA);
Dataset<String> rowDatasetWithB = logData.filter((logData.col("value").contains("b")));
List<String> rowWithB = rowDatasetWithB.collectAsList();
System.out.println("rowWithB is = " + rowWithB);
long numAs = rowWithA.size();
long numBs = rowWithB.size();
System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs);
spark.stop();
}
}
Предполагая, что содержимое input.txt
является следующим
a
b
c
a
aa
Результатом приведенного выше фрагмента будет
rowList is = [a, b, c, a, aa]
rowWithA is = [a, a, aa]
rowWithB is = [b]
Lines with a: 3, lines with b: 1
enter code here
Надеюсь, это поможет.