java从本地上的文件_java从本地读文件并上传Hbase

java从本地读文件并上传Hbase

java从本地读文件并上传Hbase

有兴趣使用的请点下载链接

数据文件与源代码下载

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+CELL

abc column=mycf:a, timestamp=1427731972925, value=123

def column=mycf:b, time

数据文件

示例(取第一行):

YhzBzMbflE zfsNGv 172.544

本数据文件以空格隔开

取第一列为key,取第二列为value,第三列不使用

数据文件截图:

ac12c6a0b6d4c3fa697f89ae60224f15.png

综上,从本地文件读取数据,按字符流处理文件,并按行上传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

016460a83e98e4225a92b6808113e05f.png

2.浏览器打开相应ip的60010端口,Hbase UI

浏览器输入:http://104.128.92.12:60010/

此处104.128.91.12是所使用的IP号,60010是端口

e45a3ea467ef9f79c87745f310564b33.png

java从本地读文件并上传Hbase相关教程

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值