- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在研究 POC,我必须使用 token 更新数据库中的帐号。我将数据读入数据集dsRecords(大约2M条记录)。我有另一个例程,它捕获了不同的帐号并获取了 token ,映射存储在 HashMap 中。
Dataset<Row> applySwappedTokens(Dataset<Row> dsRecords, Map<String, String> mappedTokens){
}
现在,我必须迭代数据集才能执行以下操作 -1.读取帐号列(accountNumber)值并使用mappedTokens中的 token 值更新(我知道数据集是不可变的。因此,更新数据集意味着创建具有更新行的数据集副本)。这可以通过 JOIN 或其他操作来实现,但由于第二项任务,我没有在这方面花费精力。2. 读取另一个 XML blob 列并找到帐号并更新它。
到目前为止,我尝试过的所有选项都会因不可序列化的代码而导致编译时错误或测试编译错误。大多数在线资源都是 Scala 语言而不是 Java 语言。请帮忙。
Spark 2.1Java 8
方法1 - 由于序列化错误而无法测试。
Dataset<Row> output = sparkSession.sqlContext().createDataFrame(dsRecords.javaRDD().map(row -> {
return RowFactory.create(row.get(0), row.get(1), row.get(2), swapToken(row.get(3)),row.get(4));
}), dsRecords.schema());
return output;
String swapToken(Object inputToken) {
return mappedTokens.get(inputToken);//mappedToken will have to be instance field.
}
方法2-不完整。
dsRecords.foreach((ForeachFunction<Row>) row -> {
Integer index = row.fieldIndex("accountNumber");
String pan = row.getString(index);
String swap = this.swapToken(pan);
//TODO: create a dataset with rows from dsRecords but swap value.
});
方法3 - 使用带有 map 功能的UDF
创建一个 UDF2(采用 2 个输入参数,即 accountNumber 和mappedToken 并返回 token )。看来UDF只能取列值
更新 1 - UDF所以,我实现了UDF(AFK,稍后会发布代码):1.定义UDF1‘updateToken’来传递xml列值并返回更新后的xml值。2. 将具有帐户- token 对映射的 HashMap 实例“mappedTokens”设为静态。在我的 UDF 函数内访问它,以在 xml 字符串中查找帐户并使用 token 进行更新。
我可以测试我的 applySwappedTokens 函数,该函数在数据集“withColumn”上调用上述 UDF。然而,当我运行 Spark 程序时,我看到“mappedToken”具有“null”数据,因此 xml 列会使用空数据进行更新。我认为静态“mappedTokens”要么在另一个 JVM 中,要么在驱动程序中(即使在本地,spark 也会创建隔离的驱动程序、执行程序)。令人沮丧的是,没有简单的解决方案来迭代和更新 Spark 中的行。
Dataset<Row> processByRow(Dataset<Row> dsRecords, SparkSession sparkSession) {
sparkSession.udf().register("updateToken", updateToken, DataTypes.StringType);
return ds = dsRecords.withColumn("eventRecordTokenText", callUDF("updateToken", dsRecords.col("eventRecordTokenText")));
}
static UDF1 updateToken = new UDF1<String, String>() {
public String call(final String tokenText) throws Exception {
// xml operations here..
for (int nodeIndex = 0; nodeIndex < nList.getLength(); nodeIndex++) {
Node thisNode = nList.item(nodeIndex);
if (thisNode.getAttributes().getNamedItem("ProcessTokenValue") != null && thisNode.getAttributes()
.getNamedItem("ProcessTokenValue").getNodeValue().equalsIgnoreCase("true")) {
Node valueNode = thisNode.getAttributes().getNamedItem("Value");
String thisToken = valueNode.getNodeValue();
String newToken = mappedTokens.get(thisToken); // *returns null values from the map*
if(newToken != null && !newToken.isEmpty())
valueNode.setNodeValue(newToken);
}
}
// more xml operations here..
return output;
}
};
更新 2 - 迭代和更新现在,我正在尝试逐行遍历..
Dataset<Row> processByRow1(Dataset<Row> dsRecords, SparkSession sparkSession) {
List<MongoRecordSmall> newRows = new ArrayList<MongoRecordSmall>();
dsRecords.foreach((ForeachFunction<Row>) record -> {
String currentToken = record.getAs(AppConstants.TokenCol);
String newToken = mappedTokens.get(currentToken);
newRows.add(new MongoRecordSmall(record.getString(0), record.getString(1), newToken, record.getString(3)));
logger.error(“Size plus=“+newRows.size());
});
return sparkSession.createDataFrame(newRows, MongoRecordSmall.class);
}
这会引发序列化错误。似乎( https://databricks.gitbooks.io/databricks-spark-knowledge-base/content/troubleshooting/javaionotserializableexception.html )我的类存在上述逻辑,正在被序列化并发送到工作节点,但未能这样做。
最佳答案
由于我没有找到更好的答案,我将用我实现的解决方案来回答我的问题(它看起来很难看!)-
Dataset<Row> processByRowUpdate(Dataset<Row> dsRecords, SparkSession sparkSession) {
List<Row> rows = dsRecords.select("accountIdentifier", "accountNumber").collectAsList();
List<MongoRecord> newRows = new ArrayList<MongoRecord>();
ListIterator<Row> it = rows.listIterator();
boolean errorOccurred = false;
while (it.hasNext()) {
try {
Row record = it.next();
MongoRecord mongo = new MongoRecord();
if (!record.isNullAt(record.fieldIndex("accountIdentifier")))
mongo.setAccountIdentifier(String.valueOf(record.getDouble(record.fieldIndex("accountIdentifier"))));
//... and so on
newRows.add(mongo);
} catch (Exception exception) {}
sparkSession.createDataFrame(newRows, MongoRecord.class);
关于java - 如何在 Java 中迭代 Spark 数据集并更新列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49395093/
目前正在学习 Spark 的类(class)并了解到执行者的定义: Each executor will hold a chunk of the data to be processed. Thisc
阅读了有关 http://spark.apache.org/docs/0.8.0/cluster-overview.html 的一些文档后,我有一些问题想要澄清。 以 Spark 为例: JavaSp
Spark核心中的调度器与以下Spark Stack(来自Learning Spark:Lightning-Fast Big Data Analysis一书)中的Standalone Schedule
我想在 spark-submit 或 start 处设置 spark.eventLog.enabled 和 spark.eventLog.dir -all level -- 不要求在 scala/ja
我有来自 SQL Server 的数据,需要在 Apache Spark (Databricks) 中进行操作。 在 SQL Server 中,此表的三个键列使用区分大小写的 COLLATION 选项
所有这些有什么区别和用途? spark.local.ip spark.driver.host spark.driver.bind地址 spark.driver.hostname 如何将机器修复为 Sp
我有大约 10 个 Spark 作业,每个作业都会进行一些转换并将数据加载到数据库中。必须为每个作业单独打开和关闭 Spark session ,每次初始化都会耗费时间。 是否可以只创建一次 Spar
/Downloads/spark-3.0.1-bin-hadoop2.7/bin$ ./spark-shell 20/09/23 10:58:45 WARN Utils: Your hostname,
我是 Spark 的完全新手,并且刚刚开始对此进行更多探索。我选择了更长的路径,不使用任何 CDH 发行版安装 hadoop,并且我从 Apache 网站安装了 Hadoop 并自己设置配置文件以了解
TL; 博士 Spark UI 显示的内核和内存数量与我在使用 spark-submit 时要求的数量不同 更多细节: 我在独立模式下运行 Spark 1.6。 当我运行 spark-submit 时
spark-submit 上的文档说明如下: The spark-submit script in Spark’s bin directory is used to launch applicatio
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 6 个月前关闭。 Improve
我想了解接收器如何在 Spark Streaming 中工作。根据我的理解,将有一个接收器任务在执行器中运行,用于收集数据并保存为 RDD。当调用 start() 时,接收器开始读取。需要澄清以下内容
有没有办法在不同线程中使用相同的 spark 上下文并行运行多个 spark 作业? 我尝试使用 Vertx 3,但看起来每个作业都在排队并按顺序启动。 如何让它在相同的 spark 上下文中同时运行
我们有一个 Spark 流应用程序,这是一项长期运行的任务。事件日志指向 hdfs 位置 hdfs://spark-history,当我们开始流式传输应用程序时正在其中创建 application_X
我们正在尝试找到一种加载 Spark (2.x) ML 训练模型的方法,以便根据请求(通过 REST 接口(interface))我们可以查询它并获得预测,例如http://predictor.com
Spark newb 问题:我在 spark-sql 中进行完全相同的 Spark SQL 查询并在 spark-shell . spark-shell版本大约需要 10 秒,而 spark-sql版
我正在使用 Spark 流。根据 Spark 编程指南(参见 http://spark.apache.org/docs/latest/programming-guide.html#accumulato
我正在使用 CDH 5.2。我可以使用 spark-shell 运行命令。 如何运行包含spark命令的文件(file.spark)。 有没有办法在不使用 sbt 的情况下在 CDH 5.2 中运行/
我使用 Elasticsearch 已经有一段时间了,但使用 Cassandra 的经验很少。 现在,我有一个项目想要使用 Spark 来处理数据,但我需要决定是否应该使用 Cassandra 还是
我是一名优秀的程序员,十分优秀!