MapReduce编程实现txt文件中的内容导入HBase
来源:互联网 发布:淘宝看访客来源 编辑:程序博客网 时间:2024/06/04 12:50
一、创建java项目。
写入代码,如下:
package translate1;import java.io.IOException;import org.apache.hadoop.conf.*;import org.apache.hadoop.fs.Path;import org.apache.hadoop.mapreduce.*;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.lib.input.*;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.io.*;import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.util.Bytes;public class translate1 { public static Job createSubmittableJob(Configuration conf, String[] args)throws IOException { String tableName = args[0]; Path inputDir = new Path(args[1]); @SuppressWarnings("deprecation") Job job = new Job (conf, "hac_chapter2_recipe3"); job.setJarByClass(HourlyImporter.class); FileInputFormat.setInputPaths(job, inputDir); job.setMapperClass(HourlyImporter.class); TableMapReduceUtil.initTableReducerJob(tableName, null, job); job.setNumReduceTasks(0); TableMapReduceUtil.addDependencyJars(job); return job; } public static void main(String[] args)throws Exception { Configuration conf = HBaseConfiguration.create(); Job job = createSubmittableJob(conf, args); System.exit (job.waitForCompletion(true) ? 0 : 1); } } class HourlyImporter extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> { private long ts; static byte[] family = Bytes.toBytes("n"); @Override protected void setup(Context context) { ts = System.currentTimeMillis(); } public static String change(String str,int n,boolean j){ if(str==null||str.length()>=n) return str; String s=""; for(int i=str.length();i<n;i++) s+="0"; if(j) return s+str; else return str+s; } @SuppressWarnings("deprecation") public void map(LongWritable offset, Text value, Context context)throws IOException { try { String line = value.toString(); String stationID = line.substring(0, 4); String month = line.substring(5, 7); String day = line.substring(7, 9); String rowkey = stationID + month + day; byte[] bRowKey = Bytes.toBytes(rowkey); ImmutableBytesWritable rowKey = new ImmutableBytesWritable(bRowKey); Put p = new Put(bRowKey); for (int i = 1; i < 4 ; i++) { String columnI ="v" + change(String.valueOf(i),2,true); int beginIndex = i * 2 + 8; String valueI =line.substring(beginIndex, beginIndex + 2).trim(); p.add(family, Bytes.toBytes(columnI),ts, Bytes.toBytes(valueI)); } context.write(rowKey, p); }catch (InterruptedException e) { e.printStackTrace(); } } }
二、上传txt文件到HDFS系统
HDFS中的文件内容为:三、在HBase终端上创建HBase表格
创建时只需指定要创建表格的表名和列族名
四、配置java项目执行的参数
配置内容如图:
五、执行结果
如图:
0 0
- MapReduce编程实现txt文件中的内容导入HBase
- MapReduce编程实现txt文件中的内容导入HBase
- HBase导入TXT文件
- txt文件导入HBase
- MapReduce读取txt文件存储至HBase,以文件名作Key,整个文件内容作Value
- MapReduce读取txt文件存储至HBase,以文件名作Key,整个文件内容作Value
- java实现读取txt文件中的内容
- mapreduce读取文件内容输出到hbase
- 使用MapReduce将Hadoop HDFS中的日志文件导入HBase中(二)
- springMVC将txt文件或Excel文件中的内容上传并导入数据库
- List Control中的内容导入到 .txt文件(可用记事本打开)
- 将TXT文件中的内容正确导入到Excel表格中不混乱的方法
- hive实现txt数据导入,理解hadoop中hdfs、mapreduce
- 使用MapReduce读取SequenceFile文件,批量导入HBase
- PHP读取txt文件中的内容
- R语言读取txt文件中的内容
- Hbase Mapreduce编程
- hbase 结合MapReduce 批量导入
- initWithFrame方法的理解
- 公司发展该时刻谨记的是什么
- Binary-safe 二进制安全
- css图像映射
- 登录页面在输入了正确的用户名和密码后仍跳转到登录页面而不是期望的系统首页面,debug的时候发现,登录页面调用的action类方法被调用了两次,后台无法获取前台页面传过来的参数。
- MapReduce编程实现txt文件中的内容导入HBase
- iOS之渐变虚化背景图片
- My Network programming library
- 用Python写了一个数据格式转换小程序
- IOS 实现录音PCM转MP3格式(边录音边转码)
- XHTML学习<1>
- Delphi:InputQuery和InputBox的区别
- wampserver的apache服务无法启动
- iReport 报表、子报表、主从报表、合计、实例解析