你如何首先提取架构,然后从java中的avro文件中提取数据?除了java之外,与此问题相同.
我已经看到了如何从avsc文件而不是avro文件中获取模式的示例.任何方向都非常赞赏.
Schema schema = new Schema.Parser().parse(new File("/home/Hadoop/Avro/schema/emp.avsc"));
Run Code Online (Sandbox Code Playgroud) 我试图将avro文件合并到一个大文件中,问题是concat
命令不接受通配符
hadoop jar avro-tools.jar concat /input/part* /output/bigfile.avro
Run Code Online (Sandbox Code Playgroud)
我明白了:
线程"main"中的异常java.io.FileNotFoundException:文件不存在:/ input/part*
我试图使用""
,''
但没有机会.
我正在尝试使用这种avro shcema
{
"namespace": "nothing",
"name": "myAvroSchema",
"type": "record",
"fields": [
{
"name": "checkInCustomerReference",
"type": "string"
},
{
"name": "customerContacts",
"type": "record",
"fields": [
{
"name": "customerEmail",
"type": "array",
"items": {
"type": "record",
"name": "customerEmail_element",
"fields": [
{
"name": "emailAddress",
"type": "string"
},
{
"name": "typeOfEmail",
"type": "string"
}
]
}
},
{
"name": "customerPhone",
"type": "array",
"items": {
"type": "record",
"name": "customerPhone_element",
"fields": [
{
"name": "fullContactNumber",
"type": "string"
},
{
"name": "ISDCode",
"type": "string"
}
]
}
}, …
Run Code Online (Sandbox Code Playgroud) 我有一些看起来像这样的json数据:
{
"id": 1998983092,
"name": "Test Name 1",
"type": "search string",
"creationDate": "2017-06-06T13:49:15.091+0000",
"lastModificationDate": "2017-06-28T14:53:19.698+0000",
"lastModifiedUsername": "testuser@test.com",
"lockedQuery": false,
"lockedByUsername": null
}
Run Code Online (Sandbox Code Playgroud)
我可以将lockedQuery null值添加到GenericRecord对象而不会出现问题.
GenericRecord record = new GenericData.Record(schema);
if(json.isNull("lockedQuery")){
record.put("lockedQuery", null);
}
Run Code Online (Sandbox Code Playgroud)
但是,稍后当我尝试将该GenericRecord对象写入avro文件时,我得到一个空指针异常.
File file = new File("~/test.arvo");
DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(datumWriter);
dataFileWriter.create(schema, file);
for(GenericRecord record: masterList) {
dataFileWriter.append(record); // NULL POINTER HERE
}
Run Code Online (Sandbox Code Playgroud)
当我运行该代码时,我得到以下异常.有关如何将空值处理成Avro文件的任何提示都非常感谢.提前致谢.
java.lang.NullPointerException: null of boolean in field lockedQuery of
com.mydomain.test1.domain.MyAvroRecord
Exception in thread "main" java.lang.RuntimeException:
org.apache.avro.file.DataFileWriter$AppendWriteException:
java.lang.NullPointerException: null of …
Run Code Online (Sandbox Code Playgroud) 假设你有这个 AVDL 作为一个简化的例子:
@namespace("example.avro")
protocol User {
record Man {
int age;
}
record Woman {
int age;
}
record User {
union {
Man,
Woman
} user_info;
}
}
Run Code Online (Sandbox Code Playgroud)
在 python 中,您无法正确序列化声明类型的对象,因为不允许使用以下语法:
{"user_info": {"Woman": {"age": 18}}}
Run Code Online (Sandbox Code Playgroud)
唯一被序列化的对象是
{"user_info": {"age": 18}}
Run Code Online (Sandbox Code Playgroud)
丢失所有类型信息并且DatumWriter
通常选择匹配字段集的第一条记录,在本例中为 a Man
。
使用 Java API 时,上述问题非常有效。
那么,我在这里做错了什么?序列化和反序列化在 Python 的 Avro 实现中是否可能不是幂等的?
我有一个JSON文件和一个avro架构文件,它正确地描述了它的结构.然后我将带有Avro工具的JSON文件转换为avro文件,而不会出现错误,如下所示:
java -jar .\avro-tools-1.7.7.jar fromjson --schema-file .\data.avsc .\data.json > .\data.avro
Run Code Online (Sandbox Code Playgroud)
然后我将生成的Avro文件转换回JSON,以验证我是否得到了这样的有效Avro文件:
java -jar .\avro-tools-1.7.7.jar tojson .\data.avro > .\data.json
Run Code Online (Sandbox Code Playgroud)
这会引发错误:
Exception in thread "main" java.io.IOException: Not a data file.
at org.apache.avro.file.DataFileStream.initialize(DataFileStream.java:105)
at org.apache.avro.file.DataFileReader.<init>(DataFileReader.java:97)
at org.apache.avro.tool.DataFileGetMetaTool.run(DataFileGetMetaTool.java:64)
at org.apache.avro.tool.Main.run(Main.java:84)
at org.apache.avro.tool.Main.main(Main.java:73)
Run Code Online (Sandbox Code Playgroud)
在做'getschema'或'getmeta'时我也得到了同样的例外,如果我使用avro-tools-1.8.2或avro-tools-1.7.4.我还尝试了多个不同的json和架构数据,我检查了它的有效性.
此处抛出错误(在Avro工具中):
if (!Arrays.equals(DataFileConstants.MAGIC, magic)) {
throw new IOException("Not a data file.");
}
Run Code Online (Sandbox Code Playgroud)
看起来,(二进制)Avro文件与预期的Avro文件不匹配,因为开头有几个字符.
我已经检查了有关此错误的所有其他stackoverflow问题,但它们都没有帮助.我在Windows 10 PowerShell上使用了命令行.
有人知道这里发生了什么事吗?
更新:如果我在Cloudera VM上而不是在Windows中进行转换,则转换有效.在生成的Avro文件中,开头只有几个不同.
我需要创建AVRO文件,但为此我需要做两件事:
1)JSON
2)Avro模式
从这两个要求-我有JSON:
{"web-app": {
"servlet": [
{
"servlet-name": "cofaxCDS",
"servlet-class": "org.cofax.cds.CDSServlet",
"init-param": {
"configGlossary:installationAt": "Philadelphia, PA",
"configGlossary:adminEmail": "ksm@pobox.com",
"configGlossary:poweredBy": "Cofax",
"configGlossary:poweredByIcon": "/images/cofax.gif",
"configGlossary:staticPath": "/content/static",
"templateProcessorClass": "org.cofax.WysiwygTemplate",
"templateLoaderClass": "org.cofax.FilesTemplateLoader",
"templatePath": "templates",
"templateOverridePath": "",
"defaultListTemplate": "listTemplate.htm",
"defaultFileTemplate": "articleTemplate.htm",
"useJSP": false,
"jspListTemplate": "listTemplate.jsp",
"jspFileTemplate": "articleTemplate.jsp",
"cachePackageTagsTrack": 200,
"cachePackageTagsStore": 200,
"cachePackageTagsRefresh": 60,
"cacheTemplatesTrack": 100,
"cacheTemplatesStore": 50,
"cacheTemplatesRefresh": 15,
"cachePagesTrack": 200,
"cachePagesStore": 100,
"cachePagesRefresh": 10,
"cachePagesDirtyRead": 10,
"searchEngineListTemplate": "forSearchEnginesList.htm",
"searchEngineFileTemplate": "forSearchEngines.htm",
"searchEngineRobotsDb": "WEB-INF/robots.db",
"useDataStore": true,
"dataStoreClass": "org.cofax.SqlDataStore",
"redirectionClass": "org.cofax.SqlRedirection",
"dataStoreName": "cofax",
"dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", …
Run Code Online (Sandbox Code Playgroud) 我的应用程序一直在使用 json 模式 (org.everit.json.schema.Schema) 来验证 JSON 消息是否符合特定格式。我们现在正在考虑转向 Avro 模式。这涉及将先前存储的 schema.json 文件转换为 Avro schema schema.avsc。此外,当前的行为是我们通过 API /schema/create 获取 JSON 格式的架构,并在使用 SchemaLoader 对其进行验证后将其存储为 schema.json 格式,例如,SchemaLoader.load(JSONObject obj)。
我们还需要一种方法将这个 schema.json 转换为 schema.avsc,因为我们通过 API 接收它运行时。我们可以使用任何实用程序/工具将 schema.json 转换为 schema.avsc 吗?
我正在尝试按照以下 avro 模式创建 JSON 字符串,用于十进制值。 https://avro.apache.org/docs/1.8.2/spec.html#Logical+Types
{
"name": "score",
"type": "bytes",
"logicalType": "decimal",
"precision": 10,
"scale": 5
}
Run Code Online (Sandbox Code Playgroud)
价值
"score":3.4,
Run Code Online (Sandbox Code Playgroud)
我得到了例外
Caused by: org.apache.avro.AvroTypeException: Expected bytes. Got VALUE_NUMBER_FLOAT.
Run Code Online (Sandbox Code Playgroud)
如果我给出“\u0000”而不是 3.4,那么它可以工作,但这是 0 的表示,我将如何获得 3.4 的表示?现在我正在创建硬编码的 JSON 字符串,但将来我必须将输出转换为十进制,我如何在 Scala 中做到这一点。
有没有办法将值转换为十进制逻辑格式?
我有一个使用 JAVA api 创建的 avro 文件,当编写者在文件中写入数据时,程序由于机器重新启动而异常关闭。\n现在,当我尝试使用 Spark/hive 读取此文件时,它会读取一些数据并然后抛出以下错误 (org.apache.avro.AvroRuntimeException: java.io.IOException: 无效同步!)\xe2\x80\x93
\nINFO DAGScheduler: ShuffleMapStage 1 (count at DataReaderSpark.java:41) failed in 7.420 s due to Job aborted due to stage failure: Task 1 in stage 1.0 failed 1 times, most recent failure: Lost task 1.0 in stage 1.0 (TID 2, localhost, executor driver): org.apache.avro.AvroRuntimeException: java.io.IOException: Invalid sync!\n at org.apache.avro.file.DataFileStream.hasNext(DataFileStream.java:210)\n at com.databricks.spark.avro.DefaultSource$$anonfun$buildReader$1$$anon$1.hasNext(DefaultSource.scala:215)\n at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)\n at org.apache.spark.sql.execution.datasources.FileScanRDD$$anon$1.hasNext(FileScanRDD.scala:106)\n at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.agg_doAggregateWithoutKey$(Unknown Source)\n at org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIteratorForCodegenStage1.processNext(Unknown Source)\n at org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)\n at org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$10$$anon$1.hasNext(WholeStageCodegenExec.scala:614)\n at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:408)\n at org.apache.spark.shuffle.sort.BypassMergeSortShuffleWriter.write(BypassMergeSortShuffleWriter.java:125)\n at …
Run Code Online (Sandbox Code Playgroud) avro ×10
avro-tools ×10
json ×4
java ×3
event-bus ×1
hadoop ×1
hdfs ×1
hive ×1
jsonschema ×1
python ×1
scala ×1
schema ×1
spark-avro ×1