javascript for循环重复上一次迭代(javascript for-loop repeating last iteration)
我正在尝试裁剪调整大小,使用JS在客户端浏览器上显示图像。 我能够这样做,除了我的文本循环是错误的。 您可以在下面的图像中看到它正在重复上一次迭代。 我的图像循环工作正常。
Hint
- 第一个文件名文本应该是black.jpg
。尝试了几个小时后无法解决问题。 以下是代码的修剪版本。 如果需要,这里是脚本的完整版本 。 请帮忙,我还在学习。
HTML
<input type="file" id="input" onchange="prevCrop()" multiple> <ul id="listWrapper"></ul>
JAVASCRIPT
function prevCrop() { var files = document.querySelector('input[type=file]').files; for (var i = 0; i < files.length; i++) { var file = files[i]; var fileSize = Math.floor(file.size/1024); var info = file.name + " " + fileSize + " KB : iteration number - " + i; var reader = new FileReader(); if (reader != null) { reader.onload = GetThumbnail; reader.readAsDataURL(file); } } function GetThumbnail(e) { var canvas = document.createElement("canvas"); var newImage = new Image(); newImage.src = e.target.result; newImage.onload = cropResize; function cropResize() { canvas.width = 70; canvas.height = 70; ### more codes here that handles image ### var dataURL = canvas.toDataURL("image/jpg"); var thumbList = document.createElement('div'); thumbList.setAttribute('class', 'tlistClass'); var nImg = document.createElement('img'); nImg.src = dataURL; var infoSpan = document.createElement('span'); infoSpan.innerHTML = info; var handle = document.getElementById("listWrapper").appendChild(thumbList); handle.appendChild(nImg); handle.appendChild(infoSpan); } } }
I am trying to crop-resize n display an image on client-browser using JS. I am able to do so except that my text loop is wrong. You can see in the image below that it is repeating the last iteration. My image loop works fine though.
Hint
- First filename text is supposed to beblack.jpg
.Am unable to solve the issue after having tried for several hours. Given below is a trimmed version of the code. If needed here is the complete version of the script. Please help, I am still learning.
HTML
<input type="file" id="input" onchange="prevCrop()" multiple> <ul id="listWrapper"></ul>
JAVASCRIPT
function prevCrop() { var files = document.querySelector('input[type=file]').files; for (var i = 0; i < files.length; i++) { var file = files[i]; var fileSize = Math.floor(file.size/1024); var info = file.name + " " + fileSize + " KB : iteration number - " + i; var reader = new FileReader(); if (reader != null) { reader.onload = GetThumbnail; reader.readAsDataURL(file); } } function GetThumbnail(e) { var canvas = document.createElement("canvas"); var newImage = new Image(); newImage.src = e.target.result; newImage.onload = cropResize; function cropResize() { canvas.width = 70; canvas.height = 70; ### more codes here that handles image ### var dataURL = canvas.toDataURL("image/jpg"); var thumbList = document.createElement('div'); thumbList.setAttribute('class', 'tlistClass'); var nImg = document.createElement('img'); nImg.src = dataURL; var infoSpan = document.createElement('span'); infoSpan.innerHTML = info; var handle = document.getElementById("listWrapper").appendChild(thumbList); handle.appendChild(nImg); handle.appendChild(infoSpan); } } }
原文:https://stackoverflow.com/questions/36959445
最满意答案
我想做同样的事情。 我记录了https://github.com/confluentinc/schema-registry/issues/629 ,以便对模式注册表进行增强,以简化这一过程。 希望该项目能够接受这一想法。 它似乎应该是一个简单的增强实现。
My idea so far is to put a proxy service in front of Schema Registry (serving pure avro schemas) and scale it with HAProxy. Schema Registry itself seems to have scalable architecture for reads. To be honest I don't understand the paragraph about
avro.schema.url
property in AvroSerDe hive documentation:Specifies a URL to access the schema from. For http schemas, this works for testing and small-scale clusters, but as the schema will be accessed at least once from each task in the job, this can quickly turn the job into a DDOS attack against the URL provider (a web server, for instance). Use caution when using this parameter for anything other than testing.
I think that my proposal is a viable solution.
Having schemas in centralised repo allows for schema evolution and checking backward/forward compatibility, hence it's better than defining hdfs path, which is recommended in AvroSerDe docs.
相关问答
更多-
如何将Hive(avro表)与Schema Registry集成?(How to integrate Hive (avro tables) with Schema Registry?)[2023-04-03]
我想做同样的事情。 我记录了https://github.com/confluentinc/schema-registry/issues/629 ,以便对模式注册表进行增强,以简化这一过程。 希望该项目能够接受这一想法。 它似乎应该是一个简单的增强实现。 My idea so far is to put a proxy service in front of Schema Registry (serving pure avro schemas) and scale it with HAProxy. Sche ... -
在花费更多时间之后,这里是我想出的答案。 使用avro4s ,你可以使用默认的data输出流来包含每个序列化消息的模式。 或者,您可以使用binary输出流,它在序列化每条消息时简单地省略了模式。 ('binary'在这里有点用词不当,因为它所做的就是省略模式,在任何情况下它仍然是一个Array[Byte] 。) Akka本身提供了一个Serializer特性或一个SerializerWithStringManifest特性,它会自动在任何序列化的对象中包含一个“模式标识符”字段。 因此,当您创建自定义序列 ...
-
Avro与蜂巢中的Enum(Avro with Enum in Hive)[2023-07-19]
原因在于,如问题的最后一行所述: 我实际使用的数据是由汇合创建的。 事实证明,在使用HDFS接收器输出时, ENUM被转换为String 。 由于我基于原始模式在Hive中创建了外部表,因此存在差异。 现在,如果我只是从由hdfs接收器创建的文件中提取模式,并在表定义中使用这个模式,那么所有工作都可以按预期进行。 The reason is that as said in the last line of the question: The data I am actually using is creat ... -
只是猜测你在找什么...... kafka-avro-console-consumer --topic topicX --bootstrap-server kafka:9092 \ --property schema.registry.url="http://schema-registry:8081" 不,你不能指定模式版本。 该ID直接从主题中的Avro数据中消耗。 主题名称映射到主题名称。 使用--property print.key=true来查看Kafka消息密钥。 这是常规控制台消费者 ...
-
Confluent Schema Registry将在消息中发送序列化的Avro消息,而不使用整个Avro Schema。 我认为这就是“架构较少”消息的含义。 Confluent Schema Registry将存储Avro架构,并且线路上的消息中仅包含一个简短的索引ID。 完整的文档包括测试Confluent Schema Registry的快速入门指南 http://docs.confluent.io/current/schema-registry/docs/index.html The Conflu ...
-
具有架构的Avro消息(Avro messages with schema)[2022-08-27]
运行Confluent架构注册表时,使用Confluent Avro Serdes库发布的Kafka消息不包含avro架构。 它们只包含一个数字Schema id,消费者反序列化器使用它来从Confluent Schema Registry中获取Schema。 这些模式由序列化器和解串器缓存,作为进一步的性能优化。 When you run the Confluent Schema Registry, the Kafka messages published with the Confluent Avro ... -
问题出在_INTERNAL_KEY_和_INTERNAL_VALUE_ 我没有为这两个添加SCHEMA_REGISTRY_URL,就像我为_KEY_CONVERTER和_KEY_VALUE一样,我切换回org.apache.kafka.connect.json.JsonConverter获取内部键和值 the problem was with the _INTERNAL_KEY_ and _INTERNAL_VALUE_ I wasn't adding the SCHEMA_REGISTRY_URL for ...
-
我决定发布@DuduMarkovitz给出的补充答案。 为了使代码示例更简洁,让我们澄清一下STORED AS AVRO子句是这三行的等价物: ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe' STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive. ...
-
在Parquet数据上动态创建带有Avro架构的Hive外部表(Dynamically create Hive external table with Avro schema on Parquet Data)[2022-11-01]
以下查询工作: CREATE TABLE avro_test ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe' STORED AS AVRO TBLPROPERTIES ('avro.schema.url'='myHost/myAvroSchema.avsc'); CREATE EXTERNAL TABLE parquet_test LIKE avro_test STORED AS PARQUET LOCATION 'hdfs ... -
Spring Cloud Stream尚未与Confluent Schema Registry兼容。 请参阅此主题中的讨论https://github.com/spring-cloud/spring-cloud-stream/issues/850 The Spring Cloud Stream is not yet compatible with Confluent Schema Registry. See discussion in this thread https://github.com/sprin ...