java 数据仓库 代码_dataframe操作hive数据仓库【Java纯代码】

该博客展示了如何使用Java代码与Hive数据仓库进行交互,包括创建表、加载数据、执行JOIN查询以及将结果保存回新的Hive表。
摘要由CSDN通过智能技术生成

package com.bjsxt;

import org.apache.spark.SparkConf;

import org.apache.spark.SparkContext;

import org.apache.spark.api.java.JavaSparkContext;

import org.apache.spark.sql.DataFrame;

import org.apache.spark.sql.Row;

import org.apache.spark.sql.SQLContext;

import org.apache.spark.sql.SaveMode;

import org.apache.spark.sql.hive.HiveContext;

public class CreateDFFromHiveLocalTest {

public static void main(String[] args) {

SparkConf conf =new SparkConf().setAppName("hive").setMaster("local");

JavaSparkContext sc=new JavaSparkContext(conf);

//SQLContext sqlContext=new SQLContext(sc);

//HiveContext是SQLContext的子类

HiveContext hiveContext=new HiveContext(sc);

hiveContext.sql("USE spark");

hiveContext.sql("DROP TABLE IF EXISTS student_infos");

//在hive中创建表student_infos表

hiveContext.sql("CREATE TABLE IF NOT EXISTS student_infos (name STRING,age INT) row format delimited fields terminated by '\t'");

hiveContext.sql("load data local inpath './student_infos' into table student_infos");

hiveContext.sql("DROP TABLE IF EXISTS student_scores");

hiveContext.sql("CREATE TABLE IF NOT EXISTS student_scores (name STRING,score INT) row format delimited fields terminated by '\t'");

hiveContext.sql(

"LOAD DATA "

+ "LOCAL INPATH './student_scores'"

+ "INTO TABLE student_scores"

);

/**

* 生成查询表

*/

DataFrame df = hiveContext.table("student_infos");//第二种读取hive表加载DF的方式

DataFrame goodStudentsDF = hiveContext.sql("SELECT si.name,si.age,ss.score "

+ "FROM student_infos si "

+ "JOIN student_scores ss "

+ "ON si.name=ss.name "

+ "WHERE ss.score>=80"

);

hiveContext.sql("DROP TABLE IF EXISTS good_student_infos");

goodStudentsDF.registerTempTable("goodstudent");

DataFrame result = hiveContext.sql("select * from goodstudent");

result.show();

/**

* 将结果保存在hive表 good_studeng_infos

*/

goodStudentsDF.write().mode(SaveMode.Overwrite).saveAsTable("good_student_infos");

Row[] goodStudentsRows = hiveContext.table("good_student_infos").collect();

for(Row goodStudentsRow: goodStudentsRows ) {

System.out.println(goodStudentsRow);

}

sc.stop();

}

}

269011.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值