1. 程式人生 > >將java RDD結果寫入Hive表中

將java RDD結果寫入Hive表中

情況一:只需插入一列

JavaRDD<String> titleParticiple = ....;

/**

 * 將分詞結果儲存到Hive表,供資料探查使用
 * */
   HiveContext hiveCtx = new HiveContext(jsc);
   SQLContext sqlCtx = new SQLContext(jsc);
/**
         * 在RDD的基礎上建立型別為Row的RDD, 
         */  
        JavaRDD<Row> brandRDD  = titleParticiple.map(new Function<String, Row>() {  


            private static final long serialVersionUID = 1L;  

            public Row call( String line )  
                throws Exception {  
                return RowFactory.create(line);  
            }  
        });  
        /** 
         *1、 動態的構建DataFrame的元資料,一般而言,有多少列以及酶類的具體型別可能來源於JSON檔案或者資料庫 
         */  

        List<StructField> structFields = new ArrayList<StructField>();  
        //structFields.add(DataTypes.createStructField( "id", DataTypes.IntegerType, true ));
        structFields.add(DataTypes.createStructField( "brand", DataTypes.StringType, true )); 
        /** 
         * 2、構建StructType用於DataFrame 元資料的描述 

         *  
         */  
        StructType structType = DataTypes.createStructType( structFields );  
        /** 
         * 3、基於MeataData以及RDD<Row>來構造DataFrame 
         */  
        Dataset<Row> personsDF = sqlCtx.createDataFrame(brandRDD,structType);  
        /** 
          * 4、註冊成為臨時表以供後續的SQL查詢操作 
          */  
        personsDF.registerTempTable("brands"); 
        hiveCtx.sql("use sousuo");  //使用sousuo資料庫
        hiveCtx.sql("drop table if exists sousuo.temp_yeqingyun_20170913");//刪除原來的表
        hiveCtx.sql("CREATE TABLE IF NOT EXISTS sousuo.temp_yeqingyun_20170913 (brand STRING)");//建立表

        hiveCtx.sql("insert into sousuo.temp_yeqingyun_20170913 select brand from brands");//將brands表中的內容全部拷貝到temp_yeqingyun_20170913表中

情況二:需要插入多列,且插入的型別有int和String:

JavaPairRDD<String, String> brandTypeGoodsPair = “...”;

HiveContext hiveCtx = new HiveContext(jsc);

 SQLContext sqlCtx = new SQLContext(jsc);

JavaRDD<Row> brandRDD  = brandTypeGoodsPair.map(new Function<Tuple2<String, String>, Row>() {  
              private static final long serialVersionUID = 1L;  
              int i=0;
              public Row call( Tuple2<String, String> pair) throws Exception {  
                i++;
                String[] valueArray = pair._2().split(":");
                String value0 = valueArray[0];
                int value1 = Integer.parseInt(valueArray[1]);
                  return RowFactory.create(i, pair._1, value0, value1);  
              }  
        }); 
        
List<StructField> structFields = new ArrayList<StructField>();  
structFields.add(DataTypes.createStructField( "id", DataTypes.IntegerType, true )); 
        structFields.add(DataTypes.createStructField( "directory3", DataTypes.StringType, true ));
        structFields.add(DataTypes.createStructField( "brandItemModel", DataTypes.StringType, true )); 
        structFields.add(DataTypes.createStructField( "num", DataTypes.IntegerType, true )); 
        StructType structType = DataTypes.createStructType( structFields );   
        Dataset<Row> brandDF = sqlCtx.createDataFrame(brandRDD,structType);
        brandDF.registerTempTable("brands_test2"); 
        hiveCtx.sql("use sousuo");
        hiveCtx.sql("drop table if exists sousuo.temp_yeqingyun_test2_20170913");
        hiveCtx.sql("CREATE TABLE IF NOT EXISTS sousuo.temp_yeqingyun_test2_20170913 (id INT, directory3 STRING, brandItemModel STRING, num INT)");
        hiveCtx.sql("insert into sousuo.temp_yeqingyun_test2_20170913 select id,directory3,brandItemModel,num from brands_test2");

相關推薦

java RDD結果寫入Hive

