Example #1
0
	function request() {
		//ajaxリクエスト以外
		if( !$this->RequestHandler->isAjax() ) {
			$this->set("result" , array("result" => "not ajax"));
			return;
		}

		//パラメータ解析
		$u_userid=$this->params['form']['u'];
		$u_id=$this->params['form']['id'];
		if ( $u_userid == "" or $u_id == "" ){
			$this->set("result" , array("result" => "parameter error"));
			return;
		}

		//ディレクトリ
		if ( CommonComponent::MakeDirectory($u_userid) != 0 ){
			$this->set("result" , array("result" => "create directory error"));
			return;
		}

		//SQL文取得
		$u_query="";
		$hql_file=DIR_REQUEST."/${u_userid}/${u_id}.hql";
		if ( !($fp=fopen($hql_file,"r")) ){
			$this->set("result" , array("result" => "file open error"));
			return;
		}
		while(!feof($fp)){
			$data = fgets($fp, 512);
			$u_query.=$data;
		}
		fclose($fp);

		//SQLファイルを書き換える(select文の前にselect文のコメントを挿入)
		if ( !($fp=fopen($hql_file,"w")) ){
			$this->set("result" , array("result" => "file open error", "id" => "$u_id"));
			return;
		}
		$arr=preg_split("/;/",$u_query);
		for ($i=0; $i<count($arr); $i++){
			$arr[$i]=str_replace(array("\r\n","\n","\r","\t"), ' ', $arr[$i]);
			$arr[$i]=ltrim($arr[$i]);
			if ( $arr[$i] == "" ){ continue; }
			if ( eregi('^--',$arr[$i]) ){
				$ret=fputs($fp,"$arr[$i]\n");
			}else{
				if ( eregi('^select',$arr[$i]) ){
					$ret=fputs($fp,"--$arr[$i]\n");
				}
				$ret=fputs($fp,"$arr[$i];\n");
			}
		}
		fclose($fp);

		//クエリの実行制限チェック
		list($res,$hive_database)=CommonComponent::HiveBefore($u_userid,$u_query);	
		if ( $res != 0 ){
			$this->set("result" , array("result" => "許可されていないクエリです"));
			return;
		}

		//同時実行数制限
		$run_cnt=CommonComponent::GetQueryExecuteNum();	
		//$this->log("CNT=$run_cnt",LOG_DEBUG);
		if ( $run_cnt >= WEBHIVE_MAX_REQUEST ){
			$this->set("result" , array("result" => "クエリ実行数が制限を超えました。しばらくたってから再実行してください"));
			return;
		}

		//クエリ実行履歴出力
		$runlog['Runhists']['username']=$u_userid;
		$runlog['Runhists']['hive_database']=$hive_database;
		$runlog['Runhists']['query']=$u_query;
		$runlog['Runhists']['rid']=$u_id;
		$runlog['Runhists']['rsts']=0;
		if ( !($this->Runhists->save($runlog, array('username','hive_database','query','rid','rsts') )) ){
			$this->set("result" , array("result" => "db access error"));
			return;
		}
		$runlog['Runhists']['id'] = $this->Runhists->getLastInsertID();

		//クエリ監査ログ出力
		CommonComponent::QueryAuditLogWrite($u_userid,$u_query);

		//HiveQLのバックグラウンド実行
		$cmd=CMD_PHP . " " . CMD_HIVE_SHELL . " $u_userid $u_id"; 
		$this->log("CMD=$cmd",LOG_DEBUG);
		exec("$cmd > /dev/null 2>&1 &",$result,$retval);
		$this->log("CMD=$cmd => $retval",LOG_DEBUG);
		$this->set("result" , array("result" => "ok", "id" => "$u_id"));
	}
	function fileupload() {

		//$this->log($this->params,LOG_DEBUG);
		$this->layout = "ajax";

		//異常チェック
		if ( empty($this->params['form']['DataUploadText']) or empty($this->params['form']['DataUploadUserid'])){
			$this->set("result" , array("success" => false,'msg'=>'データが未入力です。'));
			return;
		}
		if ( $this->params['form']['DataUploadText']=="" or $this->params['form']['DataUploadUserid']=="" ){
			$this->set("result" , array("success" => false,'msg'=>'データが未入力です。'));
			return;
		}

		if ( HIVE_DATABASE_UPLOAD == "" ){
			$this->set("result" , array("success" => false,'msg'=>'データップロード機能は利用できません。'));
			return;
		}

		//各種パラメータ設定
		$u_arr=split("\n",$this->params['form']['DataUploadText']);
		$u_userid=$this->params['form']['DataUploadUserid'];
		$u_id=sprintf("%s_%05d",date("YmdHis"),getmypid());
		$u_database=HIVE_DATABASE_UPLOAD;
		$u_table="${u_userid}_${u_id}";
		$tmp_dir = DIR_UPLOAD . "/$u_userid/${u_id}";
		$tmp_file = DIR_UPLOAD . "/${u_id}.txt";
		$hql_file = DIR_REQUEST . "/$u_userid/${u_id}.hql";

		//一時ファイル名
		if ( CommonComponent::MakeDirectory($u_userid,$u_id) != 0 ){
			$this->set("result" , array("success" => false,'msg'=>'ディレクトリ作成に失敗しました。'));
			return;
		}

		//クエリ実行履歴出力
		$u_query="use $u_database;\nLOAD DATA LOCAL INPATH '$tmp_file' OVERWRITE INTO TABLE $u_table;\n";
		$runlog['Runhists']['username']=$u_userid;
		$runlog['Runhists']['hive_database']=$u_database;
		$runlog['Runhists']['query']=$u_query;
		$runlog['Runhists']['rid']=$u_id;
		$runlog['Runhists']['rsts']=0;
		//$this->log($runlog,LOG_DEBUG);
		if ( !($this->Runhists->save($runlog, array('username','hive_database','query','rid','rsts') )) ){
			$this->set("result" , array("success" => false,'msg'=>'db access error'));
			return;
		}
		$runlog['Runhists']['id'] = $this->Runhists->getLastInsertID();

		//一時ファイル出力
		$column_cnt=0;
		$line_cnt=0;
		if ( !($fp=fopen($tmp_file,"w")) ){
			$this->set("result" , array("success" => false,'msg'=>'file open error'));
			CommonComponent::UpdateRunhistsResult($u_id,513);
			return;
		}
		for($i=0; $i<count($u_arr); $i++){
			//カラム数
			if ( $i == 0 ){
				$lines=split("\t",$u_arr[$i]);
				$column_cnt=count($lines);
			}

			fputs($fp,$u_arr[$i] . "\n");
			$line_cnt++;
		}
		fclose($fp);
		$this->log("$tmp_file => LINE=$line_cnt COLUMN=$column_cnt",LOG_DEBUG);

		//一時テーブル作成クエリ
		if ( !($fp=fopen($hql_file,"w")) ){
			$this->set("result" , array("success" => false,'msg'=>'file open error'));
			CommonComponent::UpdateRunhistsResult($u_id,514);
			return;
		}
		fputs($fp,"use $u_database;");
		fputs($fp,"create table $u_table (");
		for($i=1; $i<=$column_cnt; $i++){
			if ( $i == 1 ){
				$wk=sprintf("c%03d string",$i);
			}else{
				$wk=sprintf(",c%03d string",$i);
			}
			fputs($fp,$wk);
		}
		fputs($fp,") ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' STORED AS TEXTFILE;");
		fclose($fp);

		//一時テーブル作成
		$cmd=CMD_HIVE." -f $hql_file";
		$this->log("CMD=$cmd",LOG_DEBUG);
		exec("$cmd 2>&1",$result,$retval);
		$this->log("CMD=$cmd => $retval",LOG_DEBUG);
		if ( $retval != 0 ){
			$this->set("result" , array("success" => false,'msg'=>"一時テーブルの作成に失敗しました。"));
			CommonComponent::UpdateRunhistsResult($u_id,511);
			return;
		}

		//HiveQLファイル作成(監査ログ)
		if ( !($fp=fopen($hql_file,"w")) ){
			$this->set("result" , array("success" => false,'msg'=>'file open error'));
			CommonComponent::UpdateRunhistsResult($u_id,514);
			return;
		}
		fputs($fp,"$u_query");
		fclose($fp);

		//Load DATAの実行
		$cmd=CMD_HIVE." -f $hql_file";
		$this->log("CMD=$cmd",LOG_DEBUG);
		exec("$cmd > /dev/null 2>&1",$result,$retval);
		$this->log("CMD=$cmd => $retval",LOG_DEBUG);
		if ( $retval != 0 ){
			$this->set("result" , array("success" => false,'msg'=>"DATA LOADが失敗しました。"));
			CommonComponent::UpdateRunhistsResult($u_id,516);
			return;
		}

		CommonComponent::UpdateRunhistsResult($u_id,200);
		$msg="データ投入が正常終了しました。<br><br>${u_database}.${u_table}<br>";
		$this->set("result" , array("success" => true, 'msg'=>"$msg"));
	}