怎麼解析Apache Avro資料?這篇文章跟大家介紹一下序列化產生Avro資料、反序列化解析Avro資料、使用FlinkSQL解析Avro資料的方法,希望對大家有幫助!
隨著網路高速的發展,雲端運算、大數據、人工智慧AI、物聯網等尖端技術已然成為當今時代主流的高新技術,電商網站、臉部辨識、無人駕駛、智慧家居、智慧城市等等,不僅方面方便了人們的衣食住行,背後更是時時刻刻有大量的數據在經過各種各樣的系統平台的採集、清晰、分析,而確保資料的低時延、高吞吐、安全性就顯得尤為重要,Apache Avro本身透過Schema的方式序列化後進行二進位傳輸,一方面保證了資料的高速傳輸,另一方面保證了資料安全性,avro目前在各行業的應用越來越廣泛,如何對avro數據進行處理解析應用就格外重要,本文將示範如果序列化產生avro數據,並使用FlinkSQL進行解析。
本文是avro解析的demo,目前FlinkSQL僅適用於簡單的avro資料解析,複雜巢狀avro資料暫時不支援。
場景介紹
本文主要介紹以下三個重點內容:
#如何序列化產生Avro資料
#如何反序列化解析Avro資料
如何使用FlinkSQL解析Avro資料
前提條件
了解avro是什麼,可參考apache avro官網快速入門指南
#了解avro應用程式場景
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huawei.bigdata</groupId> <artifactId>avrodemo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.avro</groupId> <artifactId>avro</artifactId> <version>1.8.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.avro</groupId> <artifactId>avro-maven-plugin</artifactId> <version>1.8.1</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>schema</goal> </goals> <configuration> <sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory> <outputDirectory>${project.basedir}/src/main/java/</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>注意:以上pom檔案設定了自動生成類別的路徑,即
rr
o/和{"namespace": "lancoo.ecbdc.pre", "type": "record", "name": "User", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]} ] }############3、編譯schema# ##
点击maven projects项目的compile进行编译,会自动在创建namespace路径和User类代码
4、序列化
创建TestUser类,用于序列化生成数据
User user1 = new User(); user1.setName("Alyssa"); user1.setFavoriteNumber(256); // Leave favorite col or null // Alternate constructor User user2 = new User("Ben", 7, "red"); // Construct via builder User user3 = User.newBuilder() .setName("Charlie") .setFavoriteColor("blue") .setFavoriteNumber(null) .build(); // Serialize user1, user2 and user3 to disk DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class); DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter); dataFileWriter.create(user1.getSchema(), new File("user_generic.avro")); dataFileWriter.append(user1); dataFileWriter.append(user2); dataFileWriter.append(user3); dataFileWriter.close();
执行序列化程序后,会在项目的同级目录下生成avro数据
user_generic.avro内容如下:
Objavro.schema�{"type":"record","name":"User","namespace":"lancoo.ecbdc.pre","fields":[{"name":"name","type":"string"},{"name":"favorite_number","type":["int","null"]},{"name":"favorite_color","type":["string","null"]}]}
至此avro数据已经生成。
5、反序列化
通过反序列化代码解析avro数据
// Deserialize Users from disk DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class); DataFileReader<User> dataFileReader = new DataFileReader<User>(new File("user_generic.avro"), userDatumReader); User user = null; while (dataFileReader.hasNext()) { // Reuse user object by passing it to next(). This saves us from // allocating and garbage collecting many objects for files with // many items. user = dataFileReader.next(user); System.out.println(user); }
执行反序列化代码解析user_generic.avro
avro数据解析成功。
6、将user_generic.avro上传至hdfs路径
hdfs dfs -mkdir -p /tmp/lztest/ hdfs dfs -put user_generic.avro /tmp/lztest/
7、配置flinkserver
- 准备avro jar包
将flink-sql-avro-*.jar、flink-sql-avro-confluent-registry-*.jar放入flinkserver lib,将下面的命令在所有flinkserver节点执行
cp /opt/huawei/Bigdata/FusionInsight_Flink_8.1.2/install/FusionInsight-Flink-1.12.2/flink/opt/flink-sql-avro*.jar /opt/huawei/Bigdata/FusionInsight_Flink_8.1.3/install/FusionInsight-Flink-1.12.2/flink/lib chmod 500 flink-sql-avro*.jar chown omm:wheel flink-sql-avro*.jar
-
同时重启FlinkServer实例,重启完成后查看avro包是否被上传
hdfs dfs -ls /FusionInsight_FlinkServer/8.1.2-312005/lib
8、编写FlinkSQL
CREATE TABLE testHdfs( name String, favorite_number int, favorite_color String ) WITH( 'connector' = 'filesystem', 'path' = 'hdfs:///tmp/lztest/user_generic.avro', 'format' = 'avro' );CREATE TABLE KafkaTable ( name String, favorite_number int, favorite_color String ) WITH ( 'connector' = 'kafka', 'topic' = 'testavro', 'properties.bootstrap.servers' = '96.10.2.1:21005', 'properties.group.id' = 'testGroup', 'scan.startup.mode' = 'latest-offset', 'format' = 'avro' ); insert into KafkaTable select * from testHdfs;
保存提交任务
9、查看对应topic中是否有数据
FlinkSQL解析avro数据成功。
【推荐:Apache使用教程】
以上是聊聊怎麼解析Apache Avro資料(範例講解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Apache的核心功能是模塊化設計和高度的可定制性,使其能滿足各種Web服務需求。 1.模塊化設計允許通過加載不同模塊擴展功能。 2.支持多種操作系統,適用於不同環境。 3.多進程、多線程和事件驅動模型提高了性能。 4.基本用法包括配置虛擬主機和文檔根目錄。 5.高級用法涉及URL重寫、負載均衡和反向代理。 6.常見錯誤可以通過語法檢查和日誌分析調試。 7.性能優化包括調整MPM設置和啟用緩存。

Apache在現代Web環境中仍然受歡迎的原因是其強大功能和靈活性。 1)模塊化設計允許定制功能,如安全認證和負載均衡。 2)支持多操作系統,增強普及性。 3)高效處理並發請求,適合各種應用場景。