情況一:只需插入一列 JavaRDD<String> titleParticiple = ....; /**  * 將分詞結果儲存到Hive表,供資料探查使用 * */   HiveContext hiveCtx = new HiveContext(j

hive模糊查詢結果寫入分割槽

需求:只保留表1中12月27號的資料  其他日期的都不要 然後將結果又放回表1  最開始我是這種思路 :        ~設定開啟動態分割槽開關   set hive.exec.dynamic.partition

Java實現把測試結果寫入Excel

自動化測試指令碼執行完以後,會有一個測試結果,無論是通過框架還是Jenkins平臺生成的結果,可讀性都不是太好,為了方便手工測試人員檢視結果,測試完成後把結果寫入Excel是一個不錯的方法,但是指令碼多了,通過人工來寫,無疑加重了自動化測試人員的工作,為了解決這個問題,我

Hive 實戰練習(一)—按照日期每天的資料匯入Hive

需求:         每天會產生很多的日誌檔案資料,有這麼一種需求:需要將每天產生的日誌資料在晚上12點鐘過後定時執行操作,匯入到Hive表中供第二天資料分析使用。要求建立分割槽表,並按照日期分割槽。資料檔案命名是以當天日期命名的,如2015-01-09.txt一、建立分割

使用spark記憶體的資料寫入hive

使用spark將記憶體中的資料寫入到hive表中 hive-site.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/xsl" href="configurati

查詢結果插入到

not 創建 lis default inf pre police span signed 語法:INSERT INTO table_name1 (column_list) SELECT (column_list2) FROM table_name2 WHE

利用sqoop指定列指定條件的方式資料從mysql增量匯入hive

========1、sqoop增量(指定列指定條件的方式增量匯入hive表中)匯入hive指令碼======= #!/bin/bash #Set the RDBMS connection params rdbms_ip=$1 rdbms_connect="jdbc:mysq

使用shellhdfs上的資料匯入到hive

days=($(seq 20150515 20150517)) hours=() for (( i=0; i<=23;++i)) do if [ $i -lt 10 ]; then

python檔案讀寫(從file1讀出資料並計算,然後結果寫入到file2

要求新建兩個檔案,file1、file2,要求開啟file1檔案,分別對每一行數字進行求和,並將每一行的結果寫在file2中。 file1: 20 30 40 20 52 63 52 52 85 52 8 456 522 25 36 85 96 74 程式原始碼: 定義一個求和函式

用sqoopmysql的資料匯入到hive,原理分析

Sqoop 將 Mysql 的資料匯入到 Hive 中 準備Mysql 資料 如圖所示,準備一張表,資料隨便造一些,當然我這裡的資料很簡單。 編寫命令 編寫引數檔案 個人習慣問題,我喜歡把引數寫到檔案裡,然後再命令列引用。 vim mysql-info, #

用sqoopmysql的資料匯入到hive

用sqoop將mysql的資料匯入到hive表中 1:先將mysql一張表的資料用sqoop匯入到hdfs中 準備一張表    需求 將 bbs_product 表中的前100條資料導 匯出來  只要id  brand_id和 na

查詢MDB高程點的高程值有0值的圖幅(用遊標遍歷查詢某個欄位的值),並查到的結果寫入到TXT

1、 mdbs = arcpy.ListWorkspaces("*","Access") 2、 FeatureClasses = arcpy.ListFeatureClasses() 3、 Fields = arcpy.ListFields(FeatureClass) 4、 cursor =

查找MDB高程點的高程值有0值的圖幅(用遊標遍歷查找某個字段的值),並查到的結果寫入到TXT

name addm open ces pat message back ext put 1、 mdbs = arcpy.ListWorkspaces("*","Access") 2、 FeatureClasses = arcpy.ListFeatureClasses()

JAVA類實現從hdfs匯入資料到hive

// 需要引入 hadoop & hive jar import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import ja

Spark計算結果寫入到Mysql

今天主要來談談如果將Spark計算的結果寫入到Mysql或者其他的關係型資料庫裡面。其實方式也很簡單,程式碼如下: 01 /** 02 * User: 過往記憶

劍指Offer面試題15(Java版):鏈倒數第K個結點

head 計數器 easy sta 相同 ret white style 輸出 題目: 輸入一個鏈表。輸出該鏈表中倒數第k哥結點。 為了符合大多數人的習慣,本題從1開始計數。即鏈表的尾結點是倒數第1個結點。 比如一個鏈表有6個結點。從頭結點開始它們的值依次是1。2。

使用spark對hive的多列數據判重

個數 stack duplicate house transient this dataframe except cti 本文處理的場景如下,hive表中的數據,對其中的多列進行判重deduplicate。 1、先解決依賴,spark相關的所有包,pom.xml spa

flume的sink寫入hive

ket support ted ets conf netca cti ren mem a1.sources = r1 a1.sinks = s1 a1.channels = c1 a1.sources.r1.type = netcat

hibernate使用setResultTransformer()SQL查詢結果放入集合

-h cal 們的 gpo ict dds eas find ans 在平時開發中Hibernate提供的hql基本能夠滿足我們的日常需求。但是在有些特殊的情況下,還是需要使用原生的sql,並且希望sql查詢出來的結果能夠綁定到pojo上。hibernate API中的cr

python接口測試-運行結果寫入Excel表格

png 程序實現 我沒 bsp image php 生產力 優化 接口 公司工作是促進學習的第一生產力!! 一個get請求的接口,我想清楚的在Excel中看到所有的數據! 帶著學過H5,php覺得所有代碼都很簡單的自信,在公司開發的【鼓勵】下開始了一上午的鬥爭 一個小時