hbase sqoop 实验_Hive/hbase/sqoop的基本使用教程~

Hive/hbase/sqoop的基本使用教程~

###Hbase基本命令

start-hbase.sh #启动hbase

hbase shell     #进入hbase编辑命令

list       #列出当前所有的表(tablename)

create ‘test‘,‘name‘        #创建一张名为test的表,并且表中只有一列 name

put ‘test‘,‘row1‘,‘name:zhangsan‘,‘zs‘       #往test表中插入数据,行标识为row1

put ‘test‘,‘row2‘,‘name:lisi‘,‘ls‘       #往test表中插入数据,行标识为row2

插入数据时,行标识可以相同,例如:

put ‘test‘,‘row1‘,‘name:zhangsan‘,‘zs‘

put ‘test‘,‘row1‘,‘name:zhangsan1‘,‘zs1‘     #两条数据都存入数据库

如果行标识相同,列数据也相同时 后面的value值会被覆盖

put ‘test‘,‘row1‘,‘name:zhangsan‘,‘zs‘

put ‘test‘,‘row1‘,‘name:zhangsan‘,‘ls‘    #只会有一条数据,value值为ls的存入数据库

scan ‘test‘     ##查看当前test表中数据信息

get ‘test‘,‘row1‘     #查看test表中行标识为row1的数据信息

delete ‘test‘,‘row1‘,‘name:zhangsan‘     #删除行标识为row1,name:zhangsan 的数据

disable ‘test‘     #禁用test表

drop ‘test‘   #删除test表

exit    #退出hbase shell

#HIVE UDF 用户自定义函数

1、创建maven项目,pom.xml引入如下依赖:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.imodule.hive

hivedemo

1.0

jar

jdk.tools

jdk.tools

1.8

system

${JAVA_HOME}/lib/tools.jar

org.apache.hadoop

hadoop-common

2.7.7

org.apache.hive

hive-exec

2.3.3

2、编写Java类,继承 org.apache.hadoop.hive.ql.exec.UDF类 #一个类中可以有多个方法,但是方法名必须为 evaluate,可以进行重载

public class HiveApplication extends UDF

package com.imodule.hive.hivedemo;

import org.apache.hadoop.hive.ql.exec.UDF;

import org.apache.hadoop.hive.ql.udf.UDFSin;

import org.apache.hadoop.io.Text;

/**

* Hello world!

*

*/

public class HiveApplication extends UDF

{

public Text evaluate(String text){

if(text != null){

return new Text("hello,mygirl"+text);

}else{

return new Text("hello,mygirl");

}

}

public Text evaluate(String text,String OBJ,int inta){

return new Text(text+OBJ+Integer.valueOf(inta));

}

}

3、在类中定义函数 ##注意方法名一定要为 evaluate ,入参和出参可以自定义

public Text evaluate(String text){

if(text != null){

return new Text("hello,mygirl"+text);

}else{

return new Text("hello,mygirl");

}

}

4、将maven项目打包为jar包

mvn clean  install

5、在linux上输入hive命令,启动hive

***生成临时函数,会话级别的调用

>hive #进入hive

>add jar /home/imodule/hivedemo-1.0.jar; #上传jar包

>create temporary function myfun as ‘com.imodule.hive.hivedemo.HiveApplication‘; #取函数调用的别名 myfun

>select mname ,myfun(mname) from m_user1; #调用函数 ps:调用的时候需要使用数据去一一对应方法的参数类型个数 evaluate(String text)

>select ‘username‘,myfun(‘zs‘) from m_user1;

>select mname ,myfun(mname,mname,mid) from m_user1; #调用函数 ps:调用的时候需要使用数据去一一对应方法的参数类型以及个数 evaluate(String text,String OBJ,int inta)

***生成永久函数,随时都可以调用

>hadoop dfs -mkdir /user/imodule/hive_jars #在hdfs文件系统中创建文件目录 hive_jars

>hadoop dfs -put /home/imodule/hivedemo-1.0.jar /user/imodule/hive_jars/ #将jar上传到hdfs文件系统

>hive #进入hive

>create function myfun as ‘com.imodule.hive.hivedemo.HiveApplication‘ using jar ‘hdfs://SHDCL40111:9000//user/imodule/hive_jars/hivedemo-1.0.jar‘; #引用hdfs文件系统的jar创建永久函数

>select mname ,myfun(mname) from m_user1; #调用函数 ps:调用的时候需要使用数据去一一对应方法的参数类型个数 evaluate(String text)

###hadoop执行jar的命令:

hadoop jar hadoopDemo-2.7.7.jar /input /output_tmp

###sqoop从mysql库中导入数据到hive库

sqoop import --connect jdbc:mysql://10.28.85.148:3306/data_mysql2hive?useSSL=false --username root --password Abcd1234 --table m_user2 -m 1 --hive-import \ #导入hive库命令

--create-hive-table \ #创建hive表命令

--hive-table m_user2 #命名hive库中的表名为 m_user2

###测试sqoop的连接

sqoop list-databases --connect jdbc:mysql://10.28.85.148:3306/data_mysql2hive --username root --password Abcd1234

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值