Thursday, April 19, 2018

Encoding Chinese (non-ascii) for URL string

Symptom:

~/anaconda3/lib/python3.6/http/client.py in putrequest(self, method, url, skip_host, skip_accept_encoding)
   1115 
   1116         # Non-ASCII characters should have been eliminated earlier
-> 1117         self._output(request.encode('ascii'))
   1118 
   1119         if self._http_vsn == 11:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-13: ordinal not in range(128)

Solution:
import urllib.request
import urllib.parse

uriencoded = urllib.parse.quote('/s?wd=无人驾驶',encoding='UTF-8')
output = urllib.request.urlopen('https://www.baidu.com'+uriencoded)

Quick command to extract python commands (inputs) from Jupyter notebook

import json
jsonObj = json.load(open('Working with Text Data.ipynb'))
for i  in [cells['source'] for cells in jsonObj['cells']]:
    print (i[0]) if len(i)>0 else '' 


Tuesday, April 17, 2018

Pandas Dataframe example

import pandas as pd
import numpy as np
df = pd.read_csv('employees.csv',parse_dates=['Start Date','Last Login Time'])
df['Senior Management'] = df['Senior Management'].astype('bool')
df["Gender"]  = df['Gender'].astype("category")
df.info()

mask1 = df['Senior Management']
mask2 = df['Start Date']< '1990-01-01'

df [ mask1 | mask2]

mask1 = df['Gender'] == 'Male'
mask2 = df['Team'] == 'Marketing'

df[mask1 & mask2].head(3)

df[~df["Team"].duplicated()]

df.drop_duplicates(['Team'])


Sunday, April 15, 2018

count and Len difference in Pandas

In [6]: import pandas as pd

In [7]: import numpy as np

In [8]: s = pd.Series(['a','b','c','d',np.nan])

In [9]: s.describe()
Out[9]: 
count     4
unique    4
top       c
freq      1
dtype: object

In [10]: s
Out[10]: 
0      a
1      b
2      c
3      d
4    NaN
dtype: object


In [12]: s.count(),len(s)
Out[12]: (4, 5)


Sunday, April 8, 2018

Find out listening port and process behind on Linux

[donghua@cdh-vm script]$ sudo netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:21000           0.0.0.0:*               LISTEN      9715/impalad        
tcp        0      0 0.0.0.0:18088           0.0.0.0:*               LISTEN      2822/java           
tcp        0      0 0.0.0.0:25000           0.0.0.0:*               LISTEN      9715/impalad        
tcp        0      0 192.168.56.10:8040      0.0.0.0:*               LISTEN      2689/java           
tcp        0      0 192.168.56.10:9000      0.0.0.0:*               LISTEN      1200/python2.7      
tcp        0      0 0.0.0.0:18089           0.0.0.0:*               LISTEN      3754/java           
tcp        0      0 192.168.56.10:8041      0.0.0.0:*               LISTEN      2689/java           
tcp        0      0 0.0.0.0:7337            0.0.0.0:*               LISTEN      2689/java           
tcp        0      0 0.0.0.0:9994            0.0.0.0:*               LISTEN      14859/java          
tcp        0      0 192.168.56.10:8042      0.0.0.0:*               LISTEN      2689/java           
tcp        0      0 192.168.56.10:50090     0.0.0.0:*               LISTEN      2126/java           
tcp        0      0 192.168.56.10:9995      0.0.0.0:*               LISTEN      14859/java          
tcp        0      0 0.0.0.0:9996            0.0.0.0:*               LISTEN      14926/java          
tcp        0      0 0.0.0.0:7180            0.0.0.0:*               LISTEN      1067/java           
tcp        0      0 0.0.0.0:23020           0.0.0.0:*               LISTEN      3105/catalogd       
tcp        0      0 192.168.56.10:9997      0.0.0.0:*               LISTEN      14926/java          
tcp        0      0 0.0.0.0:5678            0.0.0.0:*               LISTEN      14985/java          
tcp        0      0 0.0.0.0:7182            0.0.0.0:*               LISTEN      1067/java           
tcp        0      0 0.0.0.0:7184            0.0.0.0:*               LISTEN      14867/java          
tcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN      2928/java           
tcp        0      0 0.0.0.0:22000           0.0.0.0:*               LISTEN      9715/impalad        
tcp        0      0 0.0.0.0:26000           0.0.0.0:*               LISTEN      3105/catalogd       
tcp        0      0 192.168.56.10:19888     0.0.0.0:*               LISTEN      2693/java           
tcp        0      0 0.0.0.0:7185            0.0.0.0:*               LISTEN      14867/java          
tcp        0      0 192.168.56.10:10033     0.0.0.0:*               LISTEN      2693/java           
tcp        0      0 0.0.0.0:4433            0.0.0.0:*               LISTEN      1436/python2.7      
tcp        0      0 0.0.0.0:10002           0.0.0.0:*               LISTEN      2928/java           
tcp        0      0 0.0.0.0:25010           0.0.0.0:*               LISTEN      3158/statestored    
tcp        0      0 0.0.0.0:9010            0.0.0.0:*               LISTEN      2390/java           
tcp        0      0 192.168.56.10:8083      0.0.0.0:*               LISTEN      14985/java          
tcp        0      0 0.0.0.0:8084            0.0.0.0:*               LISTEN      14867/java          
tcp        0      0 192.168.56.10:8020      0.0.0.0:*               LISTEN      2163/java           
tcp        0      0 0.0.0.0:10101           0.0.0.0:*               LISTEN      14856/java          
tcp        0      0 192.168.56.10:8086      0.0.0.0:*               LISTEN      14926/java          
tcp        0      0 192.168.56.10:8022      0.0.0.0:*               LISTEN      2163/java           
tcp        0      0 192.168.56.10:50070     0.0.0.0:*               LISTEN      2163/java           
tcp        0      0 127.0.0.1:7190          0.0.0.0:*               LISTEN      1436/python2.7      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      968/sshd            
tcp        0      0 0.0.0.0:7191            0.0.0.0:*               LISTEN      1436/python2.7      
tcp        0      0 192.168.56.10:8888      0.0.0.0:*               LISTEN      3353/python2.7      
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      9715/impalad        
tcp        0      0 192.168.56.10:8088      0.0.0.0:*               LISTEN      2717/java           
tcp        0      0 127.0.0.1:40953         0.0.0.0:*               LISTEN      2089/java           
tcp        0      0 0.0.0.0:8889            0.0.0.0:*               LISTEN      3653/httpd          
tcp        0      0 127.0.0.1:19001         0.0.0.0:*               LISTEN      1222/python         
tcp        0      0 0.0.0.0:21050           0.0.0.0:*               LISTEN      9715/impalad        
tcp        0      0 0.0.0.0:13562           0.0.0.0:*               LISTEN      2689/java           
tcp        0      0 192.168.56.10:50010     0.0.0.0:*               LISTEN      2089/java           
tcp        0      0 192.168.56.10:8091      0.0.0.0:*               LISTEN      14859/java          
tcp        0      0 0.0.0.0:9083            0.0.0.0:*               LISTEN      3000/java           
tcp        0      0 192.168.56.10:50075     0.0.0.0:*               LISTEN      2089/java           
tcp        0      0 0.0.0.0:25020           0.0.0.0:*               LISTEN      3105/catalogd       
tcp        0      0 192.168.56.10:8030      0.0.0.0:*               LISTEN      2717/java           
tcp        0      0 192.168.56.10:8031      0.0.0.0:*               LISTEN      2717/java           
tcp        0      0 192.168.56.10:8032      0.0.0.0:*               LISTEN      2717/java           
tcp        0      0 0.0.0.0:24000           0.0.0.0:*               LISTEN      3158/statestored    
tcp        0      0 192.168.56.10:8033      0.0.0.0:*               LISTEN      2717/java           
tcp        0      0 0.0.0.0:43266           0.0.0.0:*               LISTEN      2390/java           
tcp        0      0 192.168.56.10:10020     0.0.0.0:*               LISTEN      2693/java           
tcp        0      0 192.168.56.10:50020     0.0.0.0:*               LISTEN      2089/java           
tcp        0      0 0.0.0.0:2181            0.0.0.0:*               LISTEN      2390/java           
tcp6       0      0 :::3306                 :::*                    LISTEN      1065/mysqld         
tcp6       0      0 :::4434                 :::*                    LISTEN      1436/python2.7      
tcp6       0      0 :::22                   :::*                    LISTEN      968/sshd            
tcp6       0      0 :::7191                 :::*                    LISTEN      1436/python2.7      
udp        0      0 0.0.0.0:20940           0.0.0.0:*                           20170/dhclient      
udp        0      0 0.0.0.0:47725           0.0.0.0:*                           717/dhclient        
udp        0      0 0.0.0.0:7191            0.0.0.0:*                           1436/python2.7      
udp        0      0 0.0.0.0:68              0.0.0.0:*                           20170/dhclient      
udp        0      0 0.0.0.0:68              0.0.0.0:*                           717/dhclient        
udp        0      0 127.0.0.1:323           0.0.0.0:*                           660/chronyd         
udp6       0      0 :::43113                :::*                                20170/dhclient      
udp6       0      0 :::6810                 :::*                                717/dhclient        
udp6       0      0 :::7191                 :::*                                1436/python2.7      
udp6       0      0 ::1:323                 :::*                                660/chronyd         

Friday, April 6, 2018

Maximum is 65536 bytes for KUDU string

[cdh-vm.dbaglobe.com:21000] > create table byname_kudu primary key (eid) stored as kudu
 as select * from getrelationinfobyname;
Query: create table byname_kudu primary key (eid) stored as kudu
 as select * from byname
Query submitted at: 2018-04-06 21:35:17 (Coordinator: http://cdh-vm.dbaglobe.com:25000)
Query progress can be monitored at: http://cdh-vm.dbaglobe.com:25000/query_plan?query_id=68438e6127dbb1aa:7085ebb800000000
WARNINGS: Kudu error(s) reported, first error: Invalid argument: value too large for column 'c_contents' (168605 bytes, maximum is 65536 bytes)

Unpartitioned Kudu tables are inefficient for large data sizes.
Error in Kudu table 'impala::test.byname_kudu': Invalid argument: value too large for column 'c_contents' (168605 bytes, maximum is 65536 bytes)

Sunday, March 18, 2018

Install ntp packages to fix issue on cloudera manager agent installation

Install ntp packages to fix issue on cloudera manager agent installation (RHEL7: yum install ntp)

======================================

18/Mar/2018 10:40:32 +0000] 6339 MainThread agent        INFO     Active parcel list updated; recalculating component info.
[18/Mar/2018 10:40:33 +0000] 6339 MainThread throttling_logger INFO     Identified java component java8 with full version JAVA_HOME=/usr/java/defa
ult java version "1.8.0_162" Java(TM) SE Runtime Environment (build 1.8.0_162-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode
)  for requested version misc.
[18/Mar/2018 10:40:33 +0000] 6339 MainThread throttling_logger INFO     Identified java component java8 with full version JAVA_HOME=/usr/java/jdk1
.8.0_162 java version "1.8.0_162" Java(TM) SE Runtime Environment (build 1.8.0_162-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed
 mode)  for requested version 8.
[18/Mar/2018 10:40:35 +0000] 6339 Monitor-HostMonitor throttling_logger ERROR    Failed to collect NTP metrics
Traceback (most recent call last):
  File "/usr/lib64/cmf/agent/build/env/lib/python2.7/site-packages/cmf-5.14.1-py2.7.egg/cmf/monitor/host/ntp_monitor.py", line 48, in collect
    self.collect_ntpd()
  File "/usr/lib64/cmf/agent/build/env/lib/python2.7/site-packages/cmf-5.14.1-py2.7.egg/cmf/monitor/host/ntp_monitor.py", line 66, in collect_ntpd
    result, stdout, stderr = self._subprocess_with_timeout(args, self._timeout)
  File "/usr/lib64/cmf/agent/build/env/lib/python2.7/site-packages/cmf-5.14.1-py2.7.egg/cmf/monitor/host/ntp_monitor.py", line 38, in _subprocess_
with_timeout
    return subprocess_with_timeout(args, timeout)
  File "/usr/lib64/cmf/agent/build/env/lib/python2.7/site-packages/cmf-5.14.1-py2.7.egg/cmf/subprocess_timeout.py", line 49, in subprocess_with_ti
meout
    p = subprocess.Popen(**kwargs)
  File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory