我是靠谱客的博主 明亮豌豆,这篇文章主要介绍spark消费protobuf序列化的kafka数据存到hbase,现在分享给大家,希望可以做个参考。

遇见问题寻求解决办法

因为不知道是什么原因引起了这个问题,所以就尽可能详尽的把所有东西都贴上了,如果是没用的可以自行跳过。

流程是从用spark从kakfa读取数据,将数据存储到hbase里面。数据用protobuf序列化,所以反序列化时用到了protobuf里面的ListValue方法。

然后本地运行得时候数据可以存到hbase,但是将程序打成jar包在集群上用standalone模式运行会报错。

图片说明

图片说明

图片说明

在打好得jar包里面有两个protobuf的包,一个在hbase的threeparty下面版本为有一个2.1.0,另一个为pom导入的protobuf包版本为3.5.0,因为只有这个版本里面才有ListValue的方法。

下面是我写的demo。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.nio.charset.StandardCharsets import java.time.LocalDateTime import java.time.format.DateTimeFormatter import org.apache.hadoop.hbase.client.{ConnectionFactory, Put} import org.apache.hadoop.hbase.io.compress.Compression import org.apache.hadoop.hbase.util.Bytes import org.apache.hadoop.hbase.{HBaseConfiguration, HColumnDescriptor, HTableDescriptor, TableName} import org.apache.kafka.common.serialization.StringDeserializer import org.apache.spark.sql.SparkSession import org.apache.spark.streaming.kafka010.{ConsumerStrategies, KafkaUtils, LocationStrategies} import org.apache.spark.streaming.{Seconds, StreamingContext} object Test extends Serializable { val HBASE_TABLE_NAME_PREFIX: String = "DR_RT_OPC_KAFKA_" val FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM") def main(args: Array[String]): Unit = { val spark = SparkSession.builder().appName("kafka2spark2hbase").master("local[2]").getOrCreate() //.master("local[2]") val batchDuration = Seconds(5) //时间单位为秒 val ssc = new StreamingContext(spark.sparkContext, batchDuration) //ssc.checkpoint("/Users/eric/SparkWorkspace/checkpoint") val topics = Array(PropertiesUtil.getProperty("topics")).toSet val kafkaParams = Map[String, Object]( "bootstrap.servers" -> PropertiesUtil.getProperty("bootstrap.servers"), "key.deserializer" -> classOf[StringDeserializer], "value.deserializer" -> classOf[MyDeserializer], //实现自定义序列化方式 "group.id" -> PropertiesUtil.getProperty("group"), "auto.offset.reset" -> "latest", //第一次消费从最后的offset消费 "enable.auto.commit" -> (true: java.lang.Boolean) // kafka 保存消费的offset ) //创建DStream流 val stream = KafkaUtils.createDirectStream(ssc, LocationStrategies.PreferConsistent, ConsumerStrategies.Subscribe[String, Protoc.Bean](topics, kafkaParams)) //hbase配置 val hbaseConf = HBaseConfiguration.create() hbaseConf.set("hbase.zookeeper.quorum", PropertiesUtil.getProperty("quorum")) //zookeeper 集群 hbaseConf.set("hbase.zookeeper.property.client", "2181") hbaseConf.set("hbase.master", PropertiesUtil.getProperty("hbase_master")) hbaseConf.set("hbase.defaults.for.version.skip", "true") //获取连接 val conn = ConnectionFactory.createConnection(hbaseConf) val admin = conn.getAdmin import scala.collection.JavaConversions._ val tableName = HBASE_TABLE_NAME_PREFIX + FORMATTER.format(LocalDateTime.now.plusDays(1)) + "--" val tn1 = TableName.valueOf(tableName) var isExit = true val tables = admin.listTables("^" + tableName + "$") if (tables.size == 0) { isExit = false } //判断表是否存在,不存在创建表 if (!isExit) { val table: HTableDescriptor = new HTableDescriptor(tn1) val family = new HColumnDescriptor(Bytes.toBytes("info")) family.setCompressionType(Compression.Algorithm.SNAPPY) table.addFamily(family) admin.createTable(table) } //将DStream转化为RDD进行操作 if (stream != null) { stream.foreachRDD(rdd => { if (!rdd.isEmpty()) { rdd.foreachPartition(line => { import scala.collection.mutable.ListBuffer var putList: ListBuffer[Put] = ListBuffer() val hbaseConf = HBaseConfiguration.create() hbaseConf.set("hbase.zookeeper.quorum", PropertiesUtil.getProperty("quorum")) //zookeeper 集群 hbaseConf.set("hbase.zookeeper.property.client", "2181") hbaseConf.set("hbase.master", PropertiesUtil.getProperty("hbase_master")) hbaseConf.set("hbase.defaults.for.version.skip", "true") var rowKey: String = "" line.foreach(record => { rowKey = record.key().toString val p = new Put(Bytes.toBytes(record.value().getName + "_" + record.key())) p.addColumn("info".getBytes(StandardCharsets.UTF_8), "name".getBytes(StandardCharsets.UTF_8), record.value().getName.getBytes(StandardCharsets.UTF_8)) p.addColumn("info".getBytes(StandardCharsets.UTF_8), "value".getBytes(StandardCharsets.UTF_8), record.value().getValue.getBytes(StandardCharsets.UTF_8)) p.addColumn("info".getBytes(StandardCharsets.UTF_8), "describe".getBytes(StandardCharsets.UTF_8), record.value().getDescribe.getBytes(StandardCharsets.UTF_8)) putList = putList :+ p }) val conn = ConnectionFactory.createConnection(hbaseConf) val admin = conn.getAdmin val tn1 = TableName.valueOf(tableName.toString) val tables = conn.getTable(tn1) tables.put(putList) }) } else { println("RDD为空,数据消费完毕") } }) } else { println("DStream为空") } ssc.start() ssc.awaitTermination() } }

这是用protobuf的工具生成的java文件,用来反序列化。TagInfoProto为类,TagKafkaInfo为实体类对象。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
// Generated by the protocol buffer compiler. DO NOT EDIT! // source: TagInfoProto.proto public final class TagInfoProto { private TagInfoProto() {} public static void registerAllExtensions( com.google.protobuf.ExtensionRegistryLite registry) { } public static void registerAllExtensions( com.google.protobuf.ExtensionRegistry registry) { registerAllExtensions( (com.google.protobuf.ExtensionRegistryLite) registry); } public interface TagKafkaInfoOrBuilder extends // @@protoc_insertion_point(interface_extends:TagKafkaInfo) com.google.protobuf.MessageOrBuilder { /** * <code>string name = 1;</code> */ String getName(); /** * <code>string name = 1;</code> */ com.google.protobuf.ByteString getNameBytes(); /** * <code>string describe = 2;</code> */ String getDescribe(); /** * <code>string describe = 2;</code> */ com.google.protobuf.ByteString getDescribeBytes(); /** * <code>string value = 3;</code> */ String getValue(); /** * <code>string value = 3;</code> */ com.google.protobuf.ByteString getValueBytes(); /** * <code>string time = 4;</code> */ String getTime(); /** * <code>string time = 4;</code> */ com.google.protobuf.ByteString getTimeBytes(); } /** * Protobuf type {@code TagKafkaInfo} */ public static final class TagKafkaInfo extends com.google.protobuf.GeneratedMessageV3 implements // @@protoc_insertion_point(message_implements:TagKafkaInfo) TagKafkaInfoOrBuilder { private static final long serialVersionUID = 0L; // Use TagKafkaInfo.newBuilder() to construct. private TagKafkaInfo(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) { super(builder); } private TagKafkaInfo() { name_ = ""; describe_ = ""; value_ = ""; time_ = ""; } @Override public final com.google.protobuf.UnknownFieldSet getUnknownFields() { return this.unknownFields; } private TagKafkaInfo( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { this(); if (extensionRegistry == null) { throw new NullPointerException(); } int mutable_bitField0_ = 0; com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder(); try { boolean done = false; while (!done) { int tag = input.readTag(); switch (tag) { case 0: done = true; break; case 10: { String s = input.readStringRequireUtf8(); name_ = s; break; } case 18: { String s = input.readStringRequireUtf8(); describe_ = s; break; } case 26: { String s = input.readStringRequireUtf8(); value_ = s; break; } case 34: { String s = input.readStringRequireUtf8(); time_ = s; break; } default: { if (!parseUnknownFieldProto3( input, unknownFields, extensionRegistry, tag)) { done = true; } break; } } } } catch (com.google.protobuf.InvalidProtocolBufferException e) { throw e.setUnfinishedMessage(this); } catch (java.io.IOException e) { throw new com.google.protobuf.InvalidProtocolBufferException( e).setUnfinishedMessage(this); } finally { this.unknownFields = unknownFields.build(); makeExtensionsImmutable(); } } public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return TagInfoProto.internal_static_TagKafkaInfo_descriptor; } @Override protected FieldAccessorTable internalGetFieldAccessorTable() { return TagInfoProto.internal_static_TagKafkaInfo_fieldAccessorTable .ensureFieldAccessorsInitialized( TagInfoProto.TagKafkaInfo.class, TagInfoProto.TagKafkaInfo.Builder.class); } public static final int NAME_FIELD_NUMBER = 1; private volatile Object name_; /** * <code>string name = 1;</code> */ public String getName() { Object ref = name_; if (ref instanceof String) { return (String) ref; } else { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); name_ = s; return s; } } /** * <code>string name = 1;</code> */ public com.google.protobuf.ByteString getNameBytes() { Object ref = name_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (String) ref); name_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } public static final int DESCRIBE_FIELD_NUMBER = 2; private volatile Object describe_; /** * <code>string describe = 2;</code> */ public String getDescribe() { Object ref = describe_; if (ref instanceof String) { return (String) ref; } else { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); describe_ = s; return s; } } /** * <code>string describe = 2;</code> */ public com.google.protobuf.ByteString getDescribeBytes() { Object ref = describe_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (String) ref); describe_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } public static final int VALUE_FIELD_NUMBER = 3; private volatile Object value_; /** * <code>string value = 3;</code> */ public String getValue() { Object ref = value_; if (ref instanceof String) { return (String) ref; } else { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); value_ = s; return s; } } /** * <code>string value = 3;</code> */ public com.google.protobuf.ByteString getValueBytes() { Object ref = value_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (String) ref); value_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } public static final int TIME_FIELD_NUMBER = 4; private volatile Object time_; /** * <code>string time = 4;</code> */ public String getTime() { Object ref = time_; if (ref instanceof String) { return (String) ref; } else { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); time_ = s; return s; } } /** * <code>string time = 4;</code> */ public com.google.protobuf.ByteString getTimeBytes() { Object ref = time_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (String) ref); time_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } private byte memoizedIsInitialized = -1; @Override public final boolean isInitialized() { byte isInitialized = memoizedIsInitialized; if (isInitialized == 1) return true; if (isInitialized == 0) return false; memoizedIsInitialized = 1; return true; } @Override public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { if (!getNameBytes().isEmpty()) { com.google.protobuf.GeneratedMessageV3.writeString(output, 1, name_); } if (!getDescribeBytes().isEmpty()) { com.google.protobuf.GeneratedMessageV3.writeString(output, 2, describe_); } if (!getValueBytes().isEmpty()) { com.google.protobuf.GeneratedMessageV3.writeString(output, 3, value_); } if (!getTimeBytes().isEmpty()) { com.google.protobuf.GeneratedMessageV3.writeString(output, 4, time_); } unknownFields.writeTo(output); } @Override public int getSerializedSize() { int size = memoizedSize; if (size != -1) return size; size = 0; if (!getNameBytes().isEmpty()) { size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, name_); } if (!getDescribeBytes().isEmpty()) { size += com.google.protobuf.GeneratedMessageV3.computeStringSize(2, describe_); } if (!getValueBytes().isEmpty()) { size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3, value_); } if (!getTimeBytes().isEmpty()) { size += com.google.protobuf.GeneratedMessageV3.computeStringSize(4, time_); } size += unknownFields.getSerializedSize(); memoizedSize = size; return size; } @Override public boolean equals(final Object obj) { if (obj == this) { return true; } if (!(obj instanceof TagInfoProto.TagKafkaInfo)) { return super.equals(obj); } TagInfoProto.TagKafkaInfo other = ( TagInfoProto.TagKafkaInfo) obj; boolean result = true; result = result && getName() .equals(other.getName()); result = result && getDescribe() .equals(other.getDescribe()); result = result && getValue() .equals(other.getValue()); result = result && getTime() .equals(other.getTime()); result = result && unknownFields.equals(other.unknownFields); return result; } @Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptor().hashCode(); hash = (37 * hash) + NAME_FIELD_NUMBER; hash = (53 * hash) + getName().hashCode(); hash = (37 * hash) + DESCRIBE_FIELD_NUMBER; hash = (53 * hash) + getDescribe().hashCode(); hash = (37 * hash) + VALUE_FIELD_NUMBER; hash = (53 * hash) + getValue().hashCode(); hash = (37 * hash) + TIME_FIELD_NUMBER; hash = (53 * hash) + getTime().hashCode(); hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; } public static TagInfoProto.TagKafkaInfo parseFrom( java.nio.ByteBuffer data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static TagInfoProto.TagKafkaInfo parseFrom( java.nio.ByteBuffer data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static TagInfoProto.TagKafkaInfo parseFrom( com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static TagInfoProto.TagKafkaInfo parseFrom( com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static TagInfoProto.TagKafkaInfo parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data); } public static TagInfoProto.TagKafkaInfo parseFrom( byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return PARSER.parseFrom(data, extensionRegistry); } public static TagInfoProto.TagKafkaInfo parseFrom(java.io.InputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input); } public static TagInfoProto.TagKafkaInfo parseFrom( java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input, extensionRegistry); } public static TagInfoProto.TagKafkaInfo parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseDelimitedWithIOException(PARSER, input); } public static TagInfoProto.TagKafkaInfo parseDelimitedFrom( java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseDelimitedWithIOException(PARSER, input, extensionRegistry); } public static TagInfoProto.TagKafkaInfo parseFrom( com.google.protobuf.CodedInputStream input) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input); } public static TagInfoProto.TagKafkaInfo parseFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { return com.google.protobuf.GeneratedMessageV3 .parseWithIOException(PARSER, input, extensionRegistry); } @Override public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder() { return DEFAULT_INSTANCE.toBuilder(); } public static Builder newBuilder( TagInfoProto.TagKafkaInfo prototype) { return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); } @Override public Builder toBuilder() { return this == DEFAULT_INSTANCE ? new Builder() : new Builder().mergeFrom(this); } @Override protected Builder newBuilderForType( BuilderParent parent) { Builder builder = new Builder(parent); return builder; } /** * Protobuf type {@code TagKafkaInfo} */ public static final class Builder extends com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements // @@protoc_insertion_point(builder_implements:TagKafkaInfo) TagInfoProto.TagKafkaInfoOrBuilder { public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { return TagInfoProto.internal_static_TagKafkaInfo_descriptor; } @Override protected FieldAccessorTable internalGetFieldAccessorTable() { return TagInfoProto.internal_static_TagKafkaInfo_fieldAccessorTable .ensureFieldAccessorsInitialized( TagInfoProto.TagKafkaInfo.class, TagInfoProto.TagKafkaInfo.Builder.class); } // Construct using TagInfoProto.TagKafkaInfo.newBuilder() private Builder() { maybeForceBuilderInitialization(); } private Builder( BuilderParent parent) { super(parent); maybeForceBuilderInitialization(); } private void maybeForceBuilderInitialization() { if (com.google.protobuf.GeneratedMessageV3 .alwaysUseFieldBuilders) { } } @Override public Builder clear() { super.clear(); name_ = ""; describe_ = ""; value_ = ""; time_ = ""; return this; } @Override public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { return TagInfoProto.internal_static_TagKafkaInfo_descriptor; } @Override public TagInfoProto.TagKafkaInfo getDefaultInstanceForType() { return TagInfoProto.TagKafkaInfo.getDefaultInstance(); } @Override public TagInfoProto.TagKafkaInfo build() { TagInfoProto.TagKafkaInfo result = buildPartial(); if (!result.isInitialized()) { throw newUninitializedMessageException(result); } return result; } @Override public TagInfoProto.TagKafkaInfo buildPartial() { TagInfoProto.TagKafkaInfo result = new TagInfoProto.TagKafkaInfo(this); result.name_ = name_; result.describe_ = describe_; result.value_ = value_; result.time_ = time_; onBuilt(); return result; } @Override public Builder clone() { return (Builder) super.clone(); } @Override public Builder setField( com.google.protobuf.Descriptors.FieldDescriptor field, Object value) { return (Builder) super.setField(field, value); } @Override public Builder clearField( com.google.protobuf.Descriptors.FieldDescriptor field) { return (Builder) super.clearField(field); } @Override public Builder clearOneof( com.google.protobuf.Descriptors.OneofDescriptor oneof) { return (Builder) super.clearOneof(oneof); } @Override public Builder setRepeatedField( com.google.protobuf.Descriptors.FieldDescriptor field, int index, Object value) { return (Builder) super.setRepeatedField(field, index, value); } @Override public Builder addRepeatedField( com.google.protobuf.Descriptors.FieldDescriptor field, Object value) { return (Builder) super.addRepeatedField(field, value); } @Override public Builder mergeFrom(com.google.protobuf.Message other) { if (other instanceof TagInfoProto.TagKafkaInfo) { return mergeFrom(( TagInfoProto.TagKafkaInfo)other); } else { super.mergeFrom(other); return this; } } public Builder mergeFrom( TagInfoProto.TagKafkaInfo other) { if (other == TagInfoProto.TagKafkaInfo.getDefaultInstance()) return this; if (!other.getName().isEmpty()) { name_ = other.name_; onChanged(); } if (!other.getDescribe().isEmpty()) { describe_ = other.describe_; onChanged(); } if (!other.getValue().isEmpty()) { value_ = other.value_; onChanged(); } if (!other.getTime().isEmpty()) { time_ = other.time_; onChanged(); } this.mergeUnknownFields(other.unknownFields); onChanged(); return this; } @Override public final boolean isInitialized() { return true; } @Override public Builder mergeFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { TagInfoProto.TagKafkaInfo parsedMessage = null; try { parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); } catch (com.google.protobuf.InvalidProtocolBufferException e) { parsedMessage = ( TagInfoProto.TagKafkaInfo) e.getUnfinishedMessage(); throw e.unwrapIOException(); } finally { if (parsedMessage != null) { mergeFrom(parsedMessage); } } return this; } private Object name_ = ""; /** * <code>string name = 1;</code> */ public String getName() { Object ref = name_; if (!(ref instanceof String)) { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); name_ = s; return s; } else { return (String) ref; } } /** * <code>string name = 1;</code> */ public com.google.protobuf.ByteString getNameBytes() { Object ref = name_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (String) ref); name_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } /** * <code>string name = 1;</code> */ public Builder setName( String value) { if (value == null) { throw new NullPointerException(); } name_ = value; onChanged(); return this; } /** * <code>string name = 1;</code> */ public Builder clearName() { name_ = getDefaultInstance().getName(); onChanged(); return this; } /** * <code>string name = 1;</code> */ public Builder setNameBytes( com.google.protobuf.ByteString value) { if (value == null) { throw new NullPointerException(); } checkByteStringIsUtf8(value); name_ = value; onChanged(); return this; } private Object describe_ = ""; /** * <code>string describe = 2;</code> */ public String getDescribe() { Object ref = describe_; if (!(ref instanceof String)) { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); describe_ = s; return s; } else { return (String) ref; } } /** * <code>string describe = 2;</code> */ public com.google.protobuf.ByteString getDescribeBytes() { Object ref = describe_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (String) ref); describe_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } /** * <code>string describe = 2;</code> */ public Builder setDescribe( String value) { if (value == null) { throw new NullPointerException(); } describe_ = value; onChanged(); return this; } /** * <code>string describe = 2;</code> */ public Builder clearDescribe() { describe_ = getDefaultInstance().getDescribe(); onChanged(); return this; } /** * <code>string describe = 2;</code> */ public Builder setDescribeBytes( com.google.protobuf.ByteString value) { if (value == null) { throw new NullPointerException(); } checkByteStringIsUtf8(value); describe_ = value; onChanged(); return this; } private Object value_ = ""; /** * <code>string value = 3;</code> */ public String getValue() { Object ref = value_; if (!(ref instanceof String)) { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); value_ = s; return s; } else { return (String) ref; } } /** * <code>string value = 3;</code> */ public com.google.protobuf.ByteString getValueBytes() { Object ref = value_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (String) ref); value_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } /** * <code>string value = 3;</code> */ public Builder setValue( String value) { if (value == null) { throw new NullPointerException(); } value_ = value; onChanged(); return this; } /** * <code>string value = 3;</code> */ public Builder clearValue() { value_ = getDefaultInstance().getValue(); onChanged(); return this; } /** * <code>string value = 3;</code> */ public Builder setValueBytes( com.google.protobuf.ByteString value) { if (value == null) { throw new NullPointerException(); } checkByteStringIsUtf8(value); value_ = value; onChanged(); return this; } private Object time_ = ""; /** * <code>string time = 4;</code> */ public String getTime() { Object ref = time_; if (!(ref instanceof String)) { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; String s = bs.toStringUtf8(); time_ = s; return s; } else { return (String) ref; } } /** * <code>string time = 4;</code> */ public com.google.protobuf.ByteString getTimeBytes() { Object ref = time_; if (ref instanceof String) { com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8( (String) ref); time_ = b; return b; } else { return (com.google.protobuf.ByteString) ref; } } /** * <code>string time = 4;</code> */ public Builder setTime( String value) { if (value == null) { throw new NullPointerException(); } time_ = value; onChanged(); return this; } /** * <code>string time = 4;</code> */ public Builder clearTime() { time_ = getDefaultInstance().getTime(); onChanged(); return this; } /** * <code>string time = 4;</code> */ public Builder setTimeBytes( com.google.protobuf.ByteString value) { if (value == null) { throw new NullPointerException(); } checkByteStringIsUtf8(value); time_ = value; onChanged(); return this; } @Override public final Builder setUnknownFields( final com.google.protobuf.UnknownFieldSet unknownFields) { return super.setUnknownFieldsProto3(unknownFields); } @Override public final Builder mergeUnknownFields( final com.google.protobuf.UnknownFieldSet unknownFields) { return super.mergeUnknownFields(unknownFields); } // @@protoc_insertion_point(builder_scope:TagKafkaInfo) } // @@protoc_insertion_point(class_scope:TagKafkaInfo) private static final TagInfoProto.TagKafkaInfo DEFAULT_INSTANCE; static { DEFAULT_INSTANCE = new TagInfoProto.TagKafkaInfo(); } public static TagInfoProto.TagKafkaInfo getDefaultInstance() { return DEFAULT_INSTANCE; } private static final com.google.protobuf.Parser<TagKafkaInfo> PARSER = new com.google.protobuf.AbstractParser<TagKafkaInfo>() { @Override public TagKafkaInfo parsePartialFrom( com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { return new TagKafkaInfo(input, extensionRegistry); } }; public static com.google.protobuf.Parser<TagKafkaInfo> parser() { return PARSER; } @Override public com.google.protobuf.Parser<TagKafkaInfo> getParserForType() { return PARSER; } @Override public TagInfoProto.TagKafkaInfo getDefaultInstanceForType() { return DEFAULT_INSTANCE; } } private static final com.google.protobuf.Descriptors.Descriptor internal_static_TagKafkaInfo_descriptor; private static final com.google.protobuf.GeneratedMessageV3.FieldAccessorTable internal_static_TagKafkaInfo_fieldAccessorTable; public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() { return descriptor; } private static com.google.protobuf.Descriptors.FileDescriptor descriptor; static { String[] descriptorData = { "n22TagInfoProto.proto"Kn14TagKafkaInfo2214n04" + "name3001 01(t2220n10describe3002 01(t22rn05value3003 " + "01(t2214n04time3004 01(tB'n27bonyear.mes.kafka.p" + "rotoB14TagInfoProtob06proto3" }; com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { public com.google.protobuf.ExtensionRegistry assignDescriptors( com.google.protobuf.Descriptors.FileDescriptor root) { descriptor = root; return null; } }; com.google.protobuf.Descriptors.FileDescriptor .internalBuildGeneratedFileFrom(descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] { }, assigner); internal_static_TagKafkaInfo_descriptor = getDescriptor().getMessageTypes().get(0); internal_static_TagKafkaInfo_fieldAccessorTable = new com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( internal_static_TagKafkaInfo_descriptor, new String[] { "Name", "Describe", "Value", "Time", }); } // @@protoc_insertion_point(outer_class_scope) }

这个为自定义kafka的反序列化。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import com.google.protobuf.ListValue; import com.google.protobuf.Value; import org.apache.kafka.common.errors.SerializationException; import org.apache.kafka.common.serialization.Deserializer; import java.util.List; import java.util.Map; public class MyDeserializer implements Deserializer< TagInfoProto.TagKafkaInfo> { @Override public void configure(Map<String, ?> configs, boolean isKey) { } @Override public TagInfoProto.TagKafkaInfo deserialize(String topic, byte[] data) { TagInfoProto.TagKafkaInfo t = null; try { if (data == null) return null; if (data.length < 1000) throw new SerializationException("Size of data received by IntegerDeserializer is shorter than expected"); ListValue list = ListValue.parseFrom(data); List<Value> lists = list.getValuesList(); for (Value lis:lists){ TagInfoProto.TagKafkaInfo b = TagInfoProto.TagKafkaInfo.parseFrom(lis.toByteArray()); return b ; } } catch (Exception e) { throw new SerializationException("Error when serializing Customerto byte[] " + e); } return null; } @Override public void close() { // nothing to do } }

pom文件。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?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>www.byt.com</groupId> <artifactId>KafkaToHbase</artifactId> <version>1.0-SNAPSHOT</version> <properties> <scala.version>2.11.8</scala.version> <hadoop.version>2.7.4</hadoop.version> <spark.version>2.0.0</spark.version> <hbase.version>2.1.5</hbase.version> <hadoop.version>2.7.7</hadoop.version> <kafka.version>2.12</kafka.version> </properties> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming-flume_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming-kafka-0-10_2.11</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>${hbase.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka --> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.12</artifactId> <version>2.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java --> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-io</artifactId> <version>1.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.2</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> <configuration> <args> <arg>-dependencyfile</arg> <arg>${project.build.directory}/.scala_dependencies</arg> </args> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>Test</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>

这是我spark任务提交命令。

复制代码
1
2
3
4
5
bin/spark-submit --class Test --master spark://data09:7077 /opt/spark-2.0.0-bin-hadoop2.7/examples/jars/KafkaToHbase-1.0-SNAPSHOT.jar

spark集群的protobuf包为2.5.0,已经被我删除。

集群上spark版本为2.0.0,hbas版本为1.2.11,scala为2.11.8。

另外一点,是从一个spark集群上操作另一个集群上没有spark集群的数据。

在网上找了很多资料很解决办法都没能解决。

最后

以上就是明亮豌豆最近收集整理的关于spark消费protobuf序列化的kafka数据存到hbase的全部内容,更多相关spark消费protobuf序列化内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部