Apache從開源項目發展為行業標準的原因包括:1)社區驅動,吸引全球開發者參與;2)標準化與兼容性,遵循互聯網標準;3)商業支持與生態系統,獲得企業級市場支持。

Apache對Webhosting的影響主要體現在其開源特性、強大功能和靈活性。 1)開源特性降低了Webhosting的門檻。 2)強大功能和靈活性使其成為大型網站和企業的首選。 3)虛擬主機功能節省了成本。儘管在高並發情況下性能可能下降,但通過不斷優化,Apache仍保持競爭力。

Apache起源於1995年,由一群開發者創建,旨在改進NCSAHTTPd服務器,成為全球最廣泛使用的Web服務器。 1.起源於1995年,旨在改進NCSAHTTPd服務器。 2.定義了Web服務器標準,推動了開源運動的發展。 3.孕育了Tomcat、Kafka等重要子項目。 4.面臨雲計算和容器技術的挑戰,未來將注重與雲原生技術整合。

Apache通過提供穩定的Web服務器基礎設施、推動開源文化和孵化重要項目,塑造了互聯網。 1)Apache提供了穩定的Web服務器基礎設施,促進了Web技術的創新。 2)Apache推動了開源文化的發展,ASF孵化了Hadoop、Kafka等重要項目。 3)儘管面臨性能挑戰,Apache的未來依然充滿希望,ASF不斷推出新技術。

ApacheHTTPServer自1995年由志願者創建以來,對Web服務器領域產生了深遠影響。 1.它源於對NCSAHTTPd不滿,提供更穩定、可靠的服務。 2.Apache軟件基金會的成立標誌其轉變為生態系統。 3.其模塊化設計和安全性提升了Web服務器的靈活性和安全性。 4.儘管市場份額下降,Apache仍與現代Web技術緊密聯繫。 5.通過配置優化和緩存,Apache提升了性能。 6.錯誤日誌和調試模式幫助解決常見問題。

ApacheHTTPServer通過模塊化設計、虛擬主機功能和性能優化,繼續高效地服務於現代互聯網環境中的Web內容。 1)模塊化設計允許添加如URL重寫等功能,提升網站SEO性能。 2)虛擬主機功能在一個服務器上託管多個網站,節省成本並簡化管理。 3)通過多線程處理和緩存優化,Apache能處理大量並發連接,提高響應速度和用戶體驗。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3漢化版
中文版,非常好用

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)