java从本地读文件并上传Hbase
来源:互联网 发布:linux gzip压缩命令 编辑:程序博客网 时间:2024/06/07 20:20
有兴趣使用的请点下载链接
数据文件与源代码下载
1 从本地文件读数据,按行操作
String filePath = "/root/input_2";File file=new File(filePath);InputStreamReader in_stream = new InputStreamReader(new FileInputStream(file)); BufferedReader in = new BufferedReader(in_stream);String s;while ((s=in.readLine())!=null ) { System.out.println(s);}
2 Hbase 创建表并put
public class HBaseTest {public static void main(String[] args) throws MasterNotRunningException,ZooKeeperConnectionException, IOException { // create table descriptor String tableName= "mytable"; HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); // create column descriptor HColumnDescriptor cf = new HColumnDescriptor("mycf"); htd.addFamily(cf); // configure HBase Configuration configuration = HBaseConfiguration.create(); HBaseAdmin hAdmin = new HBaseAdmin(configuration); hAdmin.createTable(htd); hAdmin.close(); // put "mytable","abc","mycf:a","789" HTable table = new HTable(configuration,tableName); Put put = new Put("abc".getBytes()); put.add("mycf".getBytes(),"a".getBytes(),"789".getBytes()); table.put(put); table.close(); System.out.println("put successfully");}}
3 hbase shell 表示
create 'mytable', 'mycf’创建表, column family#put 'mytable', 'abc', 'mycf:a', '123'0 row(s) in 0.0580 seconds#put 'mytable', 'def', 'mycf:b', '456'0 row(s) in 0.0060 seconds#scan 'mytable'ROW COLUMN+CELLabc column=mycf:a, timestamp=1427731972925, value=123def column=mycf:b, time
数据文件
示例(取第一行):
YhzBzMbflE zfsNGv 172.544
- 本数据文件以空格隔开
- 取第一列为key,取第二列为value,第三列不使用
数据文件截图:
综上,从本地文件读取数据,按字符流处理文件,并按行上传key,value到hbase.
表名tableName = “HB_MEM_”
colum family=”cf”
对于示例数据(YhzBzMbflE zfsNGv 172.544)
put ‘HB_MEM_’, ‘YhzBzMbflE ‘, ‘cf:value, ‘zfsNGv ’
用如下代码实现:
/** * Created by Jiao on 2017/4/3. */import java.io.IOException;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.net.URI;import java.util.ArrayList;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.text.NumberFormat;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTablePool;import org.apache.hadoop.io.IOUtils;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Put;public class project { public static void main(String[] args) throws IOException { //writeback to hbase Configuration HBASE_CONFIG = new Configuration();// HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.0.104"); String tableName = "HB_MEM_"; String family="cf"; HBaseAdmin hBaseAdmin = new HBaseAdmin(HBASE_CONFIG); if (hBaseAdmin.tableExists(tableName)) { //check hBaseAdmin.disableTable(tableName); hBaseAdmin.deleteTable(tableName); System.out.println(tableName + " is exist,detele...."); } HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); HColumnDescriptor cf= new HColumnDescriptor(family); htd.addFamily(cf); hBaseAdmin.createTable(htd); hBaseAdmin.close(); HTable HBasetable = new HTable(HBASE_CONFIG,TableName.valueOf(tableName)); String filePath = "/root/input_2"; File file=new File(filePath); InputStreamReader in_stream = new InputStreamReader(new FileInputStream(file)); BufferedReader in = new BufferedReader(in_stream); String s; int i=0; while ((s=in.readLine())!=null ) { String[] words = s.split(" "); String key = words[0]; String value=words[1]; Put put = new Put(key.getBytes()); put.add(family.getBytes(), "value".getBytes(), value.getBytes()); System.out.println("Save to Hbase! key:"+key+" "+"value:"+value); HBasetable.put(put); } HBasetable.close(); System.out.println("put successful!!!"); }}
4 验证是否存入Hbase:
在hadoop hbase已经安装成功的情况下:
1. 命令行输入以下命令,打开hbase shell 再用list命令列出文件列表
#hbase shell
#list
2.浏览器打开相应ip的60010端口,Hbase UI
浏览器输入:http://104.128.92.12:60010/
此处104.128.91.12是所使用的IP号,60010是端口
阅读全文
0 0
- java从本地读文件并上传Hbase
- java 从服务器下载文件并保存到本地
- Java文件上传(在SpringMVC模式下,从本地上传到服务器)
- java 上传文件到本地
- JAVA实现:将文件从本地上传到HDFS上、从HDFS上读取等操作
- 从本地上传文件到服务器
- java图片从本地上传服务器
- java web中监听本地磁盘文件,并进行上传,复制,处理等
- java实现从RUI获取文件并保存本地(实用方法)
- 用java从网络上抓取HTML文件内容并写入本地文本中
- 用java从网络上抓取HTML文件内容并写入本地文本中
- [Java Web]从服务器获取文件并保存(预览)本地客户端
- 上传文件到服务器,从服务器下载文件到本地
- scp从服务器下载文件到本地或从本地上传文件到服务器
- java从数据源读文件流并写文件
- Hbase 上传CSV文件
- SecureCRT 6.5从本地上传文件到linux虚拟机
- (PHP)用cURL从本地上传文件到FTP服务器
- 【版本升级】DevExtreme v17.1.3震撼来袭|附下载
- 数据库优化——优化MySQL Server
- 多态
- php like 匹配
- 【软考】权法-计算机保护条例
- java从本地读文件并上传Hbase
- Java实现简单的kmeans聚类
- 【C#机房重构】 DataGridView控件的使用
- 关于Apple Mach -O Linker Error的bug解决
- 嵌入式Linux Nat内网穿透 设置问题
- 公司电子分单系统
- 【译文】R语言不平衡数据分类指南
- 终端命令-文件操作、编辑操作
- 创建对象数组时抛出空指针异常的情况