load data into table hive - eclipse

I'm trying to Configure Hive to Work with JDBC and I used this exemple on eclipse :
public class HiveJdbcClient {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
/**
* #param args
* #throws SQLException
**/
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
Statement stmt = con.createStatement();
String tableName = "testHiveDriverTable";
stmt.executeQuery("drop table " + tableName);
ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");
// show tables
String sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
// describe table
sql = "describe " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
// load data into table
// NOTE: filepath has to be local to the hive server
// NOTE: /tmp/test_hive_server.txt is a ctrl-A separated file with two fields per line
String filepath = "/tmp/test_hive_server.txt";
sql = "load data local inpath '" + filepath + "' into table " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
// select * query
sql = "select * from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()){
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
// regular hive query
sql = "select count(1) from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()){
System.out.println(res.getString(1));
}
}
}
I was able to create the table on hive but when I try to load data into the table an error appears . so my question is what should I put into "test_hive_server.txt" to make it work! Because I tried everything and each time I get the same error.
Thanks!
the error:
Exception in thread "main" java.sql.SQLException: org.apache.thrift.TApplicationException: Internal error processing execute
at org.apache.hadoop.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:196)
at com.palmyra.nosql.HiveJdbcClient.main(HiveJdbcClient.java:52)

I found the answer by myself actually the :
ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ';
determinate the format of the rows in the "file.txt" that we are going to load in the table and Hive’s default record and field delimiters list is :
\n
^A
^B
^C
press ^V^A could insert a ^A in Vim.
or you can just do:
create table tableName (key int, value string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ';
and the 'file.txt' should be like this:
value1 value2
value3 value4
in this example value1 and value3 represent the key
and value4 and value5 represent the value

Related

MyBatis multi-row INSERT using SQL Builder

I am using MyBatis (mybatis-spring-boot-starter 2.0), I want to insert list of values into table using java sqlBuilder. I know how to write sqlBuilder in java for storing single record. But how to write the sqlBuilder for List of records?
Mapper interface (Single record):
#InsertProvider(method = "insert", type = TestSqlBuilder.class)
#Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
public void save(final TestVO testVO );
TestSqlBuilder.java (Single record):
The below method used for inserting single TestVO values.
public String insert() {
return new SQL() {
{
INSERT_INTO("TEST")
.VALUES("COL1", "#{col1}")
.VALUES("COL2", "#{col2}")
.VALUES("COL3", "#{col3}")
.VALUES("COL3", "#{col4}")
.toString();
}
}.toString();
}
Now for List of records, how to write sqlBuilder.
Else how can we convert the below to sqlBuilder java code
#Insert("<script> "
+ "insert into sys_user "
+ "(t_id, t_name, t_age) "
+ "values "
+ "<foreach collection='list' item='item' "
+ " index='index' separator=','> "
+ "(#{item.id}, #{item.name}, #{item.age}) "
+ "</foreach> "
+ "</script> ")
int insertUserListWithForeach(List<User> list);

Query always returns false in Sqlite Android

I want to check the username and password with sqlite table,In my case query always returns false...
This is my Query...
public boolean validateUser(String u_name, String p_word){
Cursor c = getReadableDatabase().rawQuery(
"SELECT FNAME,PASSWORD FROM " + TABLE_NAME + " WHERE "
+ fname + "='" + u_name+"'AND "+ password +"='"+ p_word+"'" , null);
if (c!=null)
{
return true;
}
else{
return false;
}
}
Checking whether or not the Cursor is null is basically useless as a valid Cursor will be returned by rawQuery. That is no data to extract will result in an empty Cursor with no rows, whilst if data has been extracted the Cursor will have 1 or more rows.
You should check whether or not the Cursor is empty by using the Cursor getCount() method which will return 0 if there are no rows or the number of rows. Alternately you could use some of the Cursor's move methods which will return true/false depending upon whether the move can be performed. e.g if (c.moveToFirst) { ... //row(s) exist} else { ... //no rows exists }.
You should also close the Cursor before returning. So perhaps use :-
public boolean validateUser(String u_name, String p_word){
Cursor c = getReadableDatabase().rawQuery(
"SELECT FNAME,PASSWORD FROM " + TABLE_NAME + " WHERE "
+ fname + "='" + u_name+"'AND "+ password +"='"+ p_word+"'" , null);
int count = c.getCount(); // get the number of rows
c.close(); // Close the Cursor
return count > 0; // return result (no data found = false, else true)
Many would recommend not using rawQuery but rather the convenience Cursor query method. This could be :-
public boolean validateUser(String u_name, String p_word) {
String[] columns = new String[]{
FNAME,
PASSWORD
};
String whereclause = FNAME + "=? AND " + PASSWORD + "=?";
String[] whereargs = new String[]{
u_name,
p_word
};
Cursor c = this.getWritableDatabase().query(
TABLE_NAME,
columns,
whereclause,
whereargs,
null,null,null
);
int count = c.getCount();
c.close();
return count > 0;
}
Note the above code is in-principle code and hasn't been tested so it may contain typing errors.
The above code assumes that the method is within the Database Helper class (i.e. a subclass of SQLiteOpenHelper).
The assumption has been made that FNAME and PASSWORD are the correct String variables to be used for the column names as opposed to fname and password
Assumptions have been made due to the limited code made available.

How can convert bytea to base64 in Postgres

I have now facing the problem in bytea to Base64, actually I have save the image in below query,
user_profile_pic is defind in bytea in table
Update user_profile_pic
Set user_profile_pic = (profilepic::bytea)
Where userid = userid;
after that I have select the below query,
case 1:
SELECT user_profile_pic
FROM user_profile_pic;
its return exact same as I have updated, but after passing service its display a byte format
case 2:
Select encode(user_profile_pic::bytea, 'base64')
FROM user_profile_pic;
it returns totally different result.
I want to result case 1 along with service?
its working for me, not working query if write procedure/function, i write direct code behind
conn.Open();
NpgsqlCommand command = new NpgsqlCommand("SELECT profile_pic FROM userlog WHERE cust_id = '" + CustID + "'", conn);
Byte[] result = (Byte[])command.ExecuteScalar();
if(result.Length > 0)
{
ProfilePicture = Convert.ToBase64String(result);
ErrorNumber = 0;
ErrorMessage = "Successful operation";
}
else
{
ErrorNumber = 1;
}
conn.Close();

Conversion failed when converting the varchar value to data type

i search a lot but didn't find what i want this question is ask by many users here but i didn't find my solution so i ask this question.
i have page in which i have Assign and withdraw button which
is used to Assign and withdraw device id for the selected group
id (group id fetch from dropdownlist) from one list-box to another
that part is completed
Now when i add another device id for the same group id i need to
append that updated record and show it in listbox2.
here is my code:
string queryString1 = "select device_id from device_group_master where group_id='" + ddlGroupName.SelectedValue.ToString() +"'";
SqlDataAdapter newdataAdapter = new SqlDataAdapter(queryString1, con);
SqlCommandBuilder newcommandBuilder = new SqlCommandBuilder(newdataAdapter);
newdataAdapter.Fill(ds1, "table");
if (ds1.Tables["table"].Rows.Count != 0)
{
if (ds1.Tables["table"].Rows[0][0].ToString() == "0")
{
string queryString2 = "update DEVICE_GROUP_MASTER set DEVICE_ID='" + device.ToString() + "', NOT_EXCLUDE_DEVICE_ID='" + enable + "' where GROUP_ID='" + ddlGroupName.SelectedValue.ToString() + "'";
SqlCommand update = new SqlCommand(queryString2, con);
update.CommandType = CommandType.Text;
update.ExecuteNonQuery();
}
else
{
string append;
append = ds1.Tables["table"].Rows[0][0].ToString();
append += ","+ device;
string queryString2 = "update DEVICE_GROUP_MASTER set DEVICE_ID='" + append.ToString() + "' where GROUP_ID='" + ddlGroupName.SelectedValue.ToString() + "'";
SqlCommand update = new SqlCommand(queryString2, con);
update.CommandType = CommandType.Text;
update.ExecuteNonQuery();
}
}

table not created

The project executes but table is not created. My codes
{
public HelperData(Context context) {
super(context, database_name, null, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
String creat = "Create table " + Table_name + "(" + KEY_ID
+ " Integer primary key autoincrement," + KEY_FNAME + "TEXT,"
+ KEY_LNAME + "TEXT" + ");";
Log.d("Tag", "==table created" + creat);
db.execSQL(creat);
}
}
What might be the issue?
I can't see what the content of those Strings (like KEY_FNAME or KEY_LNAME) is but it appears you don't have a space between the strings that define the column names and their data types.
E.g.:
If String KEY_LNAME = "something",
KEY_LNAME + "TEXT" can result in something like "somethingTEXT" instead of "something TEXT".
If that is the case, you can either add a space to the end of the column name string (e.g. String KEY_LNAME = "something ") or add the space before the column data type (... KEY_LNAME + " TEXT" ...).
Also, look at Logcat, there may be some Exceptions being thrown.
I may be wrong, as I don't know what those strings of yours contain, but that's my guess.