ビッグデータオフラインプロジェクトケース

オフラインプロジェクトには、合計で次の部分が含まれ
ます。1.データ前処理段階
2.データウェアハウジング操作段階
3.データ分析段階
4.データベース段階へのデータ保存
5.データクエリおよび表示段階
は、データ形式の表とデータ例を提供します、最初にデータの説明を読んでから、対応する質問を行ってください。

ここに画像の説明を挿入
元のデータ:
qR8WRLrO2aQ:mienge:406:People&Blogs:599:2788:5:1:0:4UUEKhr6vfA:zvDPXgPiiWI:TxP1eXHJQ2Q:k5Kb1K0zVxU:hLP_mJIMqYXd :eyZOjktUb5M:Dv15_9gnM2A:lMQydgG1N2k:U0gZppW_ -2Y:dUVU6xpMc6Y:ApA6VEYI8zQ:a3_boc9Z_Pc:N1z4tYob0hM:2UJkU2neoBsの
:データ前処理後
qR8WRLrO2aQ:mienge:406:ブログと人:599:2788 :. 5 :. 1:0:4UUEKhr6vfA、zvDPXgPiiWI、TxP1eXHJQ2Q、 k5Kb1K0zVxU、hLP_mJIMNFg、tzNRSSTGF4o、BrUGfqJANn8、OVIc-mNxqHc、gdxtKvNiYXc、bHZRZ - QK 1A、GUJdU6uHyzU、eyZOjktUb5M、Dv15_9gnM2A、lMQydgG1N2k、U0gZppW_-2Y、dUVU6xpMc6Y、ApA6VEYI8zQ、a3_boc9Z_Pc、N1z4tYob0hM、2UJkU2neoBs
1、生データを前処理します。形式は、上記の前処理後のサンプルデータです。

元のデータフォームを観察すると、各フィールド間で「:」セグメンテーションが使用され、動画に複数の動画カテゴリがあり、アンパサンドがカテゴリ間で分割され、セグメンテーションの両側にスペース文字があることがわかります。同時に、関連する動画にもさらに多くのA、複数の関連動画も「:」で区切られています。データ分析を容易にするために、最初にデータの再編成とクリーニング操作を実行します。
つまり、各データのカテゴリを「&」で分割し、両側のスペースを削除し、「、」を使用して複数の「関連するビデオID」を分割します。

map:
public class Map extends Mapper <LongWritable、Text、NullWritable、Text> {

@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

    String trim = Utill(value.toString());

    String s = "";
    String ss = "";

    if (value.toString().split(":").length>=10){
        String[] split = trim.split(":");
        for (int i = 9; i < split.length; i++) {
            s += split[i]+",";
            if (i == split.length-1){
                s += split[i];
            }
        }
        if (split.length>= 10) {
            String[] sp = trim.split(split[9]);
            ss = sp[0] + s;
        }
    }
    context.write(NullWritable.get(),new Text(ss));

}
public static String Utill(String s){

    String trim = "";

    if (s.contains("&")){
        String[] split = s.split("&");
        for (String ss : split) {
            String t = ss.trim();
            trim += t+",";
        }
        return trim.substring(0,trim.lastIndexOf(","));
    }
    return "";
        }
 }

Reduce:
パブリッククラスReduceはReducer <NullWritable、Text、NullWritable、Text> {を拡張します

@Override
protected void reduce(NullWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    for (Text value : values) {
        if (value.getLength() != 0) {
            context.write(NullWritable.get(), value);
        }
    }
}

}

Driver:
public class Driver { public static void main(String [] args)throws IOException、ClassNotFoundException、InterruptedException {

    Configuration conf = new Configuration();

    Job job = Job.getInstance(conf, "Driver");

    job.setInputFormatClass(TextInputFormat.class);
    TextInputFormat.addInputPath(job,new Path("hdfs://node1:8020/video.txt"));

    job.setMapperClass(Map.class);
    job.setMapOutputKeyClass(NullWritable.class);
    job.setMapOutputValueClass(Text.class);

    job.setReducerClass(Reduce.class);
    job.setOutputKeyClass(NullWritable.class);
    job.setOutputValueClass(Text.class);

    job.setOutputFormatClass(TextOutputFormat.class);
    TextOutputFormat.setOutputPath(job,new Path("hdfs://node1:8020/b"));

    int b = job.waitForCompletion(true)?0:1;

    System.exit(b);
}
}

2.前処理されたデータをハイブに格納します
2.1データベースとテーブルを
作成しますデータベース名を作成します:ビデオ
元のデータテーブルを作成します:
ビデオテーブル:video_oriユーザーテーブル:video_user_ori
ORCフォーマットテーブルを作成します:
ビデオテーブル:video_orcユーザーテーブル:Video_user_orc

元のテーブル
作成ステートメントがある場合は、video_oriビデオテーブルを

作成します。createtable video_ori(
videoId string、
uploader string、
age int、
category array、
length int、
views int、
rate float、
ratings int、
comments int、
relatedId array)
行フォーマット区切り
フィールドが終了しました「:」によって
テキストファイルとして保存されます。

创建video_user_ori用户表
作成テーブルvideo_user_ori(
アップローダー文字列、
ビデオ整数、
フレンド整数)
行フォーマット区切り
フィールド
、テキストファイルとして保存される「、」で終了

テーブル
作成ステートメントをORC形式で記述してください:create video_orc table

create table video_orc(
videoId string、
uploader string、
age int、
category array、
length int、
views int、
rate float、
ratings int、
comments int、
relatedId array)
row format delimited
「:」で終了するフィールド
はorcとして保存されます。

创建video_user_orc表:
テーブルを作成しますvideo_user_orc(
アップローダー文字列、
ビデオ整数、
フレンド整数)
行フォーマット区切り
フィールドは、「、」で終了し、
orcとして保存されます。

2.2前処理されたビデオデータを元のテーブルvideo_oriにインポートし、元のユーザーテーブルのデータをvideo_user_oriにインポートします。
インポートステートメント
video_orcを記述してください:
テーブルに挿入video_orc select * from video_ori;

video_user_orc:
テーブルに挿入video_user_orc select * from video_user_ori;

3.保存後にデータに対してhivesqlクエリ操作を実行します
3.1ビデオテーブルからビデオスコア5のビデオ情報をカウントし、クエリ結果を/export/rate.txt
から/ opt / lianxiに保存します。sql
ステートメントを記述してください:

hive -e“ select * from video.video_orc where rate = 5;”を選択 > /opt/lianxi/rate.txt

3.2ビデオテーブルから100件を超えるコメントを含むビデオ情報をカウントし、クエリ結果を/export/comments.txt
から/ opt / lianxi
保存します。sql ステートメントを記述してください:

hive -e“ select * from video.video_orc whereコメント> 100;”を選択します。> /opt/lianxi/comments.txt

置換:
sed -e's /] // g 'a> b
sed -e's / [// g' b> c
sed -e's / "// g 'c> d
4.ハイブの分析データはhbase
4.1に保存されます4.1テーブル
作成のステートメントレート外部テーブル作成するためのステートメント:外部テーブル作成するステートメント
rate(
videoId string、
uploader string、
age string、
category string、
length string、
views string、
rate string、
ratings string、
comments string、
relatedId文字列)テキストファイルとして保存された「\ t」で終了する
行フォーマット区切り
フィールド

创建コメント外部表的语句:
(外部表のコメント作成
VIDEOID文字列、
文字列、アップローダー
年齢列、
カテゴリ文字列、
長さの文字列、
ビュー列、
レート列、
評価文字列、
コメント文字列、
relatedId文字列)
行形式区切り
「で終わるフィールドを\ t”
をテキストファイルとして保存。

4.2
数据加载语句
負荷データテーブル速度にローカルINPATH「/opt/5.txt」。
ローカルローカルパス「/opt/100.txt」をテーブルコメントにロードします。

4.3
创建hbase映射表
作成テーブルvideo.hbase_rate(
videoId string、
uploader string、
age int、
category array、
length int、
views int、
rate float、
ratings int、
comments int、
relatedId array))
によって保存された 'org.apache.hadoop .hive.hbase.HBaseStorageHandler '
with serdeproperties(“ hbase.columns.mapping” =“ cf:uploader、cf:age、cf:category、cf:length、cf:views、cf:rate、cf:ratings、cf:comments 、cf:relatedId”)
tblproperties(“ hbase.table.name” =“ hbase_rate”);

'org.apache.hadoop.hive.hbase.HBaseStorageHandlerによって保存されたテーブルvideo.hbase_comments(
videoId文字列、
アップローダー文字列、
年齢整数、
カテゴリ配列、
長さ整数、
ビュー整数、
レート浮動小数点、
評価整数、
コメント整数、
関連ID配列)を作成します
'
with serdeproperties(“ hbase.columns.mapping” =“ cf:uploader、cf:age、cf:category、cf:length、cf:views、cf:rate、cf:ratings、cf:comments、cf:relatedId”)
tblproperties(“ hbase.table.name” =“ hbase_comments”);

4.4 テーブルに
データを
挿入しますhbase_rate select * from rate;

テーブルに挿入hbase_comments select * fromコメント;

5. hbaseapiによるクエリ操作
5.1 hbaseapiを使用してhbase_rateテーブルをスキャンし、startRowKey = 1およびendRowKey = 100に従って結果をスキャンおよびクエリしてください。
ここに画像の説明を挿入
5.2 hbaseapiを使用してhbase_commentsテーブルをクエリし、コメント列の値のみをクエリしてください。

public class HBase_api { public static void main(String [] args)throws IOException { Configuration conf = new Configuration(); conf.set(“ hbase.zookeeper.quorum”、“ node1:2181、node2:2181、node3:2181”); 接続接続= ConnectionFactory.createConnection(conf); テーブル名= connection.getTable(TableName.valueOf(“ hbase_rate”));




    test2(name);
    connection.close();
}

public static void test2(Table name) throws IOException {
    Scan scan = new Scan();
    ResultScanner scanner = name.getScanner(scan);
    for (Result result : scanner) {
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("comments")){
                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("--------------");
            }
        }
    }
}

}

補足:

ファイルの内容の最初の行を削除します
sed -i '1d' filename
sedコマンドは、指定されたテキストを正規表現に置き換えることができます。基本的なスクリプト形式は、次のとおりです:
sed -e's / string to replace to / new string / g '

おすすめ

転載: blog.csdn.net/Dreamy_zsy/article/details/103858889
おすすめ