spark整理数据放入mysql

本文介绍了使用Apache Spark进行数据处理的方法,并详细展示了如何通过两种不同途径——List<Row>与JavaRDD<Row>——将处理后的数据生成表并存入数据库的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spark我还是新手,所有对他不是特别熟悉,但是在整理完数据之后,我需要生成表并且放入我想要的数据,所以发现了两种不同的方式。

1.生成List<Row>

2.JavaRDD<Row>

第一种方式:(连接到我上一篇文章中的生成的Map<Integer,List>)

List<Row> rows = new ArrayList<>();
Iterator<Entry<Integer, List>> itMap = map.entrySet().iterator();
while(itMap.hasNext()){//遍历map
    Entry<Integer, List> entry = itMap.next();
    int key = entry.getKey();
    List list2 = entry.getValue();
    for(int i = 0;i<list2.size();i++){//循环list
        rows.add(RowFactory.create(key,list2.get(i), list2.get(i+1), list2.get(i+2),
        new Timestamp(new java.util.Date().getTime())));//添加需要的字段内容
        i = 3;
    }
}
StructType schema = createSchema();//创建表结构
Dataset<Row> df_target = sparkSession.createDataFrame(rows, schema);
df_target.show();//打印内容
df_target.write().mode(SaveMode.Append).jdbc(properties.getProperty("url_jdbc_master"),"analyze_point", this.properties);
//连接数据库,创建表,添加内容

创建表结构(表中的字段类型与所添加的内容字段类型必须一致):

StructType createSchema() {
StructType ret = new StructType()//表的列名及表的字段类型,字段注释
    .add(new StructField("orgId",DataTypes.IntegerType,true,Metadata.empty()).withComment("orgId"))
    .add(new StructField("morePhoneLogin", DataTypes.IntegerType, true, Metadata.empty()).withComment("xx"))
    .add(new StructField("openAccountMore", DataTypes.IntegerType, true, Metadata.empty()).withComment("xx"))
    .add(new StructField("remoteLogin", DataTypes.IntegerType, true, Metadata.empty()).withComment("xx"))
    .add(new StructField("time", DataTypes.TimestampType, true, Metadata.empty()).withComment("时间"));
return ret;
}


第二种:(dsAccountCount :根据mysql语句从数据库中返回整理的数据,放入数据库中新表中 ,类型为DataSet<Row>

JavaRDD<Row> rdd_target= dsAccountCount.toJavaRDD().map(t2->{
    return RowFactory.create(t2.get(0),t2.get(1),t2.get(2),new Timestamp(new java.util.Date().getTime()));//根据下标来获取内容
});
StructType schema = createSchema();//上面已经贴出生成新表结构样式
Dataset<Row> df_target = sparkSession.createDataFrame(rdd_target, schema);
df_target.show();
df_target.write().mode(SaveMode.Append).jdbc(this.properties.getProperty(PropertyKeys.URL_JDBC_MASTER),"analyze_point_operator_login", this.properties);

hive Logging initialized using configuration in jar:file:/opt/programs/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:522) at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:677) at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:621) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.hadoop.util.RunJar.run(RunJar.java:221) at org.apache.hadoop.util.RunJar.main(RunJar.java:136) Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient at org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance(MetaStoreUtils.java:1523) at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.<init>(RetryingMetaStoreClient.java:86) at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:132) at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:104) at org.apache.hadoop.hive.ql.metadata.Hive.createMetaStoreClient(Hive.java:3005) at org.apache.hadoop.hive.ql.metadata.Hive.getMSC(Hive.java:3024) at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:503) ... 8 more Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccesso
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